C Programming — Dynamic Memory
C language from basics to advanced placement prep
100 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 100 questions in Dynamic Memory
Q.1 Hard Dynamic Memory
Which memory allocation technique provides better locality of reference for accessing array elements?
A Allocating each element separately with malloc
B Single malloc for contiguous array
C Using static arrays
D Allocating with calloc for zeroed memory
Correct Answer:  B. Single malloc for contiguous array
EXPLANATION

Contiguous allocation places array elements next to each other in memory, improving cache hits and CPU performance through spatial locality.

Take Test
Q.2 Hard Dynamic Memory
In competitive coding, when implementing dynamic trees, which traversal is most suitable for level-order?
A Recursion with DFS
B Stack-based iteration
C Queue-based BFS using dynamic allocation
D In-order traversal
Correct Answer:  C. Queue-based BFS using dynamic allocation
EXPLANATION

Level-order traversal requires BFS which uses a queue. Dynamic queue allocation handles arbitrary tree sizes efficiently.

Take Test
Q.3 Hard Dynamic Memory
What is the memory overhead when allocating 1000 integers vs 1 integer in a system with malloc metadata?
A Negligible difference
B Proportional to number of allocations
C Fixed overhead per allocation regardless of size
D Exponential with allocation count
Correct Answer:  C. Fixed overhead per allocation regardless of size
EXPLANATION

Each malloc() call has fixed metadata overhead (typically 8-16 bytes). Single allocation of 1000 ints has less overhead than 1000 separate allocations.

Take Test
Q.4 Hard Dynamic Memory
For a dynamic hash table with chaining, if collision occurs, the new element should be inserted where?
A Always at the beginning of the chain
B Always at the end of the chain
C In sorted position
D Depends on implementation, typically at beginning for O(1)
Correct Answer:  D. Depends on implementation, typically at beginning for O(1)
EXPLANATION

While both work, inserting at beginning provides O(1) insertion; end requires traversal. Choice depends on use case.

Take Test
Q.5 Easy Dynamic Memory
In implementing a dynamic doubly-linked list, what additional field is required compared to singly-linked list?
A Data field
B Previous pointer
C Size field
D Head pointer
Correct Answer:  B. Previous pointer
EXPLANATION

Doubly-linked list nodes require both next and previous pointers to enable bidirectional traversal unlike singly-linked lists.

Take Test
Advertisement
Q.6 Easy Dynamic Memory
If malloc() returns NULL, what should a program do?
A Ignore and continue using the pointer
B Check for NULL and handle the error appropriately
C Always typecast the return value
D Use global variables instead
Correct Answer:  B. Check for NULL and handle the error appropriately
EXPLANATION

NULL return from malloc indicates allocation failure; ignoring causes dereferencing NULL which leads to crash.

Take Test
Q.7 Medium Dynamic Memory
What is the correct way to store variable-length strings dynamically?
A char str[MAX]; gets(str);
B char *str = (char*)malloc(length+1); fgets(str, length+1, stdin);
C char *str; scanf("%s", str);
D char str[] = malloc(100);
Correct Answer:  B. char *str = (char*)malloc(length+1); fgets(str, length+1, stdin);
EXPLANATION

Proper way requires dynamic allocation with malloc, adding 1 for null terminator, and using fgets for safe input.

Take Test
Q.8 Medium Dynamic Memory
In competitive programming for graph problems, if adjacency list uses dynamic arrays, what is the time complexity of adding an edge?
A O(1) amortized
B O(n)
C O(log n)
D O(n²)
Correct Answer:  A. O(1) amortized
EXPLANATION

Using dynamic arrays (vectors in C++) with doubling strategy provides O(1) amortized time for appending edges.

Take Test
Q.9 Medium Dynamic Memory
Which scenario is most likely to cause a segmentation fault in dynamic memory?
A Calling malloc() multiple times
B Accessing memory beyond allocated size
C Freeing NULL pointer
D Calling realloc() on allocated memory
Correct Answer:  B. Accessing memory beyond allocated size
EXPLANATION

Buffer overflow by accessing beyond allocated boundaries causes segmentation fault as it accesses invalid memory pages.

Take Test
Q.10 Medium Dynamic Memory
For implementing a dynamic circular buffer of size n, how many pointers are minimally required?
A 1 pointer
B 2 pointers
C 3 pointers
D 4 pointers
Correct Answer:  B. 2 pointers
EXPLANATION

A circular buffer requires head and tail pointers minimum to track current positions in the circular structure.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips