Home Subjects C Programming Dynamic Memory

C Programming
Dynamic Memory

C language from basics to advanced placement prep

20 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 20
Topics in C Programming
Q.11 Hard Dynamic Memory
For a program handling 10^6 integers dynamically, which allocation is most appropriate?
A int arr[1000000];
B int *arr = malloc(1000000 * sizeof(int));
C static int arr[1000000];
D auto int arr[1000000];
Correct Answer:  B. int *arr = malloc(1000000 * sizeof(int));
EXPLANATION

Stack allocation (option A) will cause stack overflow. Dynamic allocation on heap is required for large data.

Test
Q.12 Hard Dynamic Memory
What distinguishes a dangling pointer?
A A pointer that points to deallocated memory
B A pointer initialized to NULL
C A pointer that hasn't been malloc'd
D A pointer that points to stack variable
Correct Answer:  A. A pointer that points to deallocated memory
EXPLANATION

Dangling pointer points to memory that has been freed. Accessing it causes undefined behavior.

Test
Q.13 Hard Dynamic Memory
Consider this code:
int *p = malloc(sizeof(int));
int *q = p;
free(p);
q[0] = 5; // What is the result?
A Valid assignment
B Compiler error
C Use-after-free: undefined behavior
D Memory automatically reallocated
Correct Answer:  C. Use-after-free: undefined behavior
EXPLANATION

Both p and q point to freed memory. Writing to q after free(p) is undefined behavior.

Test
Q.14 Hard Dynamic Memory
What is the risk in this code?
char *str = malloc(5);
strcpy(str, "Hello World");
A Memory leak (str not freed)
B Buffer overflow - writing 11 characters into 5 bytes
C strcpy() is deprecated
D Both A and B
Correct Answer:  D. Both A and B
EXPLANATION

Buffer overflow occurs (11 chars into 5 bytes), and memory is never freed, causing a leak.

Test
Q.15 Hard Dynamic Memory
What is a common mistake in this code?
void func() {
int *ptr;
ptr = malloc(sizeof(int) * 5);
func2(ptr);
free(ptr);
}
void func2(int *p) {
free(p);
}
A Double free error - memory freed in both functions
B Memory leak - malloc without free
C Buffer overflow
D Segmentation fault guaranteed
Correct Answer:  A. Double free error - memory freed in both functions
EXPLANATION

Memory is freed in func2(), then again in func(), causing double free error.

Test
Q.16 Hard Dynamic Memory
What does this realloc() call do?
int *p = malloc(5 * sizeof(int));
p = realloc(p, 10 * sizeof(int));
A Allocates new 10-integer block, discards old data
B Expands existing block to 10 integers, preserves old data
C Frees old block and allocates new one
D Returns error
Correct Answer:  B. Expands existing block to 10 integers, preserves old data
EXPLANATION

realloc() resizes the memory block while preserving existing data. If expansion in-place fails, it allocates new block and copies data.

Test
Q.17 Hard Dynamic Memory
What is the correct way to allocate memory for 2D dynamic array (3x4)?
A int arr[3][4] = malloc(...);
B int **arr = malloc(3 * sizeof(int*)); for each row: malloc(4 * sizeof(int));
C int *arr = malloc(12 * sizeof(int));
D int arr = malloc(3*4*sizeof(int));
Correct Answer:  B. int **arr = malloc(3 * sizeof(int*)); for each row: malloc(4 * sizeof(int));
EXPLANATION

For true 2D dynamic array, allocate pointer array first, then allocate each row. Option C is 1D linear allocation.

Test
Q.18 Hard Dynamic Memory
What is the issue in this code?
void func() {
int *p = malloc(sizeof(int));
*p = 5;
}
int main() {
func();
// p is not accessible here
return 0;
}
A Syntax error
B Memory leak - allocated memory is never freed
C Stack overflow
D No issue
Correct Answer:  B. Memory leak - allocated memory is never freed
EXPLANATION

The pointer p is local to func(). Memory is allocated but never freed, causing a memory leak.

Test
Q.19 Hard Dynamic Memory
Which statement about dynamic memory is TRUE?
A Dynamic memory is faster than stack memory
B Dynamic memory persists until explicitly freed
C Dynamic memory is limited by cache size
D Dynamic memory is automatically freed at function end
Correct Answer:  B. Dynamic memory persists until explicitly freed
EXPLANATION

Dynamic memory persists until free() is called, unlike automatic variables which are freed at function end.

Test
Q.20 Hard Dynamic Memory
What will happen if malloc() fails and returns NULL but code doesn't check?
A Program automatically allocates from stack
B Dereferencing NULL causes undefined behavior/crash
C Memory is automatically reallocated
D Exception is raised
Correct Answer:  B. Dereferencing NULL causes undefined behavior/crash
EXPLANATION

Dereferencing a NULL pointer causes undefined behavior, typically resulting in segmentation fault or program crash.

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