Which of the following best describes dynamic memory allocation in a computer program?
Think about when the program decides how much memory it needs while running.
Dynamic memory allocation means the program asks the computer for memory while it is running, allowing flexible use of memory.
Consider this simple program that creates and deletes objects. What is the total number of objects in memory after the program finishes?
objects = [] for i in range(3): obj = {'id': i} objects.append(obj) objects.pop(1) objects.append({'id': 3})
Count how many objects are added and removed step by step.
The program adds 3 objects, removes one, then adds one more, so total objects in the list are 3.
Which of the following scenarios is most likely to cause a memory leak in a program?
Think about what happens if memory is not released properly.
Memory leaks happen when programs hold onto memory they no longer need, preventing the system from reclaiming it.
Which statement correctly compares stack and heap memory?
Think about where local variables and dynamically created objects are stored.
Stack stores local variables with fixed size and lifetime; heap stores objects created dynamically during program execution.
What is the output of this Python code that demonstrates reference counting?
import sys x = [1, 2, 3] y = x z = y print(sys.getrefcount(x))
Remember that getrefcount includes the temporary reference as an argument.
The list x has references from x, y, z, and the argument passed to getrefcount, totaling 4.