C Programming

Memory Management — Practice MCQs for CCAT

20 Questions Section B: Programming C Programming

Practice 20 Memory Management multiple-choice questions designed for CDAC CCAT exam preparation. Click "Show Answer" to reveal the correct option with detailed explanation.

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() initializes memory to zero
Cmalloc() is faster
Dcalloc() is deprecated
Show Answer & Explanation

Correct Answer: B — 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?
Adelete()
Bfree()
Cremove()
Ddealloc()
Show Answer & Explanation

Correct Answer: B — free()

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

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

Correct Answer: B — 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
BFrees memory
CResizes allocated memory
DCopies memory
Show Answer & Explanation

Correct Answer: C — 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<stdio.h>
B<stdlib.h>
C<memory.h>
D<alloc.h>
Show Answer & Explanation

Correct Answer: B — <stdlib.h>

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

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

Correct Answer: C — 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 freed memory
CPointer to heap
DPointer to stack
Show Answer & Explanation

Correct Answer: B — 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
BHeap
CData segment
DCode segment
Show Answer & Explanation

Correct Answer: B — 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
BMemory corruption
CNothing happens
DCompilation error
Show Answer & Explanation

Correct Answer: C — 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(10 * sizeof(int))
Dmalloc(sizeof(int))
Show Answer & Explanation

Correct Answer: C — malloc(10 * sizeof(int))

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

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

Correct Answer: B — 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
BInitializes memory to zero
CUses less memory
DNo type casting needed
Show Answer & Explanation

Correct Answer: B — Initializes memory to zero

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

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

Correct Answer: B — 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()?
ADo nothing
BSet pointer to NULL
CCall malloc again
DPrint the pointer
Show Answer & Explanation

Correct Answer: B — Set pointer to NULL

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