Bird
Raised Fist0
Intro to Computingfundamentals~20 mins

Memory management basics in Intro to Computing - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Memory Allocation

Which of the following best describes dynamic memory allocation in a computer program?

AMemory is only used to store program instructions, not data.
BMemory is fixed and reserved before the program starts running and cannot change during execution.
CMemory is allocated during program execution as needed, and can be freed when no longer required.
DMemory allocation happens only when the computer is turned off.
Attempts:
2 left
💡 Hint

Think about when the program decides how much memory it needs while running.

trace
intermediate
2:00remaining
Tracing Memory Usage in a Program

Consider this simple program that creates and deletes objects. What is the total number of objects in memory after the program finishes?

Intro to Computing
objects = []
for i in range(3):
    obj = {'id': i}
    objects.append(obj)
objects.pop(1)
objects.append({'id': 3})
A1
B4
C2
D3
Attempts:
2 left
💡 Hint

Count how many objects are added and removed step by step.

identification
advanced
2:00remaining
Identifying Memory Leak Causes

Which of the following scenarios is most likely to cause a memory leak in a program?

AA program keeps references to unused objects, preventing the memory from being freed.
BA program runs without any loops.
CA program uses only static memory allocation.
DA program frees memory immediately after using it.
Attempts:
2 left
💡 Hint

Think about what happens if memory is not released properly.

Comparison
advanced
2:00remaining
Comparing Stack and Heap Memory

Which statement correctly compares stack and heap memory?

AHeap memory is faster and smaller than stack memory.
BStack memory is for static, short-lived data; heap memory is for dynamic, long-lived data allocated at runtime.
CStack memory is used for storing files; heap memory stores program instructions.
DHeap memory is automatically cleaned up; stack memory requires manual management.
Attempts:
2 left
💡 Hint

Think about where local variables and dynamically created objects are stored.

🔍 Analysis
expert
2:00remaining
Output of Memory Reference Counting

What is the output of this Python code that demonstrates reference counting?

Intro to Computing
import sys

x = [1, 2, 3]
y = x
z = y
print(sys.getrefcount(x))
A4
B3
C1
D0
Attempts:
2 left
💡 Hint

Remember that getrefcount includes the temporary reference as an argument.

Practice

(1/5)
1. What is the main purpose of memory management in a computer system?
easy
A. To display images on the screen
B. To speed up the internet connection
C. To keep track of where data is stored and free unused space
D. To control the keyboard and mouse

Solution

  1. Step 1: Understand memory management role

    Memory management is responsible for tracking where data is stored in the computer's memory and freeing space when data is no longer needed.
  2. Step 2: Eliminate unrelated options

    Options B, C, and D describe other computer functions unrelated to memory management.
  3. Final Answer:

    To keep track of where data is stored and free unused space -> Option C
  4. Quick Check:

    Memory management = tracking and freeing memory [OK]
Hint: Memory management tracks and frees memory space [OK]
Common Mistakes:
  • Confusing memory management with input/output control
  • Thinking memory management speeds up internet
  • Mixing memory management with display functions
2. Which of the following is a correct statement about manual memory management?
easy
A. The programmer must explicitly free memory when it's no longer needed
B. Memory is freed automatically without programmer action
C. Memory management is not needed in programming
D. Memory is only allocated once and never freed

Solution

  1. Step 1: Define manual memory management

    Manual memory management means the programmer must tell the computer when to free memory to avoid leaks.
  2. Step 2: Compare options

    The programmer must explicitly free memory when it's no longer needed correctly states this. Memory is freed automatically without programmer action describes automatic memory management. Options A, B, and C are incorrect because memory management is always needed and memory must be freed.
  3. Final Answer:

    The programmer must explicitly free memory when it's no longer needed -> Option A
  4. Quick Check:

    Manual memory management = programmer frees memory [OK]
Hint: Manual means programmer frees memory explicitly [OK]
Common Mistakes:
  • Assuming memory frees automatically in manual management
  • Ignoring the need to free memory
  • Thinking memory is never freed
3. Consider this simple program flow:
1. Allocate memory for data
2. Use data
3. Forget to free memory
4. Program ends

What is the likely outcome?
medium
A. Program crashes immediately
B. Memory leak occurs because allocated memory is not freed
C. Memory is freed automatically before program ends
D. Data is lost but memory is freed

Solution

  1. Step 1: Analyze memory allocation and freeing

    Memory is allocated but never freed before program ends, so the allocated space remains occupied.
  2. Step 2: Understand consequences

    This causes a memory leak, where memory is wasted and unavailable for other uses.
  3. Final Answer:

    Memory leak occurs because allocated memory is not freed -> Option B
  4. Quick Check:

    Not freeing memory = memory leak [OK]
Hint: Not freeing allocated memory causes leaks [OK]
Common Mistakes:
  • Assuming memory frees automatically at program end
  • Confusing crash with memory leak
  • Thinking data loss frees memory
4. A programmer wrote this pseudocode:
allocate memory for list
use list
free memory for list
free memory for list

What is the problem here?
medium
A. Double free error causing program crash
B. Memory leak due to missing free
C. Correct memory management
D. Memory allocated twice

Solution

  1. Step 1: Identify memory free operations

    The program frees the same memory twice, which is unsafe.
  2. Step 2: Understand double free error

    Freeing memory twice can cause crashes or undefined behavior because the memory is already released.
  3. Final Answer:

    Double free error causing program crash -> Option A
  4. Quick Check:

    Freeing memory twice = double free error [OK]
Hint: Never free the same memory twice [OK]
Common Mistakes:
  • Ignoring double free risks
  • Thinking freeing twice is safe
  • Confusing double free with memory leak
5. You have a program that creates many temporary objects during execution. Which memory management approach helps avoid running out of memory automatically?
hard
A. Allocating all memory at program start and never freeing
B. Manual memory management where programmer frees each object
C. Ignoring memory management because OS handles it all
D. Automatic garbage collection that frees unused objects

Solution

  1. Step 1: Understand temporary objects and memory use

    Temporary objects use memory that should be freed when no longer needed to avoid running out of memory.
  2. Step 2: Identify suitable memory management

    Automatic garbage collection frees unused objects without programmer action, preventing memory exhaustion.
  3. Final Answer:

    Automatic garbage collection that frees unused objects -> Option D
  4. Quick Check:

    Garbage collection = automatic freeing [OK]
Hint: Garbage collection frees unused memory automatically [OK]
Common Mistakes:
  • Assuming OS frees all program memory immediately
  • Thinking manual freeing is best for many objects
  • Allocating memory once and never freeing causes leaks