Question 1 Easy The program below does not compile. Which statement correctly describes the problem? main.cpp Copy #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 View Answer & Explanation Correct Answer: Option B auto is compile-time deduction, not a dynamic type. Workspace Report Question page & discussion Report an error in this question Submit Report
Question 2 Easy The program below does not compile. Which statement correctly describes the problem? main.cpp Copy #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 View Answer & Explanation Correct Answer: Option B That strictness is the point of scoped enums. Workspace Report Question page & discussion Report an error in this question Submit Report
Question 3 Easy The program below does not compile. Which statement correctly describes the problem? main.cpp Copy #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 View Answer & Explanation Correct Answer: Option B Scoping prevents clashes such as two enums both having red. Workspace Report Question page & discussion Report an error in this question Submit Report
Question 4 Medium The program below does not compile. Which statement correctly describes the problem? main.cpp Copy #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 View Answer & Explanation Correct Answer: Option B C allows any order; C++ does not, because members are initialised in declaration order. Workspace Report Question page & discussion Report an error in this question Submit Report
Question 5 Medium The program below does not compile. Which statement correctly describes the problem? main.cpp Copy #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 View Answer & Explanation Correct Answer: Option B kb(4) would be fine – its argument is a constant. Workspace Report Question page & discussion Report an error in this question Submit Report
Question 6 Easy The program below does not compile. Which statement correctly describes the problem? main.cpp Copy #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 View Answer & Explanation Correct Answer: Option B There is no way to skip elements (use [[maybe_unused]] or std::get for one element). Workspace Report Question page & discussion Report an error in this question Submit Report
Question 7 Medium The program below does not compile. Which statement correctly describes the problem? main.cpp Copy #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 View Answer & Explanation Correct Answer: Option B The conversion string_view → std::string is explicit because it allocates. Workspace Report Question page & discussion Report an error in this question Submit Report
Question 8 Medium The program below does not compile. Which statement correctly describes the problem? main.cpp Copy #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 View Answer & Explanation Correct Answer: Option B Asking for a valid alternative that is not active (std::get<std::string>(v)) compiles but throws at run time. Workspace Report Question page & discussion Report an error in this question Submit Report
Question 9 Hard The program below does not compile. Which statement correctly describes the problem? main.cpp Copy #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 View Answer & Explanation Correct Answer: Option B Returning an iterator or pointer is the usual modern alternative. Workspace Report Question page & discussion Report an error in this question Submit Report
Question 10 Medium The program below does not compile. Which statement correctly describes the problem? main.cpp Copy #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 View Answer & Explanation Correct Answer: Option B auto a = 1, b = 2; is fine because both are int. Workspace Report Question page & discussion Report an error in this question Submit Report