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 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.
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.
Correct Answer:
B. Create an alias for existing data types
EXPLANATION
typedef creates an alias (synonym) for existing data types. For example, 'typedef int Integer;' creates 'Integer' as an alias for 'int'. This improves code readability and portability. It doesn't create entirely new types, but provides alternative names.
Which of the following function declarations is correct for a function that takes no parameters and returns no value?
Avoid function(void);
Bvoid function();
Cnull function(void);
Dvoid function(null);
Correct Answer:
A. void function(void);
EXPLANATION
The correct syntax is 'void function(void);' where 'void' in parentheses explicitly states no parameters. Option B is also valid in C (defaults to no parameters), but option A is more explicit and portable across C standards.
What is the difference between structure and union in C?
ABoth allocate memory to all members simultaneously
BStructure allocates memory to all members; union allocates shared memory
CUnion allocates memory to all members; structure allocates shared memory
DNo difference
Correct Answer:
B. Structure allocates memory to all members; union allocates shared memory
EXPLANATION
Structure allocates separate memory for each member (total size = sum of all members). Union shares a single memory location among all members (total size = size of largest member). This is a fundamental difference in memory allocation.
What will be the output of: int x = 5; printf("%d %d", x++, ++x);?
A5 7
B6 7
C5 6
D6 6
Correct Answer:
A. 5 7
EXPLANATION
x++ is post-increment (returns 5, then x becomes 6), ++x is pre-increment (x becomes 7, then returns 7). The order of evaluation in printf is implementation-dependent, but typically right to left, giving 5 7.
Which of the following is an invalid identifier in C?
AmyVar
Bmy_var
Cmy$var
D_myVar
Correct Answer:
C. my$var
EXPLANATION
In C, identifiers can only contain alphanumeric characters (a-z, A-Z, 0-9) and underscores (_), and must start with a letter or underscore. The dollar sign (\() is not allowed, making 'my\)var' invalid.