Back to Practice C Programming

Memory Management - Practice MCQs for CCAT

50 Questions Section B: Programming C Programming

Memory Management Question Bank for C-CAT

Topic-wise Memory Management MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
Which function is used to allocate memory dynamically in C?
Aalloc()
Bmalloc()
Cnew()
Dcreate()
Show Answer & Explanation

Correct Answer: B - malloc()

malloc() (memory allocation) is used to allocate a block of memory dynamically from the heap.

Q2.
What is the difference between malloc() and calloc()?
ANo difference
Bcalloc() is deprecated
Cmalloc() is faster
Dcalloc() initializes memory to zero
Show Answer & Explanation

Correct Answer: D - calloc() initializes memory to zero

calloc() allocates memory and initializes all bytes to zero, while malloc() does not initialize the allocated memory.

Q3.
Which function is used to deallocate dynamically allocated memory?
Afree()
Bdelete()
Cremove()
Ddealloc()
Show Answer & Explanation

Correct Answer: A - free()

free() is used to deallocate memory that was previously allocated by malloc(), calloc(), or realloc().

Q4.
What is a memory leak?
AMemory overflow
BStack overflow
CBuffer overflow
DAllocated memory not freed
Show Answer & Explanation

Correct Answer: D - Allocated memory not freed

A memory leak occurs when dynamically allocated memory is not freed, causing the program to consume more and more memory over time.

Q5.
What does realloc() do?
AAllocates new memory
BResizes allocated memory
CFrees memory
DCopies memory
Show Answer & Explanation

Correct Answer: B - Resizes allocated memory

realloc() changes the size of previously allocated memory block, either expanding or shrinking it.

Q6.
Which header file contains malloc(), calloc(), and free()?
A<stdlib.h>
B<stdio.h>
C<memory.h>
D<alloc.h>
Show Answer & Explanation

Correct Answer: A - <stdlib.h>

Dynamic memory allocation functions are declared in <stdlib.h> header file.

Q7.
What is the return type of malloc()?
Avoid*
Bchar*
Cint*
Dsize_t
Show Answer & Explanation

Correct Answer: A - void*

malloc() returns void* (generic pointer) which can be cast to any pointer type.

Q8.
What happens if malloc() fails to allocate memory?
AReturns 0
BReturns NULL
CThrows exception
DProgram crashes
Show Answer & Explanation

Correct Answer: B - Returns NULL

If malloc() fails to allocate memory, it returns NULL. Always check for NULL before using the pointer.

Q9.
What is a dangling pointer?
APointer to NULL
BPointer to stack
CPointer to heap
DPointer to freed memory
Show Answer & Explanation

Correct Answer: D - Pointer to freed memory

A dangling pointer points to memory that has been freed or deallocated. Using it leads to undefined behavior.

Q10.
Which memory region is used for dynamic memory allocation?
AStack
BCode segment
CData segment
DHeap
Show Answer & Explanation

Correct Answer: D - Heap

Dynamic memory is allocated from the heap. Stack is used for local variables and function calls.

Q11.

What is the output?

int *p = (int*)malloc(sizeof(int));
*p = 100;
free(p);
printf("%d", *p);
A100
B0
CUndefined behavior
DCompilation error
Show Answer & Explanation

Correct Answer: C - Undefined behavior

After free(), accessing the memory through p is undefined behavior. The pointer becomes dangling.

Q12.
How many bytes does calloc(5, sizeof(int)) allocate? (Assuming 4-byte int)
A5 bytes
B4 bytes
C20 bytes
D9 bytes
Show Answer & Explanation

Correct Answer: C - 20 bytes

calloc(n, size) allocates n * size bytes. So 5 * 4 = 20 bytes are allocated.

Q13.
What is the difference between stack and heap memory?
ANo difference
BStack is faster, heap is dynamic
CHeap is faster
DStack is larger
Show Answer & Explanation

Correct Answer: B - Stack is faster, heap is dynamic

Stack memory is faster but limited in size and managed automatically. Heap is slower but dynamic and larger.

Q14.
What happens when you call free() on a NULL pointer?
AProgram crashes
BNothing happens
CMemory corruption
DCompilation error
Show Answer & Explanation

Correct Answer: B - Nothing happens

Calling free(NULL) is safe and does nothing. It is defined behavior in C standard.

Q15.
What is the correct way to allocate memory for an array of 10 integers?
Amalloc(10)
Bmalloc(10 * int)
Cmalloc(sizeof(int))
Dmalloc(10 * sizeof(int))
Show Answer & Explanation

Correct Answer: D - malloc(10 * sizeof(int))

malloc(10 * sizeof(int)) allocates enough bytes for 10 integers.

Q16.
What is double free error?
ACalling free() twice on same pointer
BAllocating memory twice
CFreeing NULL pointer
DMemory overflow
Show Answer & Explanation

Correct Answer: A - Calling free() twice on same pointer

Double free occurs when free() is called twice on the same memory address, causing undefined behavior.

Q17.
What is the advantage of calloc() over malloc()?
AFaster allocation
BUses less memory
CInitializes memory to zero
DNo type casting needed
Show Answer & Explanation

Correct Answer: C - Initializes memory to zero

calloc() initializes all allocated bytes to zero, while malloc() leaves memory uninitialized.

Q18.
What does realloc(ptr, 0) do?
AFrees the memory
BReturns NULL
CNo change
DCompilation error
Show Answer & Explanation

Correct Answer: A - Frees the memory

realloc(ptr, 0) is equivalent to free(ptr) and frees the memory block. The behavior may vary by implementation.

Q19.
What is wild pointer?
APointer to NULL
BUninitialized pointer
CPointer to heap
DConstant pointer
Show Answer & Explanation

Correct Answer: B - Uninitialized pointer

A wild pointer is an uninitialized pointer that points to some arbitrary memory location.

Q20.
What is the best practice after calling free()?
ASet pointer to NULL
BDo nothing
CCall malloc again
DPrint the pointer
Show Answer & Explanation

Correct Answer: A - Set pointer to NULL

After free(), set the pointer to NULL to avoid using dangling pointer accidentally.

Q21.
Which function is used to dynamically allocate memory in C?
Aalloc()
Bnew()
Cmalloc()
Dcreate()
Show Answer & Explanation

Correct Answer: C - malloc()

malloc() (memory allocation) is the standard C function for dynamic memory allocation. It allocates a specified number of bytes and returns a void pointer.

Q22.
What does malloc() return if it fails to allocate memory?
A0
B-1
CNULL
DAn error code
Show Answer & Explanation

Correct Answer: C - NULL

malloc() returns NULL if it cannot allocate the requested memory. Always check the return value before using the allocated memory.

Q23.
What is the difference between malloc() and calloc()?
ANo difference
Bcalloc() initializes memory to zero, malloc() does not
Cmalloc() is faster
Dcalloc() cannot allocate arrays
Show Answer & Explanation

Correct Answer: B - calloc() initializes memory to zero, malloc() does not

malloc() allocates uninitialized memory, while calloc() allocates memory and initializes all bytes to zero. calloc() also takes the number and size of elements as separate parameters.

Q24.

What is the output?

int *p = (int*)malloc(5 * sizeof(int));
for(int i = 0; i < 5; i++)
    p[i] = (i + 1) * 10;
printf("%d", p[3]);
free(p);
A30
BUndefined
C50
D40
Show Answer & Explanation

Correct Answer: D - 40

The loop sets p[0]=10, p[1]=20, p[2]=30, p[3]=40, p[4]=50. p[3] is 40.

Q25.
What does free() do?
AClears the contents of memory
BFrees all program memory
CSets the pointer to NULL
DDeallocates previously allocated dynamic memory
Show Answer & Explanation

Correct Answer: D - Deallocates previously allocated dynamic memory

free() deallocates memory that was previously allocated by malloc(), calloc(), or realloc(). The memory is returned to the heap for reuse.

Q26.
What happens if you call free() on a pointer that was not dynamically allocated?
AUndefined behavior
BThe pointer is set to NULL
CNothing happens
DA compile error
Show Answer & Explanation

Correct Answer: A - Undefined behavior

Calling free() on a pointer not returned by malloc/calloc/realloc causes undefined behavior, which may result in crashes or memory corruption.

Q27.
What is a memory leak?
ADynamically allocated memory that is never freed
BMemory being accessed by two pointers
CStack overflow
DAccessing freed memory
Show Answer & Explanation

Correct Answer: A - Dynamically allocated memory that is never freed

A memory leak occurs when dynamically allocated memory is never freed, making it unavailable for the rest of the program. Over time, this can exhaust available memory.

Q28.

What does realloc() do?

int *p = malloc(3 * sizeof(int));
p = realloc(p, 5 * sizeof(int));
AFrees and reallocates new memory
BOnly shrinks memory
CCreates a copy of the memory
DChanges the size of previously allocated memory, preserving existing data
Show Answer & Explanation

Correct Answer: D - Changes the size of previously allocated memory, preserving existing data

realloc() resizes previously allocated memory. If the block is expanded, existing data is preserved and the additional memory is uninitialized. It may move the block to a new location.

Q29.

What is wrong with this code?

int *p = malloc(sizeof(int));
*p = 42;
free(p);
printf("%d", *p);
ANothing is wrong
Bprintf format is wrong
Cmalloc should not be used for single integers
DAccessing memory after it has been freed (use-after-free)
Show Answer & Explanation

Correct Answer: D - Accessing memory after it has been freed (use-after-free)

After free(p), the memory pointed to by p is deallocated. Accessing *p after free is a use-after-free bug, which is undefined behavior.

Q30.
What is a dangling pointer?
AA pointer that is not initialized
BA pointer that points to memory that has been freed
CA pointer to a function
DA pointer set to NULL
Show Answer & Explanation

Correct Answer: B - A pointer that points to memory that has been freed

A dangling pointer is a pointer that refers to memory that has been deallocated (freed). Using it leads to undefined behavior.

Q31.

What is the correct way to avoid a dangling pointer after free()?

int *p = malloc(sizeof(int));
free(p);
ADo nothing
BSet p = NULL after free
CCall free twice
DUse realloc instead
Show Answer & Explanation

Correct Answer: B - Set p = NULL after free

Setting the pointer to NULL after freeing prevents accidental use-after-free bugs. Dereferencing NULL causes a predictable crash rather than subtle corruption.

Q32.
What does calloc(5, sizeof(int)) allocate?
A5 bytes
B5 integers initialized to zero
C5 integers with garbage values
DA single integer with value 5
Show Answer & Explanation

Correct Answer: B - 5 integers initialized to zero

calloc(5, sizeof(int)) allocates memory for 5 integers (5 * sizeof(int) bytes) and initializes all bits to zero.

Q33.

What is the output?

int *p = calloc(3, sizeof(int));
printf("%d %d %d", p[0], p[1], p[2]);
free(p);
AGarbage values
BUndefined
CCompilation error
D0 0 0
Show Answer & Explanation

Correct Answer: D - 0 0 0

calloc() initializes all allocated memory to zero. So p[0], p[1], and p[2] are all 0.

Q34.

What happens when realloc() is called with size 0?

int *p = malloc(10 * sizeof(int));
p = realloc(p, 0);
AMemory is expanded
BNothing happens
CMemory is freed (implementation-defined)
DCompilation error
Show Answer & Explanation

Correct Answer: C - Memory is freed (implementation-defined)

Calling realloc(p, 0) is implementation-defined. In many implementations it acts like free(p) and returns NULL, but this behavior is not guaranteed by the C standard.

Q35.

What is wrong with this code?

int *p = malloc(5 * sizeof(int));
p = malloc(10 * sizeof(int));
free(p);
ANothing wrong
BThe first allocation is leaked because p is reassigned
CDouble free error
Dmalloc cannot be called twice
Show Answer & Explanation

Correct Answer: B - The first allocation is leaked because p is reassigned

The first malloc allocates memory and p points to it. When p is reassigned to the second malloc, the first allocation becomes unreachable, causing a memory leak.

Q36.
Which memory region stores dynamically allocated memory?
AStack
BData segment
CHeap
DCode segment
Show Answer & Explanation

Correct Answer: C - Heap

Dynamic memory allocated by malloc/calloc/realloc comes from the heap. The stack is used for local variables and function calls.

Q37.

What is the output?

int *arr = malloc(3 * sizeof(int));
arr[0] = 10; arr[1] = 20; arr[2] = 30;
arr = realloc(arr, 5 * sizeof(int));
arr[3] = 40; arr[4] = 50;
printf("%d %d", arr[1], arr[4]);
free(arr);
A10 40
B20 50
CUndefined
DCompilation error
Show Answer & Explanation

Correct Answer: B - 20 50

realloc() preserves existing data (10, 20, 30) and extends the allocation. arr[1] is still 20, and arr[4] is set to 50.

Q38.
What is a double free error?
ACalling free() on the same pointer twice
BAllocating memory twice
CFreeing two different pointers
DUsing free() inside a loop
Show Answer & Explanation

Correct Answer: A - Calling free() on the same pointer twice

A double free occurs when free() is called on the same memory address twice. This causes undefined behavior and can lead to crashes or security vulnerabilities.

Q39.

What does this code demonstrate?

void func() {
    int *p = malloc(100 * sizeof(int));
    // function returns without calling free(p)
}
ANormal behavior
BStack overflow
CA memory leak
DA compilation warning
Show Answer & Explanation

Correct Answer: C - A memory leak

The function allocates memory but never frees it. Since p is a local variable, it is destroyed when the function returns, making the allocated memory unreachable — a memory leak.

Q40.

What is wrong with this realloc usage?

int *p = malloc(5 * sizeof(int));
p = realloc(p, 10 * sizeof(int));
AIf realloc fails, the original memory is leaked
BNothing is wrong
Crealloc cannot expand memory
Dsizeof is not needed
Show Answer & Explanation

Correct Answer: A - If realloc fails, the original memory is leaked

If realloc() fails, it returns NULL but does not free the original memory. Assigning directly to p loses the original pointer, causing a leak. Use a temporary pointer instead.

Q41.
What is the correct way to use realloc() safely?
Ap = realloc(p, newsize);
Brealloc(&p, newsize);
Cint *tmp = realloc(p, newsize); if(tmp) p = tmp;
Dfree(p); p = malloc(newsize);
Show Answer & Explanation

Correct Answer: C - int *tmp = realloc(p, newsize); if(tmp) p = tmp;

Using a temporary pointer preserves the original pointer if realloc fails (returns NULL). Only update the original pointer after confirming success.

Q42.
What is the difference between stack and heap memory?
AStack is larger than heap
BHeap is faster than stack
CStack memory is managed automatically; heap memory must be managed manually
DThere is no difference
Show Answer & Explanation

Correct Answer: C - Stack memory is managed automatically; heap memory must be managed manually

Stack memory is allocated and deallocated automatically with function calls. Heap memory is allocated using malloc/calloc and must be freed explicitly by the programmer.

Q43.

What does this code output?

char *s = malloc(6);
strcpy(s, "Hello");
printf("%s", s);
free(s);
AGarbage
BCompilation error
CUndefined
DHello
Show Answer & Explanation

Correct Answer: D - Hello

malloc(6) allocates 6 bytes. strcpy copies "Hello" (5 chars + null) into the buffer. printf outputs "Hello". Memory is properly freed.

Q44.
What is a wild pointer?
AA pointer that has not been initialized and contains a random address
BA pointer initialized to NULL
CA pointer to a function
DA pointer to a constant
Show Answer & Explanation

Correct Answer: A - A pointer that has not been initialized and contains a random address

A wild pointer is an uninitialized pointer that contains a random or garbage address. Dereferencing it causes undefined behavior.

Q45.

What is the problem with this code?

int *p;
*p = 10;
printf("%d", *p);
ANo problem
Bp is a wild pointer - it was never assigned a valid address
Cprintf format is wrong
D10 cannot be assigned to a pointer
Show Answer & Explanation

Correct Answer: B - p is a wild pointer - it was never assigned a valid address

p is declared but never initialized or allocated. Writing *p = 10 dereferences a wild pointer, which is undefined behavior and likely causes a crash.

Q46.
How many bytes does malloc(0) allocate?
A0 bytes exactly
B1 byte
CCompilation error
DImplementation-defined: may return NULL or a unique pointer
Show Answer & Explanation

Correct Answer: D - Implementation-defined: may return NULL or a unique pointer

malloc(0) has implementation-defined behavior. It may return NULL or a unique pointer that can be passed to free(). The result should not be dereferenced.

Q47.

What does this code do?

int **matrix = malloc(3 * sizeof(int*));
for(int i = 0; i < 3; i++)
    matrix[i] = malloc(4 * sizeof(int));
AAllocates a 3x4 integer matrix dynamically
BAllocates a 4x3 integer matrix
CCreates a linked list
DCompilation error
Show Answer & Explanation

Correct Answer: A - Allocates a 3x4 integer matrix dynamically

This allocates a dynamically-sized 2D array (3 rows, 4 columns). First, an array of 3 int pointers is allocated, then each pointer is allocated an array of 4 ints.

Q48.

What is the correct way to free the dynamically allocated 2D array?

int **matrix = malloc(3 * sizeof(int*));
for(int i = 0; i < 3; i++)
    matrix[i] = malloc(4 * sizeof(int));
AFree each row first, then free matrix
Bfree(*matrix);
Cfree(matrix);
Dfree(matrix[0]); free(matrix);
Show Answer & Explanation

Correct Answer: A - Free each row first, then free matrix

You must free each row (matrix[i]) first, then free the array of pointers (matrix). Freeing in reverse order of allocation prevents memory leaks.

Q49.

What does the following code print?

int *a = malloc(sizeof(int));
int *b = a;
*a = 100;
printf("%d", *b);
free(a);
A0
BGarbage value
C100
DCompilation error
Show Answer & Explanation

Correct Answer: C - 100

Both a and b point to the same dynamically allocated memory. Setting *a = 100 changes the value at that location, so *b also reads 100.

Q50.
What is memory fragmentation?
ARunning out of stack space
BMemory being corrupted
CMemory being split into small unused blocks due to repeated allocations and frees
DHaving too many pointers
Show Answer & Explanation

Correct Answer: C - Memory being split into small unused blocks due to repeated allocations and frees

Memory fragmentation occurs when free memory is broken into small, non-contiguous blocks after many allocations and deallocations, making it difficult to satisfy large allocation requests.

Showing 1-10 of 50 questions