What is the output of the following C code?
#include <stdio.h>
int main() {
int a = 5;
printf("%d %d %d", a++, ++a, a);
return 0;
}
Answer: D
This code contains undefined behavior because variable 'a' is modified multiple times (a++, ++a) without intervening sequence points in the same expression. The order of evaluation is unspecified, making the result compiler-dependent.
Q.2Hard
What is the correct way to declare a constant pointer to a constant integer?
Answer: D
Both declarations are equivalent. 'const int * const ptr' and 'int const * const ptr' declare a constant pointer to a constant integer. The first const makes the integer constant, the second const makes the pointer constant.
Q.3Hard
Consider the following C code. What will be printed?
int arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
int *ptr = (int *)arr;
printf("%d", *(ptr + 5));
Answer: B
A 2D array is stored in row-major order in memory: 1,2,3,4,5,6,7,8,9. When ptr is cast to int*, ptr+5 points to the 6th element (0-indexed), which is 6.
Q.4Hard
What is the output of the following C code?
#define MAX 5
int main() {
int arr[MAX];
printf("%d", sizeof(arr)/sizeof(arr[0]));
return 0;
}
Answer: B
Step 1: sizeof(arr) = 5 × sizeof(int) = 20 bytes (assuming 4-byte int). Step 2: sizeof(arr[0]) = sizeof(int) = 4 bytes. Step 3: 20 ÷ 4 = 5. This calculates the number of elements in the array.
Q.5Hard
What is the difference between struct and union in C?
Answer: B
In a struct, each member has its own memory allocation, so the total size is the sum of all members. In a union, all members share the same memory location, so the size equals the largest member. Only one member can hold a value at a time in a union.
Advertisement
Q.6Hard
What will be the output of: int x = 5; printf("%d", ++x);?
Answer: B
The pre-increment operator (++x) increments x from 5 to 6 before using its value in printf(). So the output is 6.
Q.7Hard
Consider a pointer ptr pointing to an integer array. What does ptr[2] represent?
Answer: B
ptr[2] is equivalent to *(ptr+2), which dereferences the pointer and returns the value at the third element (index 2).
Q.8Hard
What is the time complexity of searching for an element in an unsorted array using linear search?
Answer: C
Linear search checks each element sequentially. In the worst case, it needs to check all n elements, resulting in O(n) time complexity.
Q.9Hard
What will be the result of executing: int a = 5, b = 10; int *ptr = &a; ptr = &b; printf("%d", *ptr);?
Answer: B
ptr is reassigned to point to b. When we dereference ptr using *ptr, we get the value stored at b, which is 10.
Q.10Hard
Which of the following statements about static variables is TRUE?
Answer: B
Static variables are initialized only once and retain their value throughout the program execution. Their value persists between function calls.
Q.11Hard
What happens when you try to access an array element beyond its size in C?
Answer: C
C does not perform bounds checking on arrays. Accessing beyond array bounds results in undefined behavior - it may access garbage values, crash, or seem to work without error. This is a common source of bugs.
Q.12Hard
Which of the following is the correct way to pass a string to a function in C?
Answer: D
Both char s[] (array notation) and char *s (pointer notation) are valid ways to pass strings to functions in C. C does not have a built-in string type; strings are represented as character arrays or pointers to char.
Q.13Hard
Which of the following is an invalid identifier in C?
Answer: C
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 ()isnotallowed,making′myvar' invalid.
Q.14Hard
What will be the output of: int x = 5; printf("%d %d", x++, ++x);?
Answer: A
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.
Q.15Hard
What is the difference between structure and union in C?
Answer: B
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.
Q.16Hard
Which of the following function declarations is correct for a function that takes no parameters and returns no value?
Answer: A
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.
Q.17Hard
In C, what is the purpose of the typedef keyword?
Answer: B
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.
Q.18Hard
In C, when passing arrays to functions, what is actually passed?
Answer: B
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.
Q.19Hard
What will be the output of: int a = 5; int *p = &a; printf("%d %d", a, *p);?
Answer: B
p is a pointer storing the address of a. *p (dereferencing) gives the value at that address, which is 5. So both a and *p print 5.
Q.20Hard
What does the static keyword do when used with a global variable?
Answer: A
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.