LearnFrenzy
Sign in Take a free test

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

Modern C++ Features (C++11 to C++23) Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each program misuses a modern C++ feature in a way the compiler rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    auto total;
    total = 42;
    std::cout << total << '\n';
    return 0;
}
A 42 is too large
B auto deduces the type from the initialiser, so a declaration without one has no type; write auto total = 42; or int total;
C auto needs a header
D total must be const
Question page & discussion
Question 2 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

enum class Priority { low = 1, high = 3 };

int main() {
    int weight = Priority::high * 10;
    std::cout << weight << '\n';
    return 0;
}
A high must be lowercase
B enum class values do not convert implicitly to int; write static_cast<int>(Priority::high) * 10
C Priority must start at 0
D weight must be Priority
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

enum class Light { red, yellow, green };

int main() {
    Light l = green;
    std::cout << (l == Light::green) << '\n';
    return 0;
}
A Light must be an int
B Enumerators of an enum class are scoped: write Light::green (or using enum Light; in C++20)
C green is a keyword
D l must be const
Question page & discussion
Question 4 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

struct Rect { int x = 0, y = 0, w = 1, h = 1; };

int main() {
    Rect r{.w = 10, .x = 5};
    std::cout << r.x + r.w << '\n';
    return 0;
}
A Rect needs a constructor
B In C++ designated initialisers must appear in member declaration order; write {.x = 5, .w = 10}
C Default member values are not allowed
D w must be first in the struct
Question page & discussion
Question 5 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

consteval int kb(int n) { return n * 1024; }

int main() {
    int n;
    std::cin >> n;
    std::cout << kb(n) << '\n';
    return 0;
}
A kb must return long
B A consteval function must be evaluated at compile time, but n is read at run time; use constexpr instead (which may run at run time)
C cin cannot read ints
D 1024 is too big
Question page & discussion
Question 6 Easy

The program below does not compile. Which statement correctly describes the problem?

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

std::tuple<int, int, int> rgb() { return {255, 128, 0}; }

int main() {
    auto [r, g] = rgb();
    std::cout << r << ' ' << g << '\n';
    return 0;
}
A tuple needs <utility>
B The tuple has 3 elements, so the structured binding must declare exactly 3 names: auto [r, g, b] = rgb();
C r and g must be int
D rgb must return a struct
Question page & discussion
Question 7 Medium

The program below does not compile. Which statement correctly describes the problem?

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

void append_suffix(std::string& s) { s += ".txt"; }

int main() {
    std::string_view name = "notes";
    append_suffix(name);
    std::cout << name << '\n';
    return 0;
}
A string_view cannot be printed
B A string_view is a read-only view and does not convert to std::string&; create a std::string from it (std::string s{name};) and modify that
C append_suffix must be a template
D .txt is invalid
Question page & discussion
Question 8 Medium

The program below does not compile. Which statement correctly describes the problem?

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

int main() {
    std::variant<int, std::string> v = 10;
    std::cout << std::get<double>(v) << '\n';
    return 0;
}
A v must be initialised with a string
B double is not one of the variant's alternatives (int, std::string), so std::get<double> does not compile
C variant needs at least three types
D get must use an index
Question page & discussion
Question 9 Hard

The program below does not compile. Which statement correctly describes the problem?

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

std::optional<int&> find_first_even(std::vector<int>& v) {
    for (int& x : v) if (x % 2 == 0) return x;
    return std::nullopt;
}

int main() {
    std::vector<int> v{3, 8, 5};
    if (auto r = find_first_even(v)) *r = 0;
    std::cout << v[1] << '\n';
    return 0;
}
A nullopt is not a value
B std::optional cannot hold a reference type (until C++26); return an int* (nullptr when absent) or std::optional<std::reference_wrapper<int>>
C The loop must use auto
D v must be const
Question page & discussion
Question 10 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    auto count = 10, average = 7.5;
    std::cout << count << ' ' << average << '\n';
    return 0;
}
A 7.5 needs an f suffix
B In a single auto declaration every variable must deduce the same type; 10 gives int and 7.5 gives double – declare them separately
C average is a reserved word
D auto cannot declare two variables
Question page & discussion
Up next · Exercise 4Modern C++ Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links