What happens when you try to free a pointer that was not allocated by malloc/calloc/realloc?
AUndefined behavior - likely crash or memory corruption
BThe program ignores the free() call
CIt frees some random memory safely
DCompilation error occurs
Correct Answer:
A. Undefined behavior - likely crash or memory corruption
EXPLANATION
Calling free() on a pointer that wasn't returned by malloc/calloc/realloc causes undefined behavior, potentially corrupting the heap and crashing the program.
Which function should be used to allocate memory for an array of 50 structures of size 'sizeof(struct Node)' and initialize to zero?
Acalloc(50, sizeof(struct Node))
Bmalloc(50 * sizeof(struct Node))
Crealloc(NULL, 50)
Dalloc(50, sizeof(struct Node))
Correct Answer:
A. calloc(50, sizeof(struct Node))
EXPLANATION
calloc(50, sizeof(struct Node)) allocates memory for 50 elements of struct Node size and initializes all bytes to zero, which is ideal when you need initialized memory.
What is the advantage of dynamic allocation over static allocation in competitive programming?
AFlexibility to use memory based on input size, avoiding stack limits
BFaster execution speed
CNo need to include headers
DAutomatic memory cleanup
Correct Answer:
A. Flexibility to use memory based on input size, avoiding stack limits
EXPLANATION
Dynamic allocation allows using memory proportional to actual input size, preventing TLE from stack overflow with large static arrays and enabling solutions for variable-sized problems.
In graph algorithms with dynamic adjacency lists, what's the memory requirement for a sparse graph with V vertices and E edges?
AO(V + E)
BO(V²)
CO(E²)
DO(V * E)
Correct Answer:
A. O(V + E)
EXPLANATION
Using dynamic adjacency lists, you allocate V list pointers and E edge nodes total, giving O(V + E) space complexity, which is optimal for sparse graphs.