LearnFrenzy
Sign in Take a free test

Modern C++ Features (C++11 to C++23) · Exercise 2 of 4

Modern C++ Features (C++11 to C++23) Predict the Output

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each C++ program and choose what it prints. Every program was compiled and run on a 64-bit system, so the answers are the real output.

Question 11 Medium

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <array>
#include <iostream>

constexpr int square(int x) { return x * x; }
consteval int cube(int x) { return x * x * x; }

constexpr std::array<int, 5> make_table() {
    std::array<int, 5> t{};
    for (int i = 0; i < 5; ++i) t[i] = square(i) + cube(2);
    return t;
}

int main() {
    constexpr auto table = make_table();
    static_assert(table[3] == 17);
    int n = 6;
    std::cout << square(n) << ' ' << cube(3) << ' ' << table[4] << '\n';   // square at run time, cube at compile time
    return 0;
}
A 36 27 24
B 36 27 16
C 36 9 24
D Compilation error
Question page & discussion
Question 12 Medium

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

struct Release {
    int major, minor, patch;
    std::string tag;
    auto operator<=>(const Release&) const = default;
    bool operator==(const Release&) const = default;
};

int main() {
    std::vector<Release> r{{1, 4, 0, "b"}, {1, 10, 2, "a"}, {1, 4, 0, "a"}, {0, 9, 9, "z"}};
    std::sort(r.begin(), r.end());
    for (const auto& x : r) std::cout << x.major << '.' << x.minor << '.' << x.patch << x.tag << ' ';
    std::cout << (r[1] == Release{1, 4, 0, "b"}) << '\n';
    return 0;
}
A 0.9.9z 1.4.0a 1.4.0b 1.10.2a 0
B 0.9.9z 1.10.2a 1.4.0a 1.4.0b 0
C 0.9.9z 1.4.0b 1.4.0a 1.10.2a 1
D 1.10.2a 1.4.0a 1.4.0b 0.9.9z 0
Question page & discussion
Question 13 Hard

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <expected>
#include <iostream>
#include <string>

std::expected<int, std::string> to_int(const std::string& s) {
    if (s.empty() || s.find_first_not_of("0123456789") != std::string::npos) return std::unexpected("bad number: '" + s + "'");
    return std::stoi(s);
}
std::expected<int, std::string> safe_div(int a, int b) {
    if (b == 0) return std::unexpected(std::string("division by zero"));
    return a / b;
}

int main() {
    for (auto [a, b] : {std::pair{"84", "4"}, {"84", "0"}, {"8x", "2"}}) {
        auto r = to_int(a).and_then([&](int x) { return to_int(b).and_then([x](int y) { return safe_div(x, y); }); })
                          .transform([](int q) { return q * 10; });
        std::cout << (r ? std::to_string(*r) : r.error()) << '\n';
    }
    return 0;
}
A 210 division by zero bad number: '8x'
B 21 division by zero bad number: '8x'
C 210 0 0
D 210 division by zero bad number: '2'
Question page & discussion
Question 14 Medium

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <map>
#include <print>
#include <string>
#include <vector>

int main() {
    std::vector<int> v{1, 2, 3};
    std::map<std::string, int> m{{"b", 2}, {"a", 1}};
    std::println("{}", v);
    std::println("{}", m);
    std::println("{:>5}|{:<5}|", "x", 3.5);
    std::print("no newline ");
    std::println("then one");
    return 0;
}
A [1, 2, 3] {"a": 1, "b": 2} x|3.5 | no newline then one
B 1 2 3 a 1 b 2 x|3.5 | no newline then one
C [1, 2, 3] {"b": 2, "a": 1} x | 3.5| no newline then one
D Compilation error
Question page & discussion
Question 15 Hard

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <type_traits>
#include <vector>

int main() {
    std::vector<int> v{1, 2, 3};
    auto a = v[0];                 // int
    decltype(v[0]) b = v[1];       // int& – decltype keeps the reference
    decltype(auto) c = v[2];       // int& as well
    int n = 5;
    decltype(n) d = n;             // int (declared type of the name)
    decltype((n)) e = n;           // int& – parenthesised expression is an lvalue
    a = 10; b = 20; c = 30; d = 40; e = 50;
    std::cout << v[0] << ' ' << v[1] << ' ' << v[2] << ' ' << n << ' ' << std::is_reference_v<decltype(b)> << '\n';
    return 0;
}
A 1 20 30 50 1
B 10 20 30 40 1
C 1 2 3 5 0
D 1 20 30 40 1
Question page & discussion
Question 16 Medium

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <any>
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::any> bag{1, 2.5, std::string("hi"), 'c'};
    int ints = 0;
    for (const auto& a : bag) {
        if (auto p = std::any_cast<int>(&a)) ints += *p;
        else if (a.type() == typeid(std::string)) std::cout << std::any_cast<std::string>(a) << ' ';
    }
    try { std::any_cast<int>(bag[1]); }
    catch (const std::bad_any_cast&) { std::cout << "bad cast "; }
    std::cout << ints << ' ' << bag[3].has_value() << '\n';
    return 0;
}
A hi bad cast 1 1
B hi bad cast 3 1
C hi 1 1
D bad cast hi 1 1
Question page & discussion
Question 17 Easy

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>

int main() {
    long long population = 1'428'627'663;
    int mask = 0b1111'0000;
    int hex = 0xFF'FF;
    double precise = 3.141'592;
    std::cout << population / 1'000'000 << ' ' << mask << ' ' << hex << ' ' << precise << '\n';
    return 0;
}
A 1428 240 65535 3.14159
B 1428 11110000 65535 3.141'592
C 1428627663 240 65535 3.14159
D 1428 240 255 3.14159
Question page & discussion
Question 18 Medium

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <string>

struct App {
    static inline int instances = 0;                    // C++17: defined right here, no .cpp definition
    static inline const std::string version = "2.1";
    App() { ++instances; }
};

inline int global_hits = 100;                           // one variable even if this header is included many times

int main() {
    App a, b;
    { App c; }
    global_hits += App::instances;
    std::cout << App::instances << ' ' << App::version << ' ' << global_hits << '\n';
    return 0;
}
A 3 2.1 103
B 2 2.1 102
C 3 2.1 100
D 0 2.1 100
Question page & discussion
Question 19 Medium

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <ranges>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> words{"modern", "c++", "is", "expressive", "and", "fast"};
    auto longer = words | std::views::filter([](const std::string& w) { return w.size() > 3; })
                        | std::views::transform([](const std::string& w) { return w.size(); })
                        | std::ranges::to<std::vector>();
    for (auto n : longer) std::cout << n << ' ';
    std::cout << "| " << longer.size() << '\n';
    return 0;
}
A 6 10 4 | 3
B modern expressive fast | 3
C 6 3 2 10 3 4 | 6
D 6 10 | 2
Question page & discussion
Question 20 Hard

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <string>
#include <type_traits>
#include <vector>

class Playlist {
    std::vector<std::string> songs_{"Intro", "Theme"};
public:
    // C++23: one function replaces the const and non-const overloads
    template <typename Self>
    auto& at(this Self& self, std::size_t i) { return self.songs_[i]; }
};

int main() {
    Playlist p;
    p.at(0) = "Opening";                                       // non-const: returns std::string&
    const Playlist& cp = p;
    std::cout << cp.at(0) << ' ' << cp.at(1) << ' '
              << std::is_const_v<std::remove_reference_t<decltype(cp.at(0))>> << '\n';   // const: const std::string&
    return 0;
}
A Opening Theme 1
B Intro Theme 0
C Opening Theme 0
D Compilation error
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links