Dynamic Memory Allocation in C: malloc and free: 17 MCQs with answers
All 17 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 6 questions
Q1. Which header file must be included to use `malloc` and `free`?
BeginnerShow answer & explanation
`<stdlib.h>`
`malloc`, `calloc`, `realloc`, and `free` are all declared in the standard header ``.
Q2. What does `malloc` return when it cannot allocate the requested memory?
BeginnerShow answer & explanation
`NULL`
`malloc` returns `NULL` on failure, which is why every allocation must be checked before use.
Q3. What is the state of the memory returned by a successful `malloc` call?
BeginnerShow answer & explanation
Uninitialized, containing garbage values
`malloc` returns uninitialized memory; only `calloc` zeroes the bytes it allocates.
Q4. Which function allocates memory for `n` items and initializes every byte to zero?
BeginnerShow answer & explanation
`calloc`
`calloc(n, size)` allocates space for `n` items of `size` bytes each and zero-initializes all of it.
Q5. What does `free(ptr)` do to the data stored in the block?
BeginnerShow answer & explanation
It leaves the data untouched and marks the block reusable
`free` only returns the block to the allocator; it does not erase data or change the pointer's value.
Q6. `malloc`, `calloc`, `realloc`, and `free` are declared in the standard header `<stdlib.h>` and manage heap memory at runtime.
BeginnerShow answer & explanation
True
`malloc`, `calloc`, `realloc`, and `free` are declared in the standard header `` and manage heap memory at runtime.
Building up · 6 questions
Q7. A student writes `int *arr = (int *)malloc(5);` intending to store five integers. What is the problem?
IntermediateShow answer & explanation
Only 5 bytes are allocated instead of 20
The size must be `5 * sizeof(int)`; allocating only 5 bytes means writing `arr[3]` corrupts memory beyond the block.
Q8. Why is it safer to write `int *temp = realloc(arr, new_size);` before assigning back to `arr`?
IntermediateShow answer & explanation
If `realloc` fails, the original `arr` is still valid and can be freed
If `realloc` returns `NULL`, the original block remains valid; assigning directly to `arr` would lose that pointer and leak the old block.
Q9. A program allocates memory inside a loop and never calls `free`. What is the consequence?
IntermediateShow answer & explanation
The heap shrinks with each iteration, causing a memory leak
Unfreed allocations accumulate on the heap; a long-running program will eventually exhaust RAM and crash.
Q10. Which of these is illegal because `free` only accepts heap pointers from `malloc`, `calloc`, or `realloc`?
IntermediateShow answer & explanation
`free(&x);` where `x` is a local `int`
`free` cannot release stack variables; passing the address of a local variable is undefined behavior.
Q11. After `free(arr);` the pointer `arr` still holds the old address. What is this pointer called?
IntermediateShow answer & explanation
A dangling pointer
A pointer that still references freed memory is a dangling pointer; using it is undefined behavior.
Q12. Which statement about `calloc` versus `malloc` is correct?
IntermediateShow answer & explanation
`calloc` costs slightly more time because it zeroes every byte
`calloc` zeroes every byte, which takes a little extra time but saves you from garbage values.
Topper level · 5 questions
Q13. A server allocates a buffer per request but never frees it. Over weeks, what happens?
AdvancedShow answer & explanation
Heap usage grows until RAM is exhausted and the server crashes
Heap memory persists until explicitly freed; a long-running server with leaks slowly consumes all RAM.
Q14. Consider this code: ```c int *arr = (int *)malloc(3 * sizeof(int)); free(arr); free(arr); ``` What is the problem?
AdvancedShow answer & explanation
The second `free` is a double free and corrupts the heap
Calling `free` twice on the same block is a double free, which corrupts the heap and is undefined behavior.
Q15. Which sequence correctly handles a `realloc` that may fail?
AdvancedShow answer & explanation
`int *t = realloc(arr, n * sizeof(int)); if (t) arr = t;`
Assigning to a temporary first preserves the original pointer if `realloc` fails, preventing a leak.
Q16. You need an array of counters that must all start at 0. Which allocation is the better choice?
AdvancedShow answer & explanation
`calloc`, because it zero-initializes the memory
`calloc` is preferred when zero-initialized memory is required; `malloc` would leave garbage values that must be overwritten.
Q17. Consider the following code snippet that attempts to allocate memory for an array of integers and then resize it. Identify the potential issue that could arise if the memory allocation fails during the `realloc` call: ```c int *arr = (int *)malloc(5 * sizeof(int)); if (arr == NULL) return 1; // ... use arr ... arr = realloc(arr, 10 * sizeof(int)); // ... use arr ... free(arr); ```
AdvancedShow answer & explanation
The original pointer `arr` will be lost if `realloc` fails, leading to a memory leak.
The correct option is true because if `realloc` fails, it returns `NULL`, and if you assign that `NULL` to `arr`, you lose the reference to the originally allocated memory, causing a memory leak since you can no longer free that memory.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages