Home Subjects C Programming Dynamic Memory

C Programming
Dynamic Memory

C language from basics to advanced placement prep

53 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 53
Topics in C Programming
Q.21 Medium Dynamic Memory
A program uses realloc() to expand an array from 100 to 200 elements. The old address is p. After realloc():?
A p still points to old memory location
B p is automatically updated to new location
C Must assign returned value: p = realloc(p, ...)
D The array content is lost
Correct Answer:  C. Must assign returned value: p = realloc(p, ...)
EXPLANATION

realloc() returns a new pointer (old location may move). Must use: p = realloc(p, newsize) and check for NULL.

Test
Q.22 Medium Dynamic Memory
In a competitive programming scenario, you need to store strings dynamically. What is safe practice?
A char *str = malloc(100); without checking for NULL
B char *str = malloc(strlen(input)+1); with NULL check and strncpy
C char str[100] always on stack
D No allocation needed, use global arrays
Correct Answer:  B. char *str = malloc(strlen(input)+1); with NULL check and strncpy
EXPLANATION

Safe practice includes allocating exact size (input length + 1 for null terminator), checking for NULL, and using bounded string functions.

Test
Q.23 Medium Dynamic Memory
What happens if you call free() twice on the same pointer?
A Program immediately crashes
B Double free error - undefined behavior
C Memory is freed twice successfully
D No effect, second free is ignored
Correct Answer:  B. Double free error - undefined behavior
EXPLANATION

Double free is undefined behavior and can cause program crash or corruption. After first free(), the pointer should not be used.

Test
Q.24 Medium Dynamic Memory
Consider a program that repeatedly allocates memory in a loop but never frees it. What problem occurs?
A Stack overflow
B Memory leak - heap memory is exhausted
C Segmentation fault immediately
D Integer overflow
Correct Answer:  B. Memory leak - heap memory is exhausted
EXPLANATION

Allocating without freeing causes memory leak, where allocated memory is not returned to the system, eventually exhausting heap.

Test
Q.25 Medium Dynamic Memory
A competitive programmer allocates a 2D array dynamically. What is the correct approach?
A int **arr = (int**)malloc(rows * sizeof(int*)); for(i=0; i
B int arr[rows][cols] = (int**)malloc(...);
C int **arr = calloc(rows*cols, sizeof(int));
D int arr[][] = malloc(rows * cols);
Correct Answer:  A. int **arr = (int**)malloc(rows * sizeof(int*)); for(i=0; i
EXPLANATION

2D arrays are allocated as array of pointers, where each row pointer points to a dynamically allocated array.

Test
Q.26 Medium Dynamic Memory
What is the correct way to allocate a dynamic array of struct for 'n' elements?
struct Node { int data; char name[20]; };
A struct Node *arr = malloc(n);
B struct Node *arr = malloc(n * sizeof(struct Node));
C struct Node arr = malloc(n);
D struct Node *arr = calloc(n, 1);
Correct Answer:  B. struct Node *arr = malloc(n * sizeof(struct Node));
EXPLANATION

Must allocate n * sizeof(struct Node) bytes to hold all n structures.

Test
Q.27 Medium Dynamic Memory
In competitive exams, why is dynamic allocation preferred over static arrays for unknown input sizes?
A It's faster than static arrays
B Allows allocation only as much memory as needed, preventing stack overflow
C Static arrays don't work in C
D It's automatically freed by compiler
Correct Answer:  B. Allows allocation only as much memory as needed, preventing stack overflow
EXPLANATION

Dynamic allocation allocates memory on heap based on runtime input, preventing fixed-size limitations and stack overflow.

Test
Q.28 Medium Dynamic Memory
If realloc() cannot expand memory at the current location, what does it do?
A Fails and returns NULL
B Allocates new memory, copies old data, frees old memory, returns new pointer
C Extends memory anyway
D Keeps old pointer valid
Correct Answer:  B. Allocates new memory, copies old data, frees old memory, returns new pointer
EXPLANATION

realloc() finds new space if needed, copies data, deallocates old block, and returns new pointer.

Test
Q.29 Medium Dynamic Memory
What is the output of this code?
int *p = (int*)calloc(3, sizeof(int));
printf("%d %d %d", p[0], p[1], p[2]);
A Garbage values
B 0 0 0
C 3 bytes each
D Compiler error
Correct Answer:  B. 0 0 0
EXPLANATION

calloc() initializes all allocated memory to zero, so all elements will be 0.

Test
Q.30 Medium Dynamic Memory
Which scenario best demonstrates a memory leak?
A char *s = malloc(10); free(s);
B char *s = malloc(10); s = malloc(20);
C char *s = malloc(10); free(s); free(s);
D char *s = NULL; free(s);
Correct Answer:  B. char *s = malloc(10); s = malloc(20);
EXPLANATION

Original malloc(10) pointer is lost before freeing. The 10 bytes are now inaccessible - a memory leak.

Test
IGET
IGET AI
Online · Exam prep assistant
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