C Programming — Dynamic Memory
C language from basics to advanced placement prep
100 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 100 questions in Dynamic Memory
Q.21 Medium Dynamic Memory
In graph algorithms with dynamic adjacency lists, what's the memory requirement for a sparse graph with V vertices and E edges?
A O(V + E)
B O(V²)
C O(E²)
D O(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.

Take Test
Q.22 Medium Dynamic Memory
What potential issue occurs with this code in a large-scale system?
while(1) { int *arr = malloc(1000000); }
A Memory exhaustion - repeated allocation without deallocation
B Stack overflow
C Compilation error
D Segmentation fault immediately
Correct Answer:  A. Memory exhaustion - repeated allocation without deallocation
EXPLANATION

This creates an infinite loop that allocates 1MB each iteration without freeing, rapidly exhausting available memory. This is a classic memory leak scenario.

Take Test
Q.23 Medium Dynamic Memory
For storing variable-length strings dynamically in a competitive program, what's the safest approach?
A Allocate strlen(s)+1 bytes to include null terminator
B Allocate strlen(s) bytes
C Use fixed-size char array of 1000
D Allocate 2*strlen(s) for safety
Correct Answer:  A. Allocate strlen(s)+1 bytes to include null terminator
EXPLANATION

Strings in C are null-terminated, so allocating strlen(s)+1 bytes ensures space for the null terminator. This is the standard safe approach.

Take Test
Q.24 Medium Dynamic Memory
Which scenario represents a use-after-free error?
A int *p = malloc(10); free(p); p[0] = 5;
B int *p = malloc(10); free(p); free(p);
C int *p = malloc(10); int *q = p; free(p);
D free(NULL);
Correct Answer:  A. int *p = malloc(10); free(p); p[0] = 5;
EXPLANATION

In option A, after free(p), the memory is deallocated. Accessing it with p[0] = 5 is a use-after-free error, leading to undefined behavior.

Take Test
Q.25 Easy Dynamic Memory
What is the result of calling free() on a NULL pointer?
A It is safe and does nothing
B It causes undefined behavior
C It frees all allocated memory
D It returns -1
Correct Answer:  A. It is safe and does nothing
EXPLANATION

Calling free(NULL) is safe in C and does nothing. This is by design, allowing you to write code like free(ptr) without checking if ptr is NULL first.

Take Test
Q.26 Medium Dynamic Memory
In the 2024-2025 competitive exam pattern, a dynamic array needs to grow from 100 to 200 elements. Using realloc(), what should be the concern?
A Old pointers to the array may become invalid after realloc()
B realloc() always allocates new memory
C Data is lost during realloc()
D realloc() is slower than malloc() + memcpy()
Correct Answer:  A. Old pointers to the array may become invalid after realloc()
EXPLANATION

If realloc() needs to relocate the memory block to a different address, all old pointers to the array become invalid. Only the returned pointer should be used.

Take Test
Q.27 Medium Dynamic Memory
Which of the following is a memory leak?
A int *p = malloc(100); p = malloc(50);
B int *p = malloc(100); free(p);
C int *p = malloc(100); p = NULL;
D int *p = malloc(100); int *q = p;
Correct Answer:  A. int *p = malloc(100); p = malloc(50);
EXPLANATION

In option A, the first malloc(100) block becomes inaccessible after p is reassigned, but it's not freed, causing a memory leak. The original 100 bytes are lost.

Take Test
Q.28 Medium Dynamic Memory
What is the critical issue with this code snippet?
int *ptr = (int*)malloc(10);
ptr[11] = 5;
A Buffer overflow - accessing beyond allocated memory
B Memory leak - malloc not paired with free
C Type casting error
D NULL pointer dereference
Correct Answer:  A. Buffer overflow - accessing beyond allocated memory
EXPLANATION

malloc(10) allocates 10 bytes, but accessing ptr[11] goes beyond the allocated boundary, causing a buffer overflow which leads to undefined behavior and potential segmentation fault.

Take Test
Q.29 Medium Dynamic Memory
In a competitive programming problem, you need to allocate a 2D array of size 5x5 dynamically. Which approach is most efficient?
A Allocate single block of 25 integers and use 2D indexing
B Allocate 5 pointers, then allocate 5 integers for each pointer
C Use stack allocation with fixed size
D Use global arrays instead of dynamic allocation
Correct Answer:  A. Allocate single block of 25 integers and use 2D indexing
EXPLANATION

Allocating a single contiguous block is more cache-efficient and requires only one malloc() call, making it better for competitive programming where memory access speed matters.

Take Test
Q.30 Easy Dynamic Memory
What happens when malloc() fails to allocate memory?
A It returns NULL
B It throws an exception
C It allocates memory from stack
D It terminates the program
Correct Answer:  A. It returns NULL
EXPLANATION

When malloc() cannot allocate the requested memory, it returns NULL. The programmer must check for NULL before using the pointer to avoid undefined behavior.

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