Practice 20 Memory Management multiple-choice questions designed for CDAC CCAT exam preparation. Click "Show Answer" to reveal the correct option with detailed explanation.
Show Answer & Explanation
Correct Answer: B — malloc()
malloc() (memory allocation) is used to allocate a block of memory dynamically from the heap.
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.
Show Answer & Explanation
Correct Answer: B — free()
free() is used to deallocate memory that was previously allocated by malloc(), calloc(), or realloc().
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.
Show Answer & Explanation
Correct Answer: C — Resizes allocated memory
realloc() changes the size of previously allocated memory block, either expanding or shrinking it.
Show Answer & Explanation
Correct Answer: B — <stdlib.h>
Dynamic memory allocation functions are declared in <stdlib.h> header file.
Show Answer & Explanation
Correct Answer: C — void*
malloc() returns void* (generic pointer) which can be cast to any pointer type.
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.
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.
Show Answer & Explanation
Correct Answer: B — Heap
Dynamic memory is allocated from the heap. Stack is used for local variables and function calls.
What is the output?
`int *p = (int*)malloc(sizeof(int));`
`*p = 100;`
`free(p);`
`printf("%d", *p);`Show Answer & Explanation
Correct Answer: C — Undefined behavior
After free(), accessing the memory through p is undefined behavior. The pointer becomes dangling.
Show Answer & Explanation
Correct Answer: C — 20 bytes
calloc(n, size) allocates n * size bytes. So 5 * 4 = 20 bytes are allocated.
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.
Show Answer & Explanation
Correct Answer: C — Nothing happens
Calling free(NULL) is safe and does nothing. It is defined behavior in C standard.
Show Answer & Explanation
Correct Answer: C — malloc(10 * sizeof(int))
malloc(10 * sizeof(int)) allocates enough bytes for 10 integers.
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.
Show Answer & Explanation
Correct Answer: B — Initializes memory to zero
calloc() initializes all allocated bytes to zero, while malloc() leaves memory uninitialized.
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.
Show Answer & Explanation
Correct Answer: B — Uninitialized pointer
A wild pointer is an uninitialized pointer that points to some arbitrary memory location.
Show Answer & Explanation
Correct Answer: B — Set pointer to NULL
After free(), set the pointer to NULL to avoid using dangling pointer accidentally.