In C, when passing arrays to functions, what is actually passed?
AA copy of all array elements
BThe address of the first element (pointer)
CThe array size
DAll array elements and size
Correct Answer:
B. The address of the first element (pointer)
Explanation:
When an array is passed to a function in C, it decays to a pointer pointing to the first element. This is why changes made in the function affect the original array.
What does the static keyword do when used with a global variable?
AMakes it accessible only within the same file
BMakes it a constant
CIncreases its memory allocation
DMakes it thread-safe
Correct Answer:
A. Makes it accessible only within the same file
Explanation:
When static is used with a global variable, it restricts its scope to the file where it is declared. Without static, global variables are accessible across files using extern.
What is the difference between declaration and definition in C?
AThey are the same thing
BDeclaration informs compiler about type, definition allocates memory
CDefinition comes before declaration
DDeclaration is only for functions
Correct Answer:
B. Declaration informs compiler about type, definition allocates memory
Explanation:
Declaration tells the compiler about a variable's name and type (e.g., 'extern int x;'). Definition actually allocates memory (e.g., 'int x = 5;'). A variable can be declared multiple times but defined only once.
What will be the output of: int x = 10; int y = x++ + ++x; printf("%d", y);
A20
B21
C22
D23
Correct Answer:
B. 21
Explanation:
x++ returns 10 and increments x to 11. ++x increments x to 12 and returns 12. So y = 10 + 12 = 22. Wait, let me recalculate: y = 10 + 11 = 21 (as x becomes 11 after first post-increment, then ++x makes it 12).
What happens if you declare a variable but don't initialize it in C?
ACompilation error
BIt contains garbage value
CIt is automatically initialized to 0
DThe program crashes
Correct Answer:
B. It contains garbage value
Explanation:
Uninitialized local variables contain garbage values (unpredictable values from previous memory contents). Global and static variables are automatically initialized to 0, but local variables are not.
Consider: int *ptr = NULL; *ptr = 5; What will happen?
A5 is stored at NULL address
BSegmentation fault/Access violation
CCompilation error
DNo error, value is lost
Correct Answer:
B. Segmentation fault/Access violation
Explanation:
Attempting to dereference a NULL pointer causes undefined behavior, typically resulting in a segmentation fault (runtime error). A NULL pointer doesn't point to valid memory.
AThe variable can be modified externally at any time
BThe variable cannot be modified
CThe variable is stored in a register
DThe variable has global scope
Correct Answer:
A. The variable can be modified externally at any time
Explanation:
The volatile keyword tells the compiler that a variable's value may change unexpectedly (e.g., due to hardware, interrupts, or other threads), preventing compiler optimizations that assume the value doesn't change.