Last Updated: December 6, 2025
30 quizzes
Which statement about stack vs heap in C++ is TRUE?
What is a major drawback of using raw new/delete directly?
Which allocation is MOST appropriate for a large vector whose size is only known at runtime?
Which correctly matches new/delete usage?
What does RAII primarily rely on?
Which situation is MOST likely to cause a memory leak?
What is a key advantage of using std::shared_ptr over raw pointers?
Which is the BEST way in modern C++ to manage a heap-allocated file handle wrapper?
Which line describes a stack overflow risk?
How does RAII improve exception safety?
Allocate an int on the heap with value 10 using new
int* p = int(10);Click an option to fill the blank:
Declare a unique_ptr that owns a dynamically allocated std::string
std::<std::string> name = std::make_unique<std::string>("Alice");Click an option to fill the blank:
Use delete[] to free a dynamically allocated array
int* arr = new int[100];// ... use arr ...[] arr;Click an option to fill the blank:
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}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}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}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}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}Find the memory management bug in this code
Click on the line(s) that contain the bug.
#include <string> std::string* makeMessage() { std::string msg = "Hello"; return &msg;} Find the bug related to new/delete
Click on the line(s) that contain the bug.
#include <iostream> struct Node { int value;}; int main() { Node* arr = new Node[3]; arr[0].value = 1; arr[1].value = 2; arr[2].value = 3; delete arr; return 0;} Click the line that causes a memory leak
Click on the line to select.
#include <string> void loadConfig(bool fail) { std::string* cfg = new std::string("default"); if (fail) { return; // early exit } delete cfg;} Click the line where RAII correctly releases the resource
Click on the line to select.
#include <fstream>#include <string> void save(const std::string& path) { std::ofstream out(path); out << "data";} Complete the RAII-style wrapper to manage a heap allocation
#include <iostream>class IntHolder {public: IntHolder(int v) : ptr(new int(v)) {} ~IntHolder() { ptr; } int get() const { return *ptr; }private: ptr;};int main() { IntHolder h(42); std::cout << h.get() << std::endl;}Click an option to fill blank 1:
Use std::unique_ptr to avoid manual delete
#include <memory>int main() { std::<int> p = std::<int>(5); return ;}Click an option to fill blank 1:
Fill in malloc/free usage for a C-style dynamic array
#include <cstdlib>int main() { int n = 10; int* arr = (int*)(n * sizeof(int)); if (!arr) return 1; // use arr (arr); return 0;}Click an option to fill blank 1:
Complete the RAII-style std::lock_guard usage with std::mutex
#include <mutex>std::mutex m;int counter = 0;void inc() { std::<std::mutex> lock(); ++counter;}Click an option to fill blank 1:
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.
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.
Order the steps to safely allocate and use a dynamic array with new[] in C++
Drag and drop to reorder, or use the arrows.
Order the typical workflow for using RAII to manage a file resource in C++
Drag and drop to reorder, or use the arrows.