LearnFrenzy
Sign in Take a free test

Memory Allocation · Exercise 1 of 4

Memory Allocation Memory Allocation MCQ

  • 12 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twelve multiple-choice questions on malloc, calloc, realloc and free, stack vs heap, memmove, alignment and C23. Pick an option to check your answer instantly.

Question 1 Easy

What does malloc(n) return?

A A void * to n uninitialised bytes, or NULL on failure
B A pointer to n zero bytes
C An int with the number of bytes allocated
D A pointer that never needs to be checked
Question page & discussion
Question 2 Easy

Which statement about calloc(count, size) is true?

A It is the same as malloc(count * size)
B It allocates on the stack
C It zero-fills the memory and fails safely if count × size would overflow
D It cannot be freed with free()
Question page & discussion
Question 3 Easy

What happens when you call free(NULL)?

A The program crashes
B It frees the last allocated block
C Nothing – it is defined to do nothing
D It is undefined behaviour
Question page & discussion
Question 4 Medium

If realloc(p, bigger) cannot get the memory, what happens?

A It frees p and returns NULL
B It returns p unchanged
C It returns NULL and leaves the original block untouched
D The program terminates
Question page & discussion
Question 5 Easy

Which situation is a memory leak?

A Reading an uninitialised variable
B Freeing the same block twice
C Using more than 1 GB of memory
D Allocated memory that can no longer be reached and is never freed
Question page & discussion
Question 6 Easy

In void f(void) { int x; int *p = malloc(sizeof *p); }, where are x, p and the allocated int?

A All on the heap
B x on the stack; p and the int on the heap
C All on the stack
D x and p on the stack; the allocated int on the heap
Question page & discussion
Question 7 Medium

Why is p = malloc(n * sizeof *p); preferred over p = malloc(n * sizeof(int));?

A It is faster
B It allocates one extra element
C The size follows p's type automatically, so it stays correct if the type of p changes
D sizeof(int) is not allowed in malloc
Question page & discussion
Question 8 Medium

Is int *p = (int *) malloc(n * sizeof *p); necessary in C?

A Yes, C requires the cast
B No – void * converts to any object pointer automatically; the cast is only needed in C++
C Yes, otherwise malloc returns an int
D No, because malloc returns int *
Question page & discussion
Question 9 Medium

What happens if the source and destination of memcpy() overlap?

A It works correctly
B It copies backwards automatically
C Compile error
D Undefined behaviour – use memmove() instead
Question page & discussion
Question 10 Hard

In C23, what is realloc(p, 0)?

A The same as free(p)
B It returns p unchanged
C It allocates 1 byte
D Undefined behaviour – call free(p) instead
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links