Memory Management MCQs

8 MCQs  — Fundamental Programming MCQs

Practice Memory Management MCQs with correct answers and explanations. These questions are from the Fundamental Programming MCQs section.

In C++, where are local (automatic) variables and memory allocated with new typically stored?
Both on the heap
Locals on the heap; new on the stack
Locals on the stack; new on the heap (free store)
Both on the stack
In C++, what is the result of using delete p; on memory allocated with p = new int[10];?
Undefined behavior; delete[] p must be used
Compilation error
It frees only p[0]
It correctly frees all 10 ints
What is wrong with this C++ function? int* f(){ int x = 5; return &x; }
Memory leak
Compilation error
Nothing; it works correctly
It returns a dangling pointer to a local variable that no longer exists
How does CPython primarily manage memory?
Reference counting, plus a cyclic garbage collector
Manual free() calls
It never frees memory
Only a mark-and-sweep garbage collector
What happens when a null pointer is dereferenced in C++?
A NullPointerException is thrown
Compilation error
The value 0 is returned
Undefined behavior (commonly a segmentation fault)