What are std::cin and std::cout?
They are global stream objects created before main starts. Being ordinary objects, they can be passed to functions as std::istream& and std::ostream&.
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.
What are std::cin and std::cout?
They are global stream objects created before main starts. Being ordinary objects, they can be passed to functions as std::istream& and std::ostream&.
In std::cout << x, what is << called?
<< inserts data into an output stream; >> extracts data from an input stream. Both are overloads of the shift operators.
Why does std::cout << a << b; work?
The expression is ((std::cout << a) << b). Your own operator<< overloads must also return std::ostream& to support chaining.
What does std::cin >> x do with spaces and newlines before the value?
Formatted extraction skips leading whitespace (unless std::noskipws is set), so values can be separated by any mix of spaces, tabs and newlines.
Which call reads a whole line such as Rahul Kumar Sharma into a std::string?
std::cin >> name stops at the first space and would read only "Rahul".
After std::cin >> age;, the next std::getline(std::cin, name); returns an empty string. Why?
Discard the leftover newline first: std::getline(std::cin >> std::ws, name); or std::cin.ignore();.
What does std::cin >> std::ws do?
std::ws is an input manipulator that consumes whitespace (including newlines) up to the next non-space character.
Which header declares std::setw, std::setfill and std::setprecision?
Manipulators that take arguments are in <iomanip>. Argument-less ones such as std::fixed, std::hex and std::endl come with <iostream>.
Which manipulator affects only the next value written?
The width is reset to 0 after every formatted output. Precision, base and alignment stay until changed.
With std::fixed set, what does std::setprecision(2) control?
Without fixed (default notation) precision counts significant digits: setprecision(2) prints 3.14159 as 3.1.
Timed online tests with instant scores and explanations.