Memory Allocation and Deallocation MCQs

By: Prof. Dr. Fazal Rehman Shamil | Last updated: September 20, 2024

What is the primary purpose of memory allocation in a program?
A) To improve processing speed
B) To reserve space for data and instructions
C) To enhance security features
D) To manage I/O operations
Answer: B) To reserve space for data and instructions

Which of the following is a dynamic memory allocation function in C++?
A) malloc()
B) calloc()
C) new
D) free
Answer: C) new

What is the term for memory that is allocated at compile time?
A) Static memory
B) Dynamic memory
C) Stack memory
D) Heap memory
Answer: A) Static memory

Which of the following functions is used to deallocate memory in C?
A) delete
B) release()
C) free()
D) dispose()
Answer: C) free()

What is a memory leak?
A) Memory that is allocated but not used
B) Memory that cannot be freed
C) Memory that is incorrectly accessed
D) Memory that is lost due to improper deallocation
Answer: D) Memory that is lost due to improper deallocation

Which memory allocation method allows a program to request more memory at runtime?
A) Static allocation
B) Dynamic allocation
C) Stack allocation
D) Pre-allocated memory
Answer: B) Dynamic allocation

What is the role of the heap in memory allocation?
A) To store local variables
B) To manage I/O operations
C) To provide a large pool of memory for dynamic allocation
D) To execute program instructions
Answer: C) To provide a large pool of memory for dynamic allocation

What happens when a program tries to access memory that has already been deallocated?
A) The program executes successfully
B) The program experiences a segmentation fault
C) The memory is reallocated automatically
D) The program crashes without warning
Answer: B) The program experiences a segmentation fault

What is the difference between stack and heap memory?
A) Stack memory is faster than heap memory
B) Heap memory is automatically managed while stack memory is not
C) Stack memory is used for dynamic allocation, heap for static
D) Both are managed in the same way
Answer: A) Stack memory is faster than heap memory

Which function in C++ is used to release memory allocated with new?
A) free()
B) delete
C) release()
D) dispose()
Answer: B) delete

What does the term “fragmentation” refer to in memory allocation?
A) Unused memory that cannot be allocated
B) Memory that is allocated in small, non-contiguous blocks
C) Memory that is fully utilized
D) The process of reallocating memory
Answer: B) Memory that is allocated in small, non-contiguous blocks

What type of allocation occurs when a program requests memory without knowing the size at compile time?
A) Static allocation
B) Dynamic allocation
C) Automatic allocation
D) Stack allocation
Answer: B) Dynamic allocation

What is the primary purpose of the malloc() function in C?
A) To allocate memory on the stack
B) To allocate memory on the heap
C) To free allocated memory
D) To initialize variables
Answer: B) To allocate memory on the heap

Which of the following is true about the “delete” operator in C++?
A) It cannot be used with arrays
B) It automatically initializes memory
C) It deallocates memory previously allocated with new
D) It is the same as free()
Answer: C) It deallocates memory previously allocated with new

What can lead to undefined behavior in a program?
A) Properly allocated memory
B) Accessing deallocated memory
C) Using static variables
D) Allocating memory on the stack
Answer: B) Accessing deallocated memory

What is the consequence of using the free() function on a pointer that has not been allocated memory?
A) The program runs normally
B) A segmentation fault occurs
C) Memory is reallocated
D) The pointer becomes null
Answer: B) A segmentation fault occurs

Which memory allocation technique allows the reuse of freed memory?
A) First-fit allocation
B) Best-fit allocation
C) Buddy allocation
D) None of the above
Answer: C) Buddy allocation

What does the term “garbage collection” refer to in programming?
A) The process of cleaning up uninitialized memory
B) Automatic deallocation of unused memory
C) Manual memory management
D) The technique of allocating large memory blocks
Answer: B) Automatic deallocation of unused memory

Which of the following is a method to reduce memory fragmentation?
A) Using static memory allocation
B) Compaction
C) Dynamic typing
D) Using larger memory blocks
Answer: B) Compaction

What type of memory allocation is typically used for local variables in functions?
A) Static allocation
B) Dynamic allocation
C) Stack allocation
D) Heap allocation
Answer: C) Stack allocation

Which of the following can lead to stack overflow?
A) Deep recursion without proper termination
B) Allocating large amounts of heap memory
C) Using dynamic memory allocation
D) Accessing uninitialized memory
Answer: A) Deep recursion without proper termination

What is the purpose of realloc() in C?
A) To allocate memory for new variables
B) To increase or decrease the size of previously allocated memory
C) To free allocated memory
D) To initialize memory
Answer: B) To increase or decrease the size of previously allocated memory

In which memory area are global variables typically stored?
A) Stack
B) Heap
C) Data segment
D) Code segment
Answer: C) Data segment

Which of the following statements about memory allocation is false?
A) Dynamic allocation can lead to fragmentation
B) Stack memory is automatically managed
C) Heap memory is faster than stack memory
D) Both malloc() and new allocate memory on the heap
Answer: C) Heap memory is faster than stack memory

What is a common issue when using dynamic memory allocation in C/C++?
A) Memory initialization
B) Memory leaks
C) Stack overflow
D) None of the above
Answer: B) Memory leaks

What is the purpose of the free() function?
A) To allocate memory on the stack
B) To deallocate previously allocated memory
C) To initialize variables
D) To check memory usage
Answer: B) To deallocate previously allocated memory

Which of the following is an effect of using too much heap memory?
A) Increased performance
B) Memory exhaustion leading to allocation failures
C) Improved data locality
D) None of the above
Answer: B) Memory exhaustion leading to allocation failures

What is the main advantage of using dynamic memory allocation?
A) Predictable memory usage
B) Flexibility to use varying amounts of memory at runtime
C) Reduced complexity in program design
D) Faster execution
Answer: B) Flexibility to use varying amounts of memory at runtime

What happens to local variables when a function exits?
A) They remain in memory until explicitly freed
B) They are deallocated automatically
C) They are moved to the heap
D) They are preserved for future use
Answer: B) They are deallocated automatically

What is the purpose of a memory pool in dynamic allocation?
A) To manage static variables
B) To reduce allocation overhead and fragmentation
C) To allocate memory for I/O operations
D) To store global variables
Answer: B) To reduce allocation overhead and fragmentation

In C++, what does the term “smart pointer” refer to?
A) A pointer that automatically manages memory
B) A pointer with enhanced arithmetic capabilities
C) A pointer that can point to multiple types
D) A pointer that does not require deallocation
Answer: A) A pointer that automatically manages memory

What is a potential drawback of using dynamic memory allocation?
A) Increased control over memory
B) Potential for fragmentation
C) Easier debugging
D) Improved performance
Answer: B) Potential for fragmentation

Which of the following is not a type of dynamic memory allocation?
A) new
B) malloc()
C) realloc()
D) static
Answer: D) static

What happens if you forget to free dynamically allocated memory?
A) The memory will be automatically released
B) Memory leaks may occur
C) The program will crash immediately
D) The memory will be marked as unused
Answer: B) Memory leaks may occur

What does the term “stack frame” refer to?
A) The space allocated for global variables
B) The structure that holds local variables and function parameters
C) The memory allocated for heap variables
D) The portion of memory reserved for program instructions
Answer: B) The structure that holds local variables and function parameters

What is a common approach to managing memory allocation in embedded systems?
A) Using dynamic memory allocation extensively
B) Preallocating all required memory at compile time
C) Utilizing garbage collection
D) Allowing for memory overflow
Answer: B) Preallocating all required memory at compile time

Read More Computer Architecture MCQs

  1. SET 1: Computer Architecture MCQs
  2. SET 2: Computer Architecture MCQs
  3. SET 3: Computer Architecture MCQs
  4. SET 4: Computer Architecture MCQs
  5. SET 5: Computer Architecture MCQs
  6. SET 6: Computer Architecture MCQs
  7. SET 7: Computer Architecture MCQs
  8. SET 8: Computer Architecture MCQs
  9. SET 9: Computer Architecture MCQs