Memory management (stack, heap, static)(MCQs)

Which of the following is true about stack memory? a) It is used for dynamic memory allocation b) It stores global variables c) It stores function call information and local variables d) It is manually managed by the programmer Answer: c) It stores function call information and local variables Which of the following memory areas is used for dynamic memory allocation in C++? a) Stack b) Heap c) Data segment d) Code segment Answer: b) Heap What happens to local variables when a function exits? a) They are stored in the heap b) They are freed automatically from the stack c) They remain in memory d) They are converted to global variables Answer: b) They are freed automatically from the stack Which memory segment is used for static variables in C++? a) Stack b) Heap c) Data segment d) Code segment Answer: c) Data segment Which of the following is a characteristic of heap memory? a) It grows and shrinks as functions are called and returned b) It requires manual memory management c) It stores function return addresses d) It is automatically cleared when the program exits Answer: b) It requires manual memory management In which memory area are global variables stored? a) Heap b) Stack c) Data segment d) Text segment Answer: c) Data segment What is a stack overflow? a) When the heap runs out of memory b) When too much memory is allocated in the stack c) When memory is deallocated incorrectly d) When global variables exceed their memory limits Answer: b) When too much memory is allocated in the stack Which of the following memory areas is automatically managed by the system? a) Heap b) Stack c) Data segment d) Code segment Answer: b) Stack What is the primary purpose of the heap memory? a) Storing local variables b) Storing static variables c) Dynamic memory allocation d) Storing function return addresses Answer: c) Dynamic memory allocation Which memory segment holds executable instructions? a) Stack b) Heap c) Data segment d) Text segment Answer: d) Text segment What is the role of the “free” function in C++? a) To allocate memory in the heap b) To deallocate memory from the stack c) To deallocate dynamically allocated memory from the heap d) To clear the static variables Answer: c) To deallocate dynamically allocated memory from the heap Which of the following memory segments grows when a new function is called? a) Heap b) Stack c) Static segment d) Code segment Answer: b) Stack What happens to memory in the heap if not explicitly freed? a) It is automatically reclaimed when the program ends b) It causes a memory leak c) It is cleared when the stack is cleared d) It generates a compile-time error Answer: b) It causes a memory leak Which function is used to allocate memory in the heap in C++? a) malloc() b) free() c) new d) delete Answer: c) new Which of the following is true about static variables in C++? a) They are allocated on the stack b) They are created and destroyed with each function call c) They are initialized once and persist throughout the program’s life d) They must be dynamically allocated Answer: c) They are initialized once and persist throughout the program’s life Which of the following is true about stack memory allocation? a) It requires manual allocation and deallocation b) It is slower than heap memory allocation c) It follows a Last In, First Out (LIFO) order d) It cannot store local variables Answer: c) It follows a Last In, First Out (LIFO) order What is the correct way to deallocate dynamically allocated memory in C++? a) free() b) delete c) remove() d) clear() Answer: b) delete Which memory segment is used for storing string literals in C++? a) Heap b) Stack c) Data segment d) Text segment Answer: d) Text segment What happens if too much memory is allocated in the heap and it exceeds available space? a) Stack overflow b) Heap overflow c) Segmentation fault d) Memory leak Answer: b) Heap overflow Which memory area is faster to access, stack or heap? a) Stack b) Heap c) Both are equally fast d) Neither; memory access speed is not relevant Answer: a) Stack Which memory management error occurs when trying to access deallocated heap memory? a) Stack overflow b) Buffer overflow c) Use after free d) Memory leak Answer: c) Use after free Which of the following allocates memory from the stack in C++? a) malloc() b) new c) Local variable declarations d) free() Answer: c) Local variable declarations What is a memory leak? a) When a program runs out of stack memory b) When a program allocates memory in the heap and never frees it c) When a program attempts to access memory out of bounds d) When global variables exceed memory limits Answer: b) When a program allocates memory in the heap and never frees it Which memory segment is responsible for handling recursion in C++? a) Heap b) Stack c) Data segment d) Text segment Answer: b) Stack What happens if a program consumes too much stack space during execution? a) Memory leak b) Heap overflow c) Stack overflow d) Segmentation fault Answer: c) Stack overflow Which memory management function in C++ pairs with the “new” operator? a) free() b) delete c) malloc() d) remove() Answer: b) delete Where are static variables stored in memory? a) Heap b) Stack c) Static data segment d) Text segment Answer: c) Static data segment What does the “delete” operator do in C++? a) Allocates memory in the heap b) Deallocates memory from the heap c) Clears static variables d) Removes variables from the stack Answer: b) Deallocates memory from the heap Which of the following does not use dynamic memory allocation? a) Heap b) Stack c) malloc() d) new Answer: b) Stack Which of the following causes a memory leak? a) Forgetting to free dynamically allocated memory b) Allocating too much stack memory c) Deleting memory before its use d) Using the wrong variable type Answer: a) Forgetting to free dynamically allocated memory Which of the following segments does not grow or shrink during program execution? a) Stack b) Heap c) Static data segment d) Dynamic memory segment Answer: c) Static data segment How are static variables initialized in C++? a) Automatically initialized to 0 b) Must be manually initialized c) Initialized at runtime d) They are not initialized by default Answer: a) Automatically initialized to 0 What is a buffer overflow? a) Allocating too much memory on the heap b) Reading or writing outside the bounds of a buffer c) Freeing memory before it is used d) Using uninitialized memory Answer: b) Reading or writing outside the bounds of a buffer Which type of memory is automatically cleared when a function returns? a) Heap memory b) Stack memory c) Static memory d) Text memory Answer: b) Stack memory What does the “malloc” function return? a) A pointer to the allocated memory b) The size of the allocated memory c) A reference to a stack variable d) The number of bytes freed Answer: a) A pointer to the allocated memory In which memory area are dynamically allocated arrays stored? a) Stack b) Heap c) Data segment d) Code segment Answer: b) Heap Which of the following is used to allocate memory in the heap in C? a) new b) malloc() c) delete d) stack_alloc() Answer: b) malloc() Which of the following memory management issues can cause a program to crash? a) Using malloc instead of new b) Accessing freed memory c) Reusing static variables d) Using the same pointer twice Answer: b) Accessing freed memory Where are local variables typically stored? a) Heap b) Stack c) Data segment d) Text segment Answer: b) Stack What is a segmentation fault? a) When memory is not allocated correctly b) When a program tries to access restricted memory c) When variables exceed stack limits d) When global variables are overwritten Answer: b) When a program tries to access restricted memory Which C++ operator is used to release memory allocated with malloc()? a) new b) delete c) free() d) release() Answer: c) free() What happens to dynamically allocated memory if “delete” is not called? a) It causes a stack overflow b) It causes a memory leak c) It is automatically deallocated d) It is reallocated to the heap Answer: b) It causes a memory leak What is the main characteristic of stack memory allocation? a) It is slower than heap allocation b) It is managed manually by the programmer c) It is limited in size and follows LIFO order d) It stores dynamic variables Answer: c) It is limited in size and follows LIFO order Which C++ function allocates memory but does not initialize it? a) malloc() b) calloc() c) new d) delete Answer: a) malloc() Which memory management function initializes allocated memory to zero in C? a) malloc() b) calloc() c) realloc() d) free() Answer: b) calloc() Which of the following memory segments is typically used for storing constant data? a) Heap b) Stack c) Data segment d) Text segment Answer: d) Text segment Which of the following errors occurs when trying to deallocate memory that was not dynamically allocated? a) Stack overflow b) Memory leak c) Double free error d) Segmentation fault Answer: c) Double free error In which memory segment are function parameters typically stored? a) Heap b) Stack c) Data segment d) Text segment Answer: b) Stack Which of the following can lead to undefined behavior in a C++ program? a) Freeing memory on the stack b) Accessing memory after delete c) Accessing an uninitialized static variable d) Allocating memory using malloc() Answer: b) Accessing memory after delete Which function is used to allocate memory for an array in C? a) malloc() b) calloc() c) realloc() d) free() Answer: b) calloc()  
All Copyrights Reserved 2025 Reserved by T4Tutorials