What happens when you try to free a pointer that was not allocated by malloc/calloc/realloc?
Answer: A
Calling free() on a pointer that wasn't returned by malloc/calloc/realloc causes undefined behavior, potentially corrupting the heap and crashing the program.
Q.82Hard
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.83Hard
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.84Easy
How many bytes are allocated by malloc(sizeof(int) * 10) on a system where int is 4 bytes?
Which of the following correctly deallocates a 2D dynamically allocated array created as int **arr?
Answer: B
A 2D array created with double pointers requires freeing each row first, then the pointer array itself to avoid memory leaks.
Advertisement
Q.86Easy
What is the primary difference between calloc() and malloc() in C?
Answer: B
calloc(n, size) allocates n*size bytes and initializes all to 0, while malloc(size) allocates size bytes with garbage values.
Q.87Easy
In a dynamic queue implementation, if front pointer is NULL, what does it indicate?
Answer: C
In queue implementations using linked lists, NULL front pointer indicates queue is empty as there are no nodes to dequeue.
Q.88Medium
What happens when realloc() is called on a pointer allocated by malloc() with a larger size?
Answer: C
realloc() attempts to expand memory in place if possible; if not, allocates new block, copies old data, and frees old block.
Q.89Medium
For implementing a dynamic circular buffer of size n, how many pointers are minimally required?
Answer: B
A circular buffer requires head and tail pointers minimum to track current positions in the circular structure.
Q.90Medium
Which scenario is most likely to cause a segmentation fault in dynamic memory?
Answer: B
Buffer overflow by accessing beyond allocated boundaries causes segmentation fault as it accesses invalid memory pages.
Q.91Medium
In competitive programming for graph problems, if adjacency list uses dynamic arrays, what is the time complexity of adding an edge?
Answer: A
Using dynamic arrays (vectors in C++) with doubling strategy provides O(1) amortized time for appending edges.
Q.92Medium
What is the correct way to store variable-length strings dynamically?
Answer: B
Proper way requires dynamic allocation with malloc, adding 1 for null terminator, and using fgets for safe input.
Q.93Easy
If malloc() returns NULL, what should a program do?
Answer: B
NULL return from malloc indicates allocation failure; ignoring causes dereferencing NULL which leads to crash.
Q.94Easy
In implementing a dynamic doubly-linked list, what additional field is required compared to singly-linked list?
Answer: B
Doubly-linked list nodes require both next and previous pointers to enable bidirectional traversal unlike singly-linked lists.
Q.95Hard
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.96Hard
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.97Hard
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.98Hard
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.