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.
What is the output of the following code: printf("%d", sizeof(int));
A2 bytes
B4 bytes
CThe code will print a number (typically 4 on most systems)
DError in compilation
Correct Answer:
C. The code will print a number (typically 4 on most systems)
Explanation:
sizeof(int) returns the size of integer in bytes as an integer value. On most modern systems, this is 4 bytes, and printf with %d will print this numeric value.
Which of the following correctly initializes a 2D array in C?
Aint arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
Bint arr[][3] = {1,2,3,4,5,6,7,8,9};
CBoth A and B are correct
DNeither A nor B is correct
Correct Answer:
C. Both A and B are correct
Explanation:
Both syntaxes are valid in C. Option A explicitly specifies both dimensions, while Option B lets the compiler calculate the first dimension based on initialization.
Which of the following is NOT a valid identifier in C?
A_variable123
B123_variable
Cvariable_123
DVariable_123
Correct Answer:
B. 123_variable
Explanation:
In C, an identifier cannot start with a digit. It must start with a letter (a-z, A-Z) or underscore (_). Option B violates this rule by starting with a digit.
In C, what is the difference between single quotes and double quotes?
ANo difference, they are interchangeable
BSingle quotes are for characters, double quotes are for strings
CSingle quotes for strings, double quotes for characters
DSingle quotes cannot be used in C
Correct Answer:
B. Single quotes are for characters, double quotes are for strings
Explanation:
In C, single quotes (') are used for single character constants (char type), while double quotes (") are used for string literals (array of characters ending with null terminator).