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`?

Beginner
A `<math.h>` B `<stdlib.h>` C `<string.h>` D `<stdio.h>`
Show 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?

Beginner
A `NULL` B A pointer to the stack C A pointer to zero bytes D `-1`
Show 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?

Beginner
A Filled with the previous program's data B All bytes set to zero C Uninitialized, containing garbage values D Read-only and protected
Show 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?

Beginner
A `free` B `calloc` C `malloc` D `realloc`
Show 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?

Beginner
A It erases all bytes to zero B It overwrites the data with `NULL` C It copies the data to the stack D It leaves the data untouched and marks the block reusable
Show 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.

Beginner
A True B False
Show 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?

Intermediate
A Only 5 bytes are allocated instead of 20 B The cast is illegal in C C `malloc` cannot allocate fewer than 16 bytes D The pointer type must be `void*`
Show 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`?

Intermediate
A The compiler requires a temporary for `realloc` B It prevents the block from being moved C If `realloc` fails, the original `arr` is still valid and can be freed D `realloc` is faster with a temporary variable
Show 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?

Intermediate
A The heap shrinks with each iteration, causing a memory leak B `malloc` starts returning the same block C The compiler automatically frees it at exit D The stack overflows immediately
Show 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`?

Intermediate
A `free(ptr);` followed by `ptr = NULL;` B `free(&x);` where `x` is a local `int` C `free(arr);` where `arr` came from `malloc` D `free(temp);` where `temp` came from `realloc`
Show 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?

Intermediate
A A void pointer B A dangling pointer C A null pointer D A wild pointer
Show 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?

Intermediate
A `malloc` always zeroes memory, `calloc` does not B `calloc` costs slightly more time because it zeroes every byte C Both return memory filled with garbage D `calloc` is faster because it skips initialization
Show 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?

Advanced
A The buffers are moved to the stack B Heap usage grows until RAM is exhausted and the server crashes C `malloc` starts returning `NULL` immediately D The operating system reclaims each buffer automatically
Show 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?

Advanced
A `malloc` should have been `calloc` B The second `free` is a double free and corrupts the heap C The cast should be removed D The first `free` is unnecessary
Show 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?

Advanced
A `int *t = realloc(arr, n * sizeof(int)); if (t) arr = t;` B `free(arr); arr = realloc(arr, n * sizeof(int));` C `arr = realloc(arr, n * sizeof(int));` D `realloc(arr, n * sizeof(int));` without assignment
Show 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?

Advanced
A `calloc`, because it zero-initializes the memory B `malloc`, because it is always faster C A stack array, because it is automatically zeroed D `realloc`, because it resizes on demand
Show 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); ```

Advanced
A The new size of `arr` will default to zero if `realloc` fails. B The program will automatically handle the failure and terminate safely. C The memory allocated for the original `arr` will be freed automatically. D The original pointer `arr` will be lost if `realloc` fails, leading to a memory leak.
Show 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.

Make your own

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