AlgoMaster Logo

Memory Management - Quiz

Last Updated: December 6, 2025

1 min read

Memory Management Quiz

30 quizzes

1
Multiple Choice

Which statement about stack vs heap in C++ is TRUE?

2
Multiple Choice

What is a major drawback of using raw new/delete directly?

3
Multiple Choice

Which allocation is MOST appropriate for a large vector whose size is only known at runtime?

4
Multiple Choice

Which correctly matches new/delete usage?

5
Multiple Choice

What does RAII primarily rely on?

6
Multiple Choice

Which situation is MOST likely to cause a memory leak?

7
Multiple Choice

What is a key advantage of using std::shared_ptr over raw pointers?

8
Multiple Choice

Which is the BEST way in modern C++ to manage a heap-allocated file handle wrapper?

9
Multiple Choice

Which line describes a stack overflow risk?

10
Multiple Choice

How does RAII improve exception safety?

11
Code Completion

Allocate an int on the heap with value 10 using new

cpp
1
int* p = int(10);

Click an option to fill the blank:

12
Code Completion

Declare a unique_ptr that owns a dynamically allocated std::string

cpp
1
std::<std::string> name = std::make_unique<std::string>("Alice");

Click an option to fill the blank:

13
Code Completion

Use delete[] to free a dynamically allocated array

cpp
1
int* arr = new int[100];
2
// ... use arr ...
3
[] arr;

Click an option to fill the blank:

14
Output Prediction

What is the output of this code about stack vs heap variables?

1#include <iostream>
2
3void foo(int* p) {
4    *p = 20;
5}
6
7int main() {
8    int x = 10;
9    int* hp = new int(30);
10    foo(&x);
11    foo(hp);
12    std::cout << x << " " << *hp << std::endl;
13    delete hp;
14    return 0;
15}
15
Output Prediction

What is the output regarding delete and double delete?

1#include <iostream>
2
3struct Test {
4    Test() { std::cout << "C"; }
5    ~Test() { std::cout << "D"; }
6};
7
8int main() {
9    Test* t = new Test();
10    delete t;
11    t = nullptr;
12    if (!t) std::cout << "N";
13    return 0;
14}
16
Output Prediction

What is the output when using RAII with std::unique_ptr?

1#include <iostream>
2#include <memory>
3
4struct Res {
5    Res()  { std::cout << "A"; }
6    ~Res() { std::cout << "B"; }
7};
8
9int main() {
10    {
11        std::unique_ptr<Res> r = std::make_unique<Res>();
12        std::cout << "C";
13    }
14    std::cout << "D";
15    return 0;
16}
17
Output Prediction

What does this code output about shared_ptr reference counts?

1#include <iostream>
2#include <memory>
3
4int main() {
5    auto sp1 = std::make_shared<int>(42);
6    std::cout << sp1.use_count() << " ";
7    {
8        auto sp2 = sp1;
9        std::cout << sp1.use_count() << " ";
10    }
11    std::cout << sp1.use_count() << std::endl;
12    return 0;
13}
18
Output Prediction

What is the output regarding missing delete (logical effect only)?

1#include <iostream>
2
3int main() {
4    int* p = new int(5);
5    std::cout << *p << " ";
6    *p = 7;
7    std::cout << *p << std::endl;
8    // delete p; // intentionally omitted
9    return 0;
10}
19
Bug Spotting

Find the memory management bug in this code

Click on the line(s) that contain the bug.

cpp
1
#include <string>
2
 
3
std::string* makeMessage() {
4
    std::string msg = "Hello";
5
    return &msg;
6
}
7
 
20
Bug Spotting

Find the bug related to new/delete

Click on the line(s) that contain the bug.

cpp
1
#include <iostream>
2
 
3
struct Node {
4
    int value;
5
};
6
 
7
int main() {
8
    Node* arr = new Node[3];
9
    arr[0].value = 1;
10
    arr[1].value = 2;
11
    arr[2].value = 3;
12
    delete arr;
13
    return 0;
14
}
15
 
21
Hotspot Selection

Click the line that causes a memory leak

Click on the line to select.

cpp
1
#include <string>
2
 
3
void loadConfig(bool fail) {
4
    std::string* cfg = new std::string("default");
5
    if (fail) {
6
        return; // early exit
7
    }
8
    delete cfg;
9
}
10
 
22
Hotspot Selection

Click the line where RAII correctly releases the resource

Click on the line to select.

cpp
1
#include <fstream>
2
#include <string>
3
 
4
void save(const std::string& path) {
5
    std::ofstream out(path);
6
    out << "data";
7
}
8
 
23
Fill in the Blanks

Complete the RAII-style wrapper to manage a heap allocation

cpp
1
#include <iostream>
2
3
class IntHolder {
4
public:
5
IntHolder(int v) : ptr(new int(v)) {}
6
~IntHolder() { ptr; }
7
int get() const { return *ptr; }
8
private:
9
ptr;
10
};
11
12
int main() {
13
IntHolder h(42);
14
std::cout << h.get() << std::endl;
15
}
16

Click an option to fill blank 1:

24
Fill in the Blanks

Use std::unique_ptr to avoid manual delete

cpp
1
#include <memory>
2
3
int main() {
4
std::<int> p = std::<int>(5);
5
return ;
6
}
7

Click an option to fill blank 1:

25
Fill in the Blanks

Fill in malloc/free usage for a C-style dynamic array

cpp
1
#include <cstdlib>
2
3
int main() {
4
int n = 10;
5
int* arr = (int*)(n * sizeof(int));
6
if (!arr) return 1;
7
// use arr
8
(arr);
9
return 0;
10
}
11

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the RAII-style std::lock_guard usage with std::mutex

cpp
1
#include <mutex>
2
3
std::mutex m;
4
int counter = 0;
5
6
void inc() {
7
std::<std::mutex> lock();
8
++counter;
9
}
10

Click an option to fill blank 1:

27
Matching

Match the memory concept to its description

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

28
Matching

Match the smart pointer with its primary ownership model

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

29
Sequencing

Order the steps to safely allocate and use a dynamic array with new[] in C++

Drag and drop to reorder, or use the arrows.

30
Sequencing

Order the typical workflow for using RAII to manage a file resource in C++

Drag and drop to reorder, or use the arrows.

Premium Content

Subscribe to unlock full access to this content and more premium articles.