C- Memory Allocation Questions and Answers

Practice Mode
Showing 10 of 28 questions
Q11
What does malloc() return if it fails to allocate memory?
  • A NULL
  • B 0
  • C -1
  • D Garbage value
Answer: Option A
Explanation: malloc() returns NULL when it cannot allocate the requested amount of memory.
Q12
How is calloc() different from malloc() in terms of parameters?
  • A Takes number of elements and element size
  • B Takes only total size
  • C Takes pointer to existing memory
  • D Takes alignment parameter
Answer: Option A
Explanation: calloc() takes number of elements and element size, while malloc() takes total bytes.
Q13
What is dangling pointer?
  • A Pointer to freed memory
  • B Uninitialized pointer
  • C Pointer to stack memory
  • D Pointer to constant data
Answer: Option A
Explanation: Dangling pointer points to memory that has already been freed.
Q14
Which memory allocation function should be used for arrays?
  • A calloc()
  • B malloc()
  • C realloc()
  • D free()
Answer: Option A
Explanation: calloc() is often preferred for arrays as it initializes memory to zero.
Q15
What is the return type of malloc()?
  • A void*
  • B int*
  • C char*
  • D float*
Answer: Option A
Explanation: malloc() returns void* which can be cast to any pointer type.
Q16
What happens if you free the same memory twice?
  • A Undefined behavior
  • B Memory leak
  • C Safe operation
  • D Automatic reallocation
Answer: Option A
Explanation: Double freeing causes undefined behavior, often program crash.
Q17
Which function allocates memory and initializes it to zero?
  • A calloc()
  • B malloc()
  • C realloc()
  • D alloc()
Answer: Option A
Explanation: calloc() both allocates and initializes memory to zero.
Q18
What is the main advantage of dynamic memory allocation?
  • A Memory size determined at runtime
  • B Faster execution
  • C Automatic memory management
  • D No need to free memory
Answer: Option A
Explanation: Dynamic allocation allows memory size to be determined at runtime.
Q19
Where is dynamically allocated memory stored?
  • A Heap
  • B Stack
  • C Data segment
  • D Code segment
Answer: Option A
Explanation: Dynamic memory is allocated from the heap segment.
Q20
What should you always check after calling malloc()?
  • A If return value is NULL
  • B If memory is initialized
  • C If pointer is aligned
  • D If enough stack space
Answer: Option A
Explanation: Always check if malloc() returned NULL indicating allocation failure.
Questions and Answers for Competitive Exams Various Entrance Test