AlgoMaster Logo

Advanced Topics - Quiz

Last Updated: December 6, 2025

1 min read

Advanced Topics Quiz

30 quizzes

1
Multiple Choice

Which preprocessor directive is MOST appropriate to prevent multiple inclusion of a header file in modern C++?

2
Multiple Choice

What is the main drawback of object-like macros used as constants?

3
Multiple Choice

Which using-directive or declaration is generally considered safest in headers?

4
Multiple Choice

Which header must you include to use std::is_integral and std::enable_if?

5
Multiple Choice

In SFINAE, what happens when template argument substitution fails in a candidate function template?

6
Multiple Choice

What kind of polymorphism does CRTP typically provide?

7
Multiple Choice

What is the primary motivation for using the Pimpl idiom?

8
Multiple Choice

Which key idea makes the copy-swap idiom exception-safe for copy assignment?

9
Multiple Choice

Which macro feature allows you to accept a variable number of arguments?

10
Multiple Choice

Which statement about inline namespaces is accurate?

11
Code Completion

Use conditional compilation to include <thread> only when MULTI_THREAD is defined

cpp
1
#ifdef MULTI_THREAD
2
#include <>
3
#endif

Click an option to fill the blank:

12
Code Completion

Complete the macro to compute the maximum of two values safely with parentheses

cpp
1
#define MAX_SAFE(a, b) (((a) > (b)) ? (a) : ())

Click an option to fill the blank:

13
Code Completion

Declare a nested namespace in C++17 for project::net

cpp
1
namespace project:: {
2
3
}

Click an option to fill the blank:

14
Output Prediction

What is the output of this code using conditional compilation?

1#include <iostream>
2#define DEBUG 1
3int main() {
4#if DEBUG
5    std::cout << "Debug mode";
6#else
7    std::cout << "Release mode";
8#endif
9    return 0;
10}
15
Output Prediction

What is the output considering macro expansion?

1#include <iostream>
2#define SQUARE(x) ((x) * (x))
3int main() {
4    int a = 3;
5    std::cout << SQUARE(a + 1) << std::endl;
6    return 0;
7}
16
Output Prediction

What is the output regarding namespaces and scope resolution?

1#include <iostream>
2namespace A {
3    void f() { std::cout << "A"; }
4}
5namespace B {
6    void f() { std::cout << "B"; }
7}
8int main() {
9    using A::f;
10    f();
11    B::f();
12    return 0;
13}
17
Output Prediction

What is the output of this CRTP-based code?

1#include <iostream>
2
3template <typename Derived>
4class Base {
5public:
6    void run() {
7        static_cast<Derived*>(this)->impl();
8    }
9};
10
11class Worker : public Base<Worker> {
12public:
13    void impl() { std::cout << "Worker"; }
14};
15
16int main() {
17    Worker w;
18    w.run();
19    return 0;
20}
18
Output Prediction

What is the output of this copy-swap style assignment?

1#include <iostream>
2#include <utility>
3
4class Data {
5public:
6    Data(int v) : value(new int(v)) {}
7    Data(const Data& other) : value(new int(*other.value)) {}
8    ~Data() { delete value; }
9
10    void swap(Data& other) noexcept {
11        std::swap(value, other.value);
12    }
13
14    Data& operator=(Data other) {
15        swap(other);
16        return *this;
17    }
18
19    void print() const { std::cout << *value; }
20private:
21    int* value;
22};
23
24int main() {
25    Data a(1);
26    Data b(2);
27    b = a;
28    b.print();
29    return 0;
30}
19
Bug Spotting

Find the bug related to the Pimpl idiom and memory management

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

cpp
1
#include <string>
2
 
3
class Widget {
4
public:
5
    Widget();
6
    ~Widget();
7
    void setName(const std::string& n);
8
private:
9
    class Impl;
10
    Impl* pImpl;
11
};
12
 
13
Widget::Widget() : pImpl(new Impl) {}
14
 
15
Widget::~Widget() {}
16
 
17
void Widget::setName(const std::string& n) {
18
    // use pImpl
19
}
20
 
20
Bug Spotting

Identify the bug in this SFINAE-based function template

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

cpp
1
#include <type_traits>
2
#include <iostream>
3
 
4
template <typename T>
5
std::enable_if_t<std::is_integral<T>::value>
6
printIfInt(T v) {
7
    std::cout << v << " is integral\n";
8
}
9
 
10
int main() {
11
    printIfInt(42);
12
    printIfInt(3.14);
13
}
14
 
21
Hotspot Selection

Click the line that causes undefined behavior due to misuse of a macro

Click on the line to select.

cpp
1
#include <iostream>
2
#define INCREMENT(x) ++x
3
 
4
int main() {
5
    int n = 1;
6
    int m = INCREMENT(n) * INCREMENT(n);
7
    std::cout << n << " " << m << std::endl;
8
    return 0;
9
}
22
Hotspot Selection

Click the line where the copy-swap idiom performs the swap operation

Click on the line to select.

cpp
1
#include <utility>
2
 
3
class Buffer {
4
public:
5
    Buffer(size_t n) : size(n), data(new int[n]) {}
6
    Buffer(const Buffer& other) : size(other.size), data(new int[other.size]) {}
7
 
8
    void swap(Buffer& other) noexcept {
9
        std::swap(size, other.size);
10
        std::swap(data, other.data);
11
    }
12
 
13
    Buffer& operator=(Buffer other) {
14
        swap(other);
15
        return *this;
16
    }
17
private:
18
    size_t size;
19
    int* data;
20
};
21
 
23
Fill in the Blanks

Complete the header guard using traditional preprocessor directives

cpp
1
#ifndef
2
#define
3
4
class Session {
5
public:
6
void run();
7
};
8
9
#endif //
10

Click an option to fill blank 1:

24
Fill in the Blanks

Complete the type traits-based enable_if for integral types

cpp
1
#include <type_traits>
2
3
template <typename T>
4
std::enable_if_t<<T>::value, >
5
process(T) {
6
// implementation for integral types
7
}
8

Click an option to fill blank 1:

25
Fill in the Blanks

Complete this CRTP base class pattern

cpp
1
template <typename >
2
class BaseLogger {
3
public:
4
void log(const std::string& msg) {
5
static_cast<*>(this)->write(msg);
6
}
7
};
8

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the Pimpl idiom using std::unique_ptr for exception safety

cpp
1
#include <memory>
2
3
class Engine {
4
public:
5
Engine();
6
~Engine();
7
private:
8
class Impl;
9
std::unique_ptr<Impl> ;
10
};
11
12
Engine::Engine() : (new Impl) {}
13
Engine::~Engine() = default;
14

Click an option to fill blank 1:

27
Matching

Match each type trait with what it checks

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

28
Matching

Match each advanced C++ concept with its primary benefit

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 implement a class using the Pimpl idiom

Drag and drop to reorder, or use the arrows.

30
Sequencing

Order the steps for using SFINAE with std::enable_if in a function template

Drag and drop to reorder, or use the arrows.

Premium Content

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