What will be the output of the following C code?
int main() {
int x = 5;
int *p = &x;
int q = &p;
printf("%d", q + 1);
return 0;
}
Answer: A
The correct answer is 6 because q is a pointer to pointer that points to p, which points to x. When we dereference q, we get the value of x (which is 5), and then adding 1 gives us 6, which is printed. The key concept here is understanding pointer dereferencing: a single asterisk dereferences one level, so double asterisks dereference two levels, bringing us from q to p to the actual value stored in x. Any other answer would be incorrect because it would either misunderstand the double dereferencing operation or incorrectly interpret what value is being accessed—for instance, if someone only dereferenced once or forgot to add 1, they would get a different result.
Q.2Easy
Which of the following correctly describes the difference between malloc() and calloc() in C?
Answer: B
The answer is correct because it accurately captures the fundamental distinction between these two memory allocation functions in C. malloc() allocates a block of uninitialized memory and requires only the total number of bytes as a single argument, while calloc() allocates memory for an array of elements, takes two arguments (number of elements and size per element), and crucially initializes all allocated memory to zero. This initialization feature of calloc() is a key advantage when you need guaranteed zero values, whereas malloc() leaves memory with whatever garbage values were previously there. Other options would be incorrect if they claimed malloc() initializes memory, if they stated calloc() takes only one argument, or if they reversed which function initializes to zero.
Q.3Medium
What is the output of the following code snippet?
int main() {
char arr[] = "GATE";
char *p = arr;
printf("%d\n", sizeof(arr));
printf("%d", sizeof(p));
return 0;
}
Answer: C
The output is 5 and 8 because sizeof(arr) returns 5 bytes (4 characters plus 1 null terminator in the string "GATE"), while sizeof(p) returns 8 bytes on a 64-bit system since p is a pointer variable and pointers occupy 8 bytes in 64-bit architecture. The key concept is that sizeof() behaves differently for arrays versus pointers: when applied to an array, it returns the total memory allocated for the entire array, but when applied to a pointer, it only returns the size of the pointer itself, not what it points to. Other options would be incorrect because they either miscalculate the array size by forgetting the null terminator, use incorrect pointer sizes (like 4 bytes for 32-bit systems), or confuse the size of the array with the size of the pointer variable.
Q.4Hard
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.5Hard
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.
Advertisement
Q.6Easy
Which data structure uses the LIFO (Last In First Out) principle and is commonly used for function call management in programming languages?
Answer: B
A Stack follows LIFO principle where the last element inserted is the first one to be removed.
It is fundamental in managing function calls through the call stack, undo/redo operations, and expression evaluation.
Queues use FIFO, Heaps are used for priority ordering, and Linked Lists maintain sequential but flexible storage.
Q.7Medium
Which of the following operations has an average time complexity of O(1) in a Hash Table with good hash function and low collision rate?
Answer: C
Hash Tables achieve O(1) average-case time complexity for both insertion and search operations when using a well-designed hash function that minimizes collisions and maintains a good load factor.
Deletion also operates in O(1) average time.
However, in worst-case scenarios with poor hash functions or high collision rates, these operations can degrade to O(n).
The key to performance is maintaining low collision rates through techniques like chaining or open addressing.
Q.8Hard
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.9Medium
Which data structure is most suitable for implementing a priority queue where elements with higher priority need to be dequeued first?
Answer: C
A Heap (either Min-Heap or Max-Heap) is the optimal data structure for implementing priority queues with O(log n) insertion and deletion time complexity.
In a Max-Heap, the element with the highest priority is always at the root, enabling efficient extraction of the maximum element.
Arrays and Linked Lists would require O(n) time for priority-based operations, while Graphs are unsuitable for this purpose.
Heaps are fundamental in algorithms like Dijkstra's and Prim's for finding shortest paths.
Q.10Easy
What is the output of the following C code?
int x = 5;
printf("%d", x++);
Answer: B
The post-increment operator (x++) returns the current value of x before incrementing. So printf prints 5, and then x becomes 6.
Q.11Easy
Which of the following is the correct syntax to declare a pointer in C?
Answer: A
In C, pointers are declared using the asterisk (*) symbol before the variable name. The syntax is 'datatype *pointer_name;'
Q.12Easy
What will be the size of the following array in bytes?
char arr[10];
Answer: A
Each char occupies 1 byte in memory. An array of 10 chars will occupy 10 × 1 = 10 bytes.
Q.13Easy
What is the purpose of the 'void' keyword in C?
Answer: C
void is used in two contexts: (1) as a function return type when a function doesn't return a value, and (2) to declare a generic pointer (void *) that can point to any data type.
Q.14Easy
What is the output of the following C code?
int a = 10, b = 20;
int c = (a > b) ? a : b;
printf("%d", c);
Answer: B
The ternary operator (condition ? true_value : false_value) evaluates the condition (a > b). Since 10 is not greater than 20, it returns b which is 20.
Q.15Easy
What does the strlen() function return?
Answer: B
strlen() returns the length of a string without counting the null terminator ('\0'). For example, strlen("hello") returns 5, not 6.
Q.16Medium
What is the output of the following C code?
int x = 5;
int y = ++x + x++;
printf("%d", y);
Answer: D
This code exhibits undefined behavior because x is modified twice between sequence points without an intervening sequence point. The result depends on the compiler's implementation.
Q.17Medium
What is the difference between scanf() and gets() functions?
Answer: A
scanf() reads formatted input based on format specifiers and stops at whitespace. gets() reads a string until a newline is encountered. Note: gets() is deprecated due to buffer overflow vulnerabilities.
Q.18Medium
What is the correct way to allocate memory for a single integer using malloc()?
Answer: D
Both A and B are correct. Option A uses explicit type casting which is optional in C (not in C++). Option B avoids casting. Using sizeof(int) is preferred over hardcoding 4, as int size may vary across systems.
Q.19Medium
What is the output of the following C code?
int x = 5;
int *ptr = &x;
printf("%d %d", *ptr, x);
Answer: A
*ptr dereferences the pointer to access the value at the address it points to, which is x = 5. So both *ptr and x print 5.
Q.20Medium
What happens when you use the strcpy() function without bounds checking?
Answer: B
strcpy() does not perform bounds checking. If the source string is longer than the destination buffer, it will write beyond the buffer boundary, causing a buffer overflow. This is a security vulnerability. Using strncpy() is safer.