AlgoMaster Logo

Modern C++ (C++11/14/17/20) - Quiz

Last Updated: December 6, 2025

Modern C++ (C++11/14/17/20) Quiz

30 quizzes

1
Multiple Choice

When is using 'auto' most appropriate in modern C++?

2
Multiple Choice

Which expression is the safest modern C++ way to represent a null pointer?

3
Multiple Choice

Which constructor enables brace initialization with a variable number of elements?

4
Multiple Choice

What is a key benefit of uniform (brace) initialization?

5
Multiple Choice

For which type is a move constructor most beneficial?

6
Multiple Choice

What does a parameter of type T&& in a function template usually represent?

7
Multiple Choice

Which statement about constexpr functions is correct in C++14 and later?

8
Multiple Choice

What happens if you call value() on an empty std::optional<int>?

9
Multiple Choice

Which utility is typically used to visit the active alternative of a std::variant?

10
Multiple Choice

Which statement about std::string_view is true?

11
Code Completion

Use auto with an iterator returned from std::vector<int>::begin()

cpp
1
std::vector<int> v{1,2,3}; auto = v.begin();

Click an option to fill the blank:

12
Code Completion

Use nullptr to initialize a raw pointer member

cpp
1
int* current = ;

Click an option to fill the blank:

13
Code Completion

Use std::filesystem::path to join a directory and filename

cpp
1
std::filesystem::path full = dir / ;

Click an option to fill the blank:

14
Output Prediction

What is the output of this code using auto and initializer lists?

1#include <iostream>
2#include <vector>
3int main() {
4    std::vector<int> v{1, 2, 3};
5    auto sum = 0;
6    for (auto x : v) sum += x;
7    std::cout << sum << "\n";
8    return 0;
9}
15
Output Prediction

What is the output related to move semantics?

1#include <iostream>
2#include <string>
3
4struct S {
5    std::string name;
6    S(std::string n) : name(std::move(n)) {}
7};
8
9int main() {
10    std::string s = "hello";
11    S a{s};
12    S b{std::move(s)};
13    std::cout << a.name << " " << b.name << " " << s << "\n";
14}
16
Output Prediction

What is the output using constexpr and compile-time calculation?

1#include <iostream>
2constexpr int mul(int a, int b) { return a * b; }
3int main() {
4    constexpr int v = mul(3, 4);
5    int x = 2;
6    std::cout << v << " " << mul(x, 5) << "\n";
7}
17
Output Prediction

What is the output when using std::optional?

1#include <iostream>
2#include <optional>
3
4std::optional<int> find_positive(int x) {
5    if (x > 0) return x;
6    return std::nullopt;
7}
8
9int main() {
10    auto a = find_positive(5);
11    auto b = find_positive(-1);
12    std::cout << a.has_value() << " " << b.has_value() << "\n";
13}
18
Output Prediction

What is the output when using structured bindings with std::pair?

1#include <iostream>
2#include <utility>
3
4std::pair<int, int> f() {
5    return {1, 2};
6}
7
8int main() {
9    auto [x, y] = f();
10    x += 3;
11    std::cout << x << " " << y << "\n";
12}
19
Bug Spotting

Find the bug in this code using std::string_view

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

cpp
1
#include <string>
2
#include <string_view>
3
 
4
std::string_view make_view() {
5
    std::string s = "temp";
6
    return s;
7
}
8
 
9
int main() {
10
    auto v = make_view();
11
    return v.size();
12
}
20
Bug Spotting

Find the bug in this code using std::filesystem

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

cpp
1
#include <filesystem>
2
#include <iostream>
3
 
4
int main() {
5
    std::filesystem::path p{"/tmp"};
6
    if (std::filesystem::is_regular_file(p)) {
7
        std::cout << "dir\n";
8
    } else if (std::filesystem::is_directory(p)) {
9
        std::cout << "file\n";
10
    }
11
}
21
Hotspot Selection

Click the line where perfect forwarding should be used instead of passing arg directly

Click on the line to select.

cpp
1
#include <utility>
2
 
3
void log(int&) {}
4
void log(int&&) {}
5
 
6
template <typename T>
7
void wrapper(T&& arg) {
8
    log(arg);
9
}
10
 
11
int main() {
12
    int x = 1;
13
    wrapper(x);
14
    wrapper(2);
15
}
22
Hotspot Selection

Click the line that may throw std::bad_variant_access

Click on the line to select.

cpp
1
#include <variant>
2
#include <string>
3
 
4
int main() {
5
    std::variant<int, std::string> v = std::string{"hi"};
6
    int i = std::get<int>(v);
7
    return i;
8
}
23
Fill in the Blanks

Complete the constructor to support std::initializer_list for a Matrix

cpp
1
#include <vector>
2
3
class Matrix {
4
std::vector<int> data;
5
public:
6
Matrix(std::initializer_list<int> ) : data() {}
7
};

Click an option to fill blank 1:

24
Fill in the Blanks

Fill in types for structured bindings from std::pair

cpp
1
#include <utility>
2
3
int main() {
4
std::pair<int, double> p{1, 2.5};
5
auto [, ] = p;
6
}

Click an option to fill blank 1:

25
Fill in the Blanks

Use std::filesystem to check if a path exists and is a directory

cpp
1
#include <filesystem>
2
3
bool is_dir(const std::filesystem::path& p) {
4
return std::filesystem::(p) && std::filesystem::(p);
5
}

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the function that returns std::optional<std::string_view>

cpp
1
#include <optional>
2
#include <string_view>
3
4
std::optional<std::string_view> first_word(std::string_view text) {
5
auto pos = text.find(' ');
6
if (pos == std::string_view::npos) return ;
7
return text.substr(0, pos);
8
}

Click an option to fill blank 1:

27
Matching

Match the modern C++ feature with its primary purpose

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

28
Matching

Match the move-related concept with its description

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 read a text file using std::filesystem and std::ifstream

Drag and drop to reorder, or use the arrows.

30
Sequencing

Order the steps to implement perfect forwarding in a wrapper function

Drag and drop to reorder, or use the arrows.

Premium Content

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