AlgoMaster Logo

Best Practices - Quiz

Last Updated: December 6, 2025

1 min read

Best Practices Quiz

30 quizzes

1
Multiple Choice

Which naming convention best follows the described C++ coding standard for a boolean member?

2
Multiple Choice

Why should you mark read-only member functions as const?

3
Multiple Choice

According to the Rule of Zero, how should resource management usually be handled?

4
Multiple Choice

Which code most safely avoids a memory leak in modern C++?

5
Multiple Choice

Which is the MOST important first step before performance optimization?

6
Multiple Choice

Which modern C++ feature best supports RAII-based resource management?

7
Multiple Choice

In a multi-threaded C++ program, which is generally the safest way to protect shared data?

8
Multiple Choice

Which practice BEST helps when debugging a hard-to-reproduce bug?

9
Multiple Choice

Which declaration shows best const correctness for a read-only parameter?

10
Multiple Choice

A class manages a file handle. It defines a destructor but no copy/move operations. Which rule applies?

11
Code Completion

Declare a constant maximum buffer size following the described naming convention

cpp
1
const std::size_t = 4096;

Click an option to fill the blank:

12
Code Completion

Make this member function const-correct

cpp
1
int getCount() { return count; }

Click an option to fill the blank:

13
Code Completion

Use a smart pointer to express exclusive ownership of a dynamically allocated object

cpp
1
std::<Resource> res = std::make_unique<Resource>();

Click an option to fill the blank:

14
Output Prediction

What is the output of this code related to const pointers?

1#include <iostream>
2
3void foo() {
4    int value = 10;
5    const int* p1 = &value;
6    int* const p2 = &value;
7    *p2 = 20;
8    std::cout << *p1 << " " << *p2 << std::endl;
9}
10
11int main() {
12    foo();
13}
15
Output Prediction

What does this Rule-of-Three-inspired class print?

1#include <iostream>
2#include <cstring>
3
4class Buffer {
5public:
6    Buffer(const char* s) {
7        data = new char[std::strlen(s) + 1];
8        std::strcpy(data, s);
9    }
10    ~Buffer() { delete[] data; }
11    Buffer(const Buffer& other) {
12        data = new char[std::strlen(other.data) + 1];
13        std::strcpy(data, other.data);
14    }
15    void print() const { std::cout << data << std::endl; }
16private:
17    char* data{};
18};
19
20int main() {
21    Buffer a("Hello");
22    Buffer b = a;
23    b.print();
24}
16
Output Prediction

What is the output regarding smart pointer lifetime?

1#include <iostream>
2#include <memory>
3
4struct Tracer {
5    Tracer(const char* msg) : msg(msg) { std::cout << "ctor " << msg << '\n'; }
6    ~Tracer() { std::cout << "dtor " << msg << '\n'; }
7    const char* msg;
8};
9
10int main() {
11    {
12        std::unique_ptr<Tracer> p = std::make_unique<Tracer>("A");
13        std::cout << "inside" << '\n';
14    }
15    std::cout << "outside" << '\n';
16}
17
Output Prediction

What is the output for this range-based for loop example?

1#include <iostream>
2#include <vector>
3
4int main() {
5    std::vector<int> v{1, 2, 3};
6    for (auto x : v) {
7        x *= 2;
8    }
9    for (auto x : v) {
10        std::cout << x << " ";
11    }
12}
18
Output Prediction

What is the output when using std::lock_guard for thread-safe increments?

1#include <iostream>
2#include <thread>
3#include <mutex>
4
5int main() {
6    int counter = 0;
7    std::mutex m;
8
9    auto work = [&]() {
10        for (int i = 0; i < 1000; ++i) {
11            std::lock_guard<std::mutex> lock(m);
12            ++counter;
13        }
14    };
15
16    std::thread t1(work);
17    std::thread t2(work);
18    t1.join();
19    t2.join();
20
21    std::cout << counter << std::endl;
22}
19
Bug Spotting

Find the bug related to memory management and Rule of Three/Five

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

cpp
1
#include <string>
2
 
3
class ResourceHolder {
4
public:
5
    ResourceHolder(const std::string& name)
6
        : name(name), data(new int[100]) {}
7
 
8
    ~ResourceHolder() { delete[] data; }
9
 
10
private:
11
    std::string name;
12
    int* data;
13
};
14
 
15
ResourceHolder makeCopy(ResourceHolder r) {
16
    return r;
17
}
20
Bug Spotting

Find the bug that can cause undefined behavior in this debugging example

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

cpp
1
#include <iostream>
2
 
3
int divide(int a, int b) {
4
    int result = a / b;
5
    return result;
6
}
7
 
8
int main() {
9
    int x = 10;
10
    int y = 0;
11
    std::cout << divide(x, y) << std::endl;
12
}
21
Hotspot Selection

Click the line that causes a memory leak when an early return happens

Click on the line to select.

cpp
1
void process(bool error) {
2
    int* data = new int[100];
3
    // use data
4
    if (error) return;
5
    delete[] data;
6
}
22
Hotspot Selection

Click the line that violates const correctness best practices

Click on the line to select.

cpp
1
#include <string>
2
 
3
class User {
4
public:
5
    User(const std::string& name) : name(name) {}
6
 
7
    const std::string& getName() {
8
        return name;
9
    }
10
 
11
private:
12
    std::string name;
13
};
23
Fill in the Blanks

Complete the RAII-style class that manages a file using modern C++ and const correctness

cpp
1
#include <fstream>
2
3
class FileGuard {
4
public:
5
FileGuard(const std::string& path) : file(path) {}
6
7
bool () const { return file.is_open(); }
8
9
void write(const std::string& text)
10
11
private:
12
std::ofstream file;
13
};

Click an option to fill blank 1:

24
Fill in the Blanks

Fix const correctness in this accessor following the guidelines

cpp
1
class Config {
2
public:
3
Config(int v) : value(v) {}
4
5
int () { return value; }
6
7
private:
8
int value;
9
};

Click an option to fill blank 1:

25
Fill in the Blanks

Use modern C++ to safely share ownership of a dynamically created object

cpp
1
#include <memory>
2
3
struct Data { };
4
5
int main() {
6
auto p1 = std::<Data>(new Data{});
7
std::<Data> p2 = p1;
8
}

Click an option to fill blank 1:

26
Fill in the Blanks

Complete this performance-conscious loop using references to avoid copies

cpp
1
#include <vector>
2
3
int sum(const std::vector<int>& v) {
4
int s = 0;
5
for ( x : ) {
6
s += x;
7
}
8
return s;
9
}

Click an option to fill blank 1:

27
Matching

Match each rule with its main recommendation

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

28
Matching

Match each modern C++ guideline with its benefit

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

29
Sequencing

Order the typical debugging steps for a tricky C++ bug

Drag and drop to reorder, or use the arrows.

30
Sequencing

Order the steps to implement a safe, high-performance C++ feature

Drag and drop to reorder, or use the arrows.

Premium Content

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