"Hello World" is 11 bytes (including null terminator). Writing to 10-byte buffer causes buffer overflow, corrupting adjacent memory.
Q.167Hard
For implementing a dynamic stack in competitive programming, which memory management approach is best?
Answer: B
Single allocation with index tracking minimizes malloc/free overhead, suitable for competitive programming time constraints.
Q.168Hard
In a dynamic stack implementation for competitive programming, how should you handle reallocation when the stack becomes full?
Answer: A
Doubling capacity and using realloc() provides amortized O(1) insertion with manageable memory overhead, which is the standard approach for dynamic data structures.
Q.169Hard
Which of these represents proper error handling for malloc in competitive programming?
Answer: A
Checking if malloc returns NULL before using the pointer is essential error handling. If allocation fails, the program should handle it gracefully rather than proceeding with NULL.
Q.170Hard
For a dynamic hash table with chaining, if collision occurs, the new element should be inserted where?
Answer: D
While both work, inserting at beginning provides O(1) insertion; end requires traversal. Choice depends on use case.
Q.171Hard
What is the memory overhead when allocating 1000 integers vs 1 integer in a system with malloc metadata?
Answer: C
Each malloc() call has fixed metadata overhead (typically 8-16 bytes). Single allocation of 1000 ints has less overhead than 1000 separate allocations.
Q.172Hard
In competitive coding, when implementing dynamic trees, which traversal is most suitable for level-order?
Answer: C
Level-order traversal requires BFS which uses a queue. Dynamic queue allocation handles arbitrary tree sizes efficiently.
Q.173Hard
Which memory allocation technique provides better locality of reference for accessing array elements?
Answer: B
Contiguous allocation places array elements next to each other in memory, improving cache hits and CPU performance through spatial locality.
Q.174Hard
Which statement about #define is TRUE?
Answer: B
Macros are purely textual substitutions performed by the preprocessor at compile time, not at runtime. They don't allocate memory themselves.
Q.175Hard
What is the problem with this macro?
#define DOUBLE(x) x*x
int result = DOUBLE(2+3);
Answer: B
The macro expands to 2+3*2+3 = 2+6+3 = 11, not 25. This is because x is not parenthesized. Should use #define DOUBLE(x) ((x)*(x))
Q.176Hard
What will be the output?
#define MIN(a,b) ((a)<(b)?(a):(b))
int x=5; int y=10;
int z = MIN(x++, y++);
Answer: A
The macro expands to ((x++)<(y++)?(x++):(y++)). In the condition, x++ returns 5 and increments x to 6. Since 5<10 is true, x++ is evaluated again (x becomes 7, but z gets 6). Actually, this shows side effects problem - x becomes 7, y stays 10.
Q.177Hard
What is the difference between #if and #ifdef?
Answer: A
#ifdef checks if a macro is defined (exists). #if evaluates a constant integer expression (can check macro values, compare numbers, etc.).
Q.178Hard
What preprocessor features should be avoided for safer code?
Answer: B
Function-like macros can cause issues due to multiple evaluation of arguments and operator precedence problems. Modern C prefers inline functions and const for safety.
Q.179Hard
Consider:
#define MAX(a,b) ((a)>(b)?(a):(b))
Which advantage does this provide?
Answer: D
Macros with parentheses provide inline substitution (better performance, no function call overhead) and can work with any data type. However, they lack type safety that functions provide.
Q.180Hard
What will be output?
#define PRINT(x) printf(#x " = %d\n", x)
PRINT(5+3);
Answer: A
The # operator converts 5+3 into the string "5+3". The macro expands to printf("5+3" " = %d\n", 5+3) which prints: 5+3 = 8