AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content

16 Modern C++ / Checkpoint

Checkpoint — Modern C++

25 min · +25 XP · 3 exercises · 15 questions

Exercises0/3
Quiz · 70% to clear15 q

This checkpoint covers the whole module: the standards and what compiles here, auto, decltype and structured bindings, constexpr/consteval/constinit and compile-time tables, the vocabulary types from std::tuple to std::span and <chrono>, and the C++20 set — concepts, ranges, <=>, std::format — with C++23 as reading.

How it works. Fifteen questions and three programs. You need 70% on the questions and every program accepted to clear the module. You can retake it as often as you like; your best score counts.

Before you start, make sure you can answer these from memory:

  • What does auto drop from the initialiser's type, and how do auto& and const auto& differ?
  • Why is auto [k, v] = *it; a copy, and what makes it a reference?
  • What is the difference between const, constexpr, consteval and constinit?
  • Why can a consteval function not be called with a loop variable from a constexpr function?
  • Why does std::chrono::seconds s = 1500ms; fail to compile, and what does duration_cast do instead?
  • Which of std::format, std::print, std::expected and std::span compile under -std=c++20?

The three programs are a duration ledger that sums <chrono> durations, a day-of-year calculator on a constexpr prefix-sum table and std::optional, and a concept-constrained statistics function over a std::span printed with std::format. Read the input format carefully before writing the parsing.

Common questions

Why is auto [k, v] = *it; a copy?

Plain auto in a structured binding copies the whole object into a hidden variable, and k and v name that copy's members. Write auto& [k, v] = *it; to bind the original so writes go through, or const auto& [k, v] to read without copying.

What is the difference between const, constexpr, consteval and constinit?

const is read-only after initialisation, possibly at run time. constexpr is a compile-time constant, or a function that may run at compile time. consteval marks a function that must run at compile time. constinit forces a static variable to be initialised at compile time while leaving it mutable.

Which of std::format, std::print, std::expected and std::span compile under -std=c++20?

std::format and std::span are C++20 and compile under -std=c++20. std::print and std::expected are C++23 additions and do not exist in C++20 mode, so code that uses them fails to compile there.

Modern C++ Quiz: auto, constexpr and C++20 Practice Test

Test your modern C++ with 15 questions and three programs on auto, structured bindings, constexpr, std::span, chrono durations, concepts and std::format.

  • Course: C++ study plan
  • Module: Modern C++
  • Kind: Checkpoint — cleared at 70%
  • Reading time: 25 min
  • Runtime: Clang 18 · C++20

Checkpoint — Modern C++ is the checkpoint that closes the Modern C++ module: a graded quiz and whole-program exercises, passed at 70%.

Instructions

This checkpoint covers the whole module: the standards and what compiles here, auto, decltype and structured bindings, constexpr/consteval/constinit and compile-time tables, the vocabulary types from std::tuple to std::span and <chrono>, and the C++20 set — concepts, ranges, <=>, std::format — with C++23 as reading.

How it works. Fifteen questions and three programs. You need 70% on the questions and every program accepted to clear the module. You can retake it as often as you like; your best score counts.

Before you start, make sure you can answer these from memory:

  • What does auto drop from the initialiser's type, and how do auto& and const auto& differ?
  • Why is auto [k, v] = *it; a copy, and what makes it a reference?
  • What is the difference between const, constexpr, consteval and constinit?
  • Why can a consteval function not be called with a loop variable from a constexpr function?
  • Why does std::chrono::seconds s = 1500ms; fail to compile, and what does duration_cast do instead?
  • Which of std::format, std::print, std::expected and std::span compile under -std=c++20?

The three programs are a duration ledger that sums <chrono> durations, a day-of-year calculator on a constexpr prefix-sum table and std::optional, and a concept-constrained statistics function over a std::span printed with std::format. Read the input format carefully before writing the parsing.

Common questions

Why is auto [k, v] = *it; a copy?

Plain auto in a structured binding copies the whole object into a hidden variable, and k and v name that copy's members. Write auto& [k, v] = *it; to bind the original so writes go through, or const auto& [k, v] to read without copying.

What is the difference between const, constexpr, consteval and constinit?

const is read-only after initialisation, possibly at run time. constexpr is a compile-time constant, or a function that may run at compile time. consteval marks a function that must run at compile time. constinit forces a static variable to be initialised at compile time while leaving it mutable.

Which of std::format, std::print, std::expected and std::span compile under -std=c++20?

std::format and std::span are C++20 and compile under -std=c++20. std::print and std::expected are C++23 additions and do not exist in C++20 mode, so code that uses them fails to compile there.

Exercises

Duration ledger

Read an integer n, then n durations, one per line, each an integer immediately followed by a unit: ms, s, min or h (250ms, 90s, 5min, 2h). Add them all up as a std::chrono::milliseconds — convert each token into the matching <chrono> type and let the implicit widening conversion do the rest; never multiply by 1000 by hand. Then split the total with duration_cast and duration subtraction and print total: <h>h <m>m <s>s <ms>ms, followed by seconds: <total in whole seconds>.

Input: n, then n tokens. Output: two lines.

Example: input 3 / 90s / 5min / 2h →

total: 2h 6m 30s 0ms
seconds: 7590

Day of the year at compile time

Using a constexpr std::array<int, 12> of month lengths for a non-leap year, build a second table START of thirteen prefix sums with a constexpr function — START[m] is the number of days before month m + 1, so START[0] == 0, START[2] == 59 and START[12] == 365 — and prove those three with a static_assert. Then write std::optional<int> dayOfYear(int month, int day) that returns std::nullopt when the month is not 1–12 or the day is not valid for that month, and otherwise the day's number in the year.

Read an integer n, then n lines of month day, and print <month>/<day> -> <day of year> or <month>/<day> -> invalid. Use an if with initialiser to test the optional.

Input: n, then n lines. Output: n lines.

Example: input 3 / 1 1 / 3 1 / 12 31 →

1/1 -> 1
3/1 -> 60
12/31 -> 365

Constrained statistics

Define a concept Number satisfied by any integral or floating-point type, and a function template summarise constrained by it that takes a std::span<const T> and returns a std::tuple<T, T, double> of the smallest value, the largest value and the mean. Accumulate the sum in a long long when T is integral and in a double otherwise — if constexpr or std::conditional_t — so integer input never overflows.

The first token of the input is int or double; then comes n (at least 1) and n numbers of that kind. Read them into a std::vector of the matching type, call summarise on a span over it, bind the result with a structured binding, and print one line with std::format: for int, lo=<lo> hi=<hi> mean=<mean>; for double, the same with lo and hi to two decimals. The mean always has two decimals.

Input: the kind, n, then n numbers. Output: one line.

Example: input int 4 1 9 3 5 → lo=1 hi=9 mean=4.50; input double 3 1.5 9.25 2.75 → lo=1.50 hi=9.25 mean=4.50.

In this module: Modern C++

← C++20 and beyond — concepts, ranges, format and what is coming · Threads — std::thread, std::jthread and the join rule →