How does the 'ternary' operator (?:) work in C?

  • It defines a function in C
  • It performs bit-level operations
  • It's used for binary arithmetic
  • It's used to create a conditional expression
The 'ternary' operator in C, represented as '?:', is a shorthand way to write a simple conditional expression. It allows you to return one of two values based on a condition.

How do you declare a two-dimensional array of integers with 3 rows and 4 columns?

  • int arr[12];
  • int arr[3][4];
  • int arr[4][3];
  • int arr[][];
To declare a 2D array in C with 3 rows and 4 columns, you use the syntax: int arr[3][4]; The first dimension specifies the number of rows, and the second dimension specifies the number of columns.

In the context of C programming, what is the importance of having a base case in a recursive function?

  • It makes the program run faster
  • It prevents infinite recursion and stack overflow
  • It reduces the need for using pointers
  • It simplifies the program's overall structure
Having a base case in a recursive function is crucial to prevent infinite recursion, which can lead to a stack overflow and program termination. The base case provides a stopping condition for the recursion and ensures the function terminates properly.

You are writing a C program where you need to maintain a counter that persists across multiple function calls. Which type of variable would you use?

  • Automatic variable
  • Global variable
  • Local variable
  • Static variable
In this scenario, you would use a static variable to maintain a counter that persists across multiple function calls. Static variables retain their values between function calls, making them suitable for this purpose. Local variables have limited scope and are not suitable for maintaining a persistent counter. Global variables introduce unnecessary complexity and may lead to naming conflicts. Automatic variables are limited to a function's scope and are not designed for persistent storage.

What happens to the memory allocation when a structure is defined?

  • Memory is allocated for all its members
  • Memory is allocated for the structure itself
  • Memory is allocated only for the first member
  • No memory allocation occurs
When you define a structure in C, memory is allocated for the structure itself, which includes memory for all its members.

In a union, all members share the same ________.

  • Data Type
  • Memory Address
  • Name
  • Value
In a union, all members share the same memory location. Unions allow different variables to occupy the same memory space, providing efficient storage.

How can using pointers to structures optimize memory usage in a C program?

  • Efficient memory allocation
  • Faster program execution
  • Improved data security
  • Reducing memory leaks
Using pointers to structures allows for more efficient memory allocation since only memory addresses are stored, reducing memory overhead. It also helps in preventing memory leaks by allowing explicit memory deallocation.

What considerations should be taken into account when using nested loops?

  • Nested loops are only used for mathematical calculations and not in other scenarios.
  • Nested loops should always have the same loop control variable.
  • Nested loops should never be used in programming.
  • The number of iterations in nested loops can grow quickly, impacting performance.
When using nested loops, it's important to consider the potential impact on performance. The number of iterations in nested loops can grow exponentially, especially if the outer loop iterates N times and the inner loop iterates M times, resulting in N * M iterations. This can lead to performance issues, so it's essential to carefully plan and optimize nested loop usage.

In C, which operator has the highest precedence?

  • Arithmetic operators
  • Assignment operators
  • Logical operators
  • Relational operators
In C, arithmetic operators have the highest precedence, meaning they are evaluated first in an expression.

What is the primary difference between the scope and the lifetime of a variable?

  • Scope and lifetime are the same thing in C.
  • Scope defines the data type of a variable, while lifetime determines its value.
  • Scope determines whether a variable is local or global, while lifetime indicates its data type.
  • Scope refers to the range where a variable can be accessed, while lifetime refers to how long the variable exists.
The primary difference between the scope and lifetime of a variable is that scope refers to where a variable can be accessed, while lifetime indicates how long the variable exists in memory.