Consider a structure with bit fields. What will be the size of the following structure in bytes?
struct demo {
unsigned int a : 5;
unsigned int b : 3;
unsigned int c : 4;
unsigned int d : 7;
};
Answer: C
Bit fields in C are packed into the smallest integral type that can accommodate them.
Here, a(5) + b(3) + c(4) + d(7) = 19 bits total.
Since 19 bits exceed 16 bits but fit within 32 bits, the compiler allocates 4 bytes (32 bits) for this structure, following standard packing rules.
Q.2Hard
What is the purpose of the volatile keyword in C?
Answer: B
The volatile keyword tells the compiler that a variable's value may change at any time (due to external factors like hardware registers, signal handlers, or multi-threading) and should be read from memory every time it's accessed, rather than being optimized by the compiler or cached in a register.
Q.3Hard
In a B-tree of order m, what is the maximum number of children a non-leaf node can have?
Answer: C
A B-tree of order m has a maximum of m children per node, which means it can have a maximum of (m-1) keys.
Each node can have between ⌈m/2⌉ and m children (for non-leaf nodes), ensuring balance. B-trees are commonly used in database indexing and file systems because they minimize disk I/O operations through their multi-level structure and balanced properties.
Q.4Hard
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.5Hard
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.
Advertisement
Q.6Hard
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.7Hard
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.8Hard
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.
Q.9Hard
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.10Hard
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.11Hard
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.12Hard
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.13Hard
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.14Hard
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.15Hard
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.16Hard
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.17Hard
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.18Hard
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.19Hard
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.20Hard
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.