Sample questions
Question 1Easy
Memory is obtained with malloc inside a function, and the pointer is returned. What is true about that memory?
- It stays allocated after the function returns until free is called
- It becomes read-only after the function returns
- It is freed automatically when the function returns
- It is freed when the pointer variable goes out of scope
Answer: A. It stays allocated after the function returns until free is called
Explanation
Heap blocks are not tied to any scope; they live until explicitly freed (or the program ends).
Step-by-step solution
- Memory allocated with
malloc is on the heap. - Heap memory remains allocated even after the function returns.
- The memory must be freed manually using
free(). - The pointer can be used outside the function to access the memory.
- It stays allocated after the function returns until free is called.
Why the other options are wrong
- B. Heap memory is not made read-only; it stays writable until it is freed.
- C. Malloc memory is not freed automatically at return; it needs an explicit free call.
- D. The pointer variable going out of scope does not free the memory, so that causes a leak.
Tip: Always free malloced memory to prevent memory leaks.
Question 2Easy
What is the output?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *p = malloc(6);
if (!p) return 1;
memset(p, 'x', 5);
p[5] = '\0';
memset(p + 1, '-', 2);
printf("%s\n", p);
free(p);
return 0;
}
x-x-xx--xxx--xxxx--xx
Answer: D. x--xx
Explanation
The first memset fills 5 bytes with x; the second overwrites bytes 1 and 2 with -, giving "x--xx".
Step-by-step solution
- p holds xxxxx.
- memset(p+1, '-', 2) changes positions 1 and 2.
- Result: x--xx.
Why the other options are wrong
- A. The dashes are consecutive.
- B. There are five characters.
- C. p+1 starts after the first x.
Tip: p+1 starts at index 1.
Question 3Easy
What is the output?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *s = malloc(4);
if (!s) return 1;
strcpy(s, "abc");
char *t = realloc(s, 7);
if (!t) { free(s); return 1; }
strcat(t, "def");
printf("%s %zu\n", t, strlen(t));
free(t);
return 0;
}
- abcdef 7
- abcdef 6
- abc 3
- def 3
Answer: B. abcdef 6
Explanation
realloc keeps "abc" and grows the block to 7 bytes, enough for "abcdef" plus the terminator; strlen is 6.
Step-by-step solution
- malloc(4) gives 4 bytes, and strcpy writes abc with its terminator into them.
- Realloc to 7 bytes keeps abc, then strcat adds def, so t holds abcdef with a terminator in 7 bytes.
- strlen(t) counts 6 characters, so the output is abcdef 6, the marked option B.
Why the other options are wrong
- A. Strlen leaves out the terminator, so the length is 6, not 7.
- C. Strcat added def, so the string is abcdef, not abc.
- D. T holds abc followed by def, so the text is abcdef, not def.
Tip: Realloc keeps the old bytes; strcat needs room for the joined text plus the terminator.
Question 4Easy
What is a memory leak in a C program?
- Heap memory that is no longer reachable and was never freed
- Writing past the end of an array
- Reading memory after it has been freed
- A local variable going out of scope
Answer: A. Heap memory that is no longer reachable and was never freed
Explanation
Leaked blocks stay allocated until the process exits because no pointer to them remains to pass to free.
Step-by-step solution
- Memory leak is when memory is allocated but not released.
- Heap memory that is no longer reachable and was never freed stays allocated.
- This happens because no pointer exists to call free on it.
- Leaked memory is lost until the program ends.
- Final answer: Heap memory that is no longer reachable and was never freed.
Why the other options are wrong
- B. Writing past the end of an array is a buffer overflow, not a leak; no allocated memory is lost.
- C. Reading freed memory is a use-after-free bug, not a leak; the problem is access, not lost memory.
- D. A local variable going out of scope is automatic storage ending, not heap memory that was never freed.
Question 5Medium
What is the output?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int **m = malloc(3 * sizeof *m);
if (!m) return 1;
for (int i = 0; i < 3; i++) {
m[i] = malloc((i + 1) * sizeof **m);
if (!m[i]) return 1;
for (int j = 0; j <= i; j++)
m[i][j] = i * j;
}
printf("%d %d\n", m[2][2], m[1][0] + m[2][1]);
for (int i = 0; i < 3; i++)
free(m[i]);
free(m);
return 0;
}
- 4 3
- 2 2
- 4 2
- 6 2
Answer: C. 4 2
Explanation
Row i has i + 1 elements holding i * j: m[2][2] = 4, m[1][0] = 0 and m[2][1] = 2.
Step-by-step solution
- M is an array of 3 row pointers; row i has i + 1 ints.
- Each element is m[i][j] = i × j.
- m[2][2] = 2 × 2 = 4, m[1][0] = 0, m[2][1] = 2 × 1 = 2.
- The second value is 0 + 2 = 2, so the output is 4 2: option C.
Why the other options are wrong
- A. m[1][0] + m[2][1] is 0 + 2 = 2, not 3.
- B. m[2][2] is 4, not 2.
- D. m[2][2] is 4, not 6; the second value is 2, so only the first value is wrong.
Tip: A pointer-to-pointer array can have rows of different lengths; allocate and free each row.
Question 6Medium
What does free(NULL); do?
- It frees the most recently allocated block
- Nothing; freeing a null pointer is a valid no-op
- It causes undefined behaviour
- It returns -1 to signal an error
Answer: B. Nothing; freeing a null pointer is a valid no-op
Explanation
The standard says that if the argument is a null pointer, free performs no action.
Step-by-step solution
- free(NULL); passes a null pointer to the function.
- The C standard states that free does nothing with a null pointer.
- This makes freeing a null pointer a valid no-op.
- So, free(NULL); has no effect.
- Nothing; freeing a null pointer is a valid no-op.
Why the other options are wrong
- A. Free(NULL) does not free any block; the standard says a null pointer is ignored.
- C. Free(NULL) is safe and does nothing; the standard defines it as a no-op, so no undefined behaviour occurs.
- D. Free returns nothing, so it cannot return -1 to signal an error.
Tip: Remember: free(NULL) is safe and does nothing.
Question 7Medium
A function builds an array whose size is known only at run time and must return it to the caller. Which approach is correct?
- Return a pointer to a local variable-length array
- Declare a static array whose size is the run-time value
- Allocate it with malloc and document that the caller must free it
- Return a local array declared inside the function
Answer: C. Allocate it with malloc and document that the caller must free it
Explanation
Local arrays (including VLAs) die when the function returns, and static arrays need a constant size. Heap memory outlives the call.
Step-by-step solution
- Malloc allocates memory on the heap.
- Heap memory is not destroyed when the function ends.
- Caller must free the allocated memory to avoid leaks.
- This ensures the array survives beyond the function call.
- Final answer: Allocate it with malloc and document that the caller must free it.
Why the other options are wrong
- A. Returning a pointer to a local variable-length array gives a dangling pointer once the function returns.
- B. A static array has a fixed size set at compile time, so it cannot take a size known only at run time.
- D. A local array is destroyed when the function returns, so the caller would get an invalid pointer.
Tip: Use malloc for run-time sized arrays and always free them.
Question 8Medium
On a 64-bit system this code allocates a table of row pointers, but later writes corrupt the heap. Which fix is correct?
int **m = malloc(rows * sizeof(int));
for (int i = 0; i < rows; i++)
m[i] = malloc(cols * sizeof(int));
- Cast the result of malloc to (int **)
- Use calloc instead of malloc for the rows
- Allocate cols * sizeof(int *) in the loop
- Allocate rows * sizeof(int *) (or rows * sizeof *m) for the first call
Answer: D. Allocate rows * sizeof(int *) (or rows * sizeof *m) for the first call
Explanation
m holds pointers (8 bytes each) but only 4 bytes per element were requested, so m[i] writes past the block. sizeof *m always matches the element type.
Step-by-step solution
- M is an array of row pointers, so each element is an int * which is 8 bytes on a 64-bit system.
- The first malloc asks for rows × sizeof(int), which is only 4 bytes per row.
- The table is too small, so the writes m[i] go past the block and corrupt the heap.
- Allocate rows × sizeof(int *) or rows × sizeof *m, so the correct answer is option D.
Why the other options are wrong
- A. A cast does not change how many bytes malloc allocates.
- B. Calloc zeroes the memory but still sizes the table as rows × sizeof(int), which is too small.
- C. The row allocations are fine, but the pointer table itself is still too small.
Tip: Size with sizeof *p so the size always matches the type the pointer points to.
Question 9Hard
Why can calloc(n, size) be safer than malloc(n * size) when n is very large?
- malloc cannot allocate more than 64 KB
- calloc automatically frees memory when it is not used
- calloc fails if n * size overflows, while the product passed to malloc can wrap to a small value
- calloc allocates on the stack, which has no limit
Answer: C. calloc fails if n * size overflows, while the product passed to malloc can wrap to a small value
Explanation
An overflowing size_t product wraps around, so malloc may succeed with a tiny buffer and later writes overflow it. Common calloc implementations check the multiplication and return NULL instead.
Step-by-step solution
- calloc takes count and size separately.
- It can check for overflow in n * size.
- With malloc, the product can wrap to a small value.
Why the other options are wrong
- A. malloc has no 64 KB limit.
- B. calloc does not free automatically.
- D. calloc uses the heap.
Tip: Prefer calloc for arrays.
Question 10Hard
What is the output?
#include <stdio.h>
#include <stdlib.h>
struct N { int v; struct N *next; };
int main(void) {
struct N *head = NULL;
for (int i = 1; i <= 4; i++) {
struct N *n = malloc(sizeof *n);
if (!n) return 1;
n->v = i;
n->next = head;
head = n;
}
struct N *prev = NULL;
while (head) {
struct N *nx = head->next;
head->next = prev;
prev = head;
head = nx;
}
head = prev;
for (struct N *p = head; p; p = p->next)
printf("%d", p->v);
printf("\n");
while (head) {
struct N *nx = head->next;
free(head);
head = nx;
}
return 0;
}
- 4
- 1234
- 1
- 4321
Answer: B. 1234
Explanation
Inserting at the head builds 4 -> 3 -> 2 -> 1. The second loop reverses the links, giving 1 -> 2 -> 3 -> 4.
Step-by-step solution
- Each new node goes at the front, so the list becomes 4, 3, 2, 1.
- The reversal loop points each next pointer backward, one node at a time.
- After the loop head is node 1, and the list reads 1, 2, 3, 4.
- Printing the list gives 1234, so the correct answer is option B.
Why the other options are wrong
- A. Only the first value of the list is 4 before reversal; the loop prints every node.
- C. Printing stops only when the list ends, so all four values print.
- D. 4321 is the list before reversal, and the loop reverses it.
Tip: To reverse a linked list, save next, point it back, then move forward; the old tail becomes the head.
Practise all 27 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →