PRACTICE • LEARN • GROW
Module 10: Dynamic Memory Allocation - Final Quiz MCQs
Solve topic-wise placement questions and improve your skills.
Module 10: Dynamic Memory Allocation - Final Quiz Progress
0 of 11 completed
0%
Topic Track
You are viewing MCQs in preview mode. Log in to select options, submit answers, and track your progress.
Log In to Answer
1
What is the critical behavioral difference between
malloc() and calloc()?
A.
malloc allocates on the stack; calloc allocates on the heap
B.
malloc leaves allocated memory uninitialized (containing random garbage); calloc initializes every allocated byte to zero
C.
malloc cannot be freed; calloc can be freed
D.
calloc takes one argument; malloc takes two
Explanation & Key
Correct Answer:
2
What is the output of accessing a pointer after calling free()?
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = (int*)malloc(sizeof(int));
*p = 50;
free(p);
printf("%d", *p);
return 0;
}
A.
50
B.
0
C.
Undefined Behavior (Use-After-Free accessing deallocated memory)
D.
Compile error
Explanation & Key
Correct Answer:
3
Why is the code
ptr = (int*)realloc(ptr, new_size); considered dangerous in production C code?
A.
It triggers a compiler syntax error in C99
B.
If realloc fails, it returns NULL, overwriting
ptr and losing the only reference to the original heap block, causing an unrecoverable memory leak
C.
It calls free() twice on ptr
D.
realloc cannot expand memory
Explanation & Key
Correct Answer:
4
What happens at runtime when freeing an offset pointer?
#include <stdlib.h>
int main() {
int *arr = (int*)malloc(5 * sizeof(int));
arr++;
free(arr);
return 0;
}
A.
Successfully frees the remaining 4 elements
B.
Runtime crash / abort (
free(): invalid pointer)
C.
Frees element 0 only
D.
Runs cleanly with no errors
Explanation & Key
Correct Answer:
5
In which segment of virtual process memory are dynamic heap allocations created during runtime?
A.
Stack segment
B.
Heap segment
C.
BSS segment
D.
Text (Code) segment
Explanation & Key
Correct Answer:
6
Where are uninitialized global and static variables stored in program memory?
A.
Heap segment
B.
Stack segment
C.
BSS (Block Started by Symbol) segment
D.
Text segment
Explanation & Key
Correct Answer:
7
What defines a "memory leak" in C systems?
A.
Accessing memory beyond array bounds
B.
Heap memory allocated with malloc/calloc that is never freed, where all pointers referencing it are lost
C.
Stack frame overflow during recursion
D.
Dereferencing a NULL pointer
Explanation & Key
Correct Answer:
8
What happens when
free(NULL) is executed in standard C?
A.
Segmentation Fault
B.
Compile error
C.
Safe no-op (the function performs no action and safely returns)
D.
OS abort
Explanation & Key
Correct Answer:
9
In standard computer architectures, what direction do the Call Stack and the Heap typically grow?
A.
Both grow upward toward high memory
B.
Both grow downward toward low memory
C.
The Stack grows downward (high to low addresses); the Heap grows upward (low to high addresses)
D.
Neither grows; their memory boundaries are strictly static
Explanation & Key
Correct Answer:
10
What happens when free() is called twice on the same pointer?
#include <stdlib.h>
int main() {
int *p = (int*)malloc(sizeof(int));
free(p);
free(p);
return 0;
}
A.
Second call is ignored as a no-op
B.
Runtime abort / heap corruption crash (Double Free Vulnerability)
C.
Reallocates the memory block
D.
Compile-time error
Explanation & Key
Correct Answer:
11
What is guaranteed by the C standard when
malloc(0) is called?
A.
Always causes a compile error
B.
Implementation-defined: either returns NULL or a non-null unique pointer that must not be dereferenced but can be safely passed to free()
C.
Always allocates 4 bytes minimum
D.
Throws a hardware exception
Explanation & Key
Correct Answer:
Question Palette
Answered
Current
Not Attempted
Wrong