C Memory Management Quiz — 19 Questions with Answers

Test your understanding of C memory management with these 19 multiple-choice questions — malloc and free, dangling pointers, memory leaks, and stack versus heap. Every question has the answer and a short explanation hidden below it, and the trickier ones link to a full walkthrough with compilable code.

These questions come from our free Android app, C Programming Quiz — 150+ questions across 9 categories with score tracking, if you prefer to practise on your phone.

How to use this page: answer each question yourself before tapping Show answer. If you get one wrong, follow the linked explanation — knowing why the wrong options are wrong is what interviews test.

Question 1 of 19 (Easy)

Which function allocates a block of memory on the heap?

  1. malloc
  2. alloc
  3. new
  4. create
Show answer

Answer: A — malloc

malloc (and calloc/realloc) allocate dynamic memory on the heap. 'new' is C++, not C.

Question 2 of 19 (Medium)

What does malloc return if it cannot allocate the requested memory?

  1. NULL
  2. The requested size
  3. A valid pointer anyway
  4. -1
Show answer

Answer: A — NULL

On failure malloc returns a null pointer (NULL). Always check the return value before using it.

Question 3 of 19 (Medium)

Which header declares malloc and free?

  1. stdio.h
  2. stdlib.h
  3. string.h
  4. memory.h
Show answer

Answer: B — stdlib.h

malloc, calloc, realloc and free are declared in <stdlib.h>.

Question 4 of 19 (Medium)

When does a memory leak occur?

  1. When you read past an array
  2. When dynamically allocated memory is never freed
  3. When you free memory twice
  4. When a pointer is set to NULL
Show answer

Answer: B — When dynamically allocated memory is never freed

A memory leak happens when heap memory is allocated but never released, so it remains reserved and unusable until the program exits.

Question 5 of 19 (Medium)

How does calloc differ from malloc?

  1. It is faster
  2. It initializes the allocated memory to zero
  3. It allocates on the stack
  4. It never fails
Show answer

Answer: B — It initializes the allocated memory to zero

calloc allocates memory and initializes every byte to zero. malloc leaves the contents uninitialized (indeterminate).

Question 6 of 19 (Hard)

After 'free(p);', what best describes pointer p?

  1. It is automatically set to NULL
  2. It becomes a dangling pointer
  3. It still safely points to valid memory
  4. It is reused for the next malloc
Show answer

Answer: B — It becomes a dangling pointer

free releases the memory but does not change p. p now points to freed memory — a dangling pointer. Using it is undefined behavior; set it to NULL to be safe.

Question 7 of 19 (Hard)

What is the output of this code?

int *p = (int *)malloc(sizeof(int));
*p = 99;
free(p);
printf("%d", *p);
  1. 99
  2. 0
  3. NULL
  4. Undefined behavior
Show answer

Answer: D — Undefined behavior

After free(p), p is a dangling pointer. Reading through it is undefined behavior — the memory may have been reused or overwritten.

Full walkthrough with compilable code: read the detailed explanation.

Question 8 of 19 (Medium)

What does realloc(ptr, 0) do?

  1. Has no effect
  2. Is equivalent to free(ptr) and returns NULL or a unique pointer
  3. Allocates 0 bytes and crashes
  4. Doubles the existing allocation
Show answer

Answer: B — Is equivalent to free(ptr) and returns NULL or a unique pointer

realloc with size 0 frees the old block. The behavior (NULL or a unique freeable pointer) is implementation-defined, but not undefined. Treat the return as non-usable.

Question 9 of 19 (Hard)

What is a double-free error?

  1. Allocating memory twice
  2. Calling free on the same pointer twice
  3. Not freeing memory at all
  4. Using a freed pointer to allocate again
Show answer

Answer: B — Calling free on the same pointer twice

Calling free twice on the same pointer corrupts the heap allocator's internal state, causing undefined behavior — often a crash or security vulnerability.

Question 10 of 19 (Medium)

How many bytes does 'malloc(5 * sizeof(int))' allocate on a platform where sizeof(int) == 4?

  1. 5
  2. 10
  3. 20
  4. Depends on alignment
Show answer

Answer: C — 20

malloc requests 5 * 4 = 20 bytes to hold 5 int values.

Question 11 of 19 (Easy)

Where are local variables stored by default?

  1. Heap
  2. Stack
  3. Data segment
  4. Code segment
Show answer

Answer: B — Stack

Local (automatic) variables are allocated on the call stack. They are created when the function is entered and destroyed when it returns.

Question 12 of 19 (Hard)

What is the output of this code?

int *f(void) {
    int x = 42;
    return &x;
}
printf("%d", *f());
  1. 42
  2. 0
  3. Undefined behavior
  4. Compile error
Show answer

Answer: C — Undefined behavior

f returns the address of a local variable. Once f returns, x is destroyed and the address is dangling. Dereferencing it is undefined behavior.

Full walkthrough with compilable code: read the detailed explanation.

Question 13 of 19 (Medium)

What is stack overflow?

  1. Writing beyond the end of a heap allocation
  2. Exceeding the stack's capacity, usually through deep recursion
  3. Allocating too many global variables
  4. Using too much heap memory
Show answer

Answer: B — Exceeding the stack's capacity, usually through deep recursion

The call stack has a fixed size. Deep or infinite recursion keeps pushing frames until the stack is exhausted, causing a stack overflow (typically a segfault).

Question 14 of 19 (Hard)

What is an off-by-one error in array access?

  1. Starting an array at index 1 instead of 0
  2. Accessing index n when valid indices are 0 to n-1
  3. Forgetting to initialize array elements
  4. Passing an array to a function by value
Show answer

Answer: B — Accessing index n when valid indices are 0 to n-1

An off-by-one error accesses one element past the end (index n of an n-element array). It is undefined behavior and a common source of bugs and security vulnerabilities.

Question 15 of 19 (Medium)

What does memset(buf, 0, size) do?

  1. Allocates a zeroed buffer of 'size' bytes
  2. Sets every byte of 'buf' to 0 for 'size' bytes
  3. Copies 'size' zeros to buf from dev/zero
  4. Frees 'buf' and sets it to NULL
Show answer

Answer: B — Sets every byte of 'buf' to 0 for 'size' bytes

memset fills a region of memory with a specified byte value. memset(buf, 0, size) zeros out 'size' bytes starting at 'buf'.

Question 16 of 19 (Hard)

What is a buffer overflow?

  1. Allocating too much heap memory
  2. Writing data beyond the end of an allocated buffer
  3. Calling free too many times
  4. Reading an uninitialized variable
Show answer

Answer: B — Writing data beyond the end of an allocated buffer

A buffer overflow writes past the end of an array or allocated buffer, corrupting adjacent memory. It is a leading cause of security vulnerabilities.

Question 17 of 19 (Easy)

Which segment of memory stores global and static variables?

  1. Stack
  2. Heap
  3. Data segment
  4. Code segment
Show answer

Answer: C — Data segment

Global and static variables are stored in the data segment. Initialized globals go into .data; uninitialized (zero-initialized) globals go into .bss.

Question 18 of 19 (Medium)

What does memcpy(dest, src, n) do?

  1. Compares n bytes of dest and src
  2. Copies n bytes from src to dest (no overlap allowed)
  3. Moves n bytes handling overlapping regions
  4. Zeroes n bytes starting at dest
Show answer

Answer: B — Copies n bytes from src to dest (no overlap allowed)

memcpy copies exactly n bytes from src to dest. The regions must not overlap — use memmove for overlapping regions.

Question 19 of 19 (Hard)

What is the output of this code?

int a[3] = {1, 2, 3};
int *p = a;
p++;
free(p);
  1. No output — runs fine
  2. Crash or undefined behavior
  3. Frees a[1] cleanly
  4. Compile error
Show answer

Answer: B — Crash or undefined behavior

You can only pass to free() a pointer returned by malloc/calloc/realloc. p is now a+1 (pointing into the middle of a stack array), not a heap allocation. free(p) is undefined behavior.

Full walkthrough with compilable code: read the detailed explanation.

How Did You Score?

All 19 correct means this topic is interview-ready — try the harder timed rounds in the C Programming Quiz app. Missed a few? The guides below cover everything these questions test.

Keep Practising

More C Quizzes