The user types abc for std::cin >> n; where n is an int. What happens (C++11 and later)?
Until clear() is called every further extraction fails immediately, which is how many infinite input loops happen.
Input/Output with cin and cout · Exercise 1 of 4
Twenty multiple-choice questions on C++ streams: cin and cout, manipulators, getline, stream states, string streams, std::format and fast I/O. Pick an option to check your answer instantly.
The user types abc for std::cin >> n; where n is an int. What happens (C++11 and later)?
Until clear() is called every further extraction fails immediately, which is how many infinite input loops happen.
Why are std::cin.clear() and std::cin.ignore(…, '\n') called after a failed read?
Calling only clear() would make the next read fail again on the same bad characters; calling only ignore() does nothing while the stream is in a fail state.
When does the loop while (std::cin >> x) { … } stop?
The extraction returns the stream, which converts to false once failbit or badbit is set.
What is the difference between std::cin.get(c) and std::cin >> c for a char c?
Use get when spaces and newlines matter, e.g. counting characters or reading a text verbatim.
How do std::cerr and std::clog differ?
Unbuffered cerr guarantees error messages appear even if the program crashes right after; clog is more efficient for large amounts of log output.
What is std::istringstream typically used for?
It is declared in <sstream>. A common pattern is getline a line from cin, then parse it with an istringstream.
What does std::ios::sync_with_stdio(false); do?
It is usually combined with std::cin.tie(nullptr), which stops cin from flushing cout before every read.
Which header declares std::print and std::println?
<print> is new in C++23. std::format, which only builds strings, is in <format> (C++20).
How do you produce a literal { character with std::format?
Braces are doubled: std::format("{{{}}}", 5) produces {5}.
What does std::cin.peek() do?
peek lets a parser decide how to read what comes next (a digit or a letter, say). It returns EOF when no character is available.
Timed online tests with instant scores and explanations.