Interview Questions
Input/Output with cin and cout · Study material
C++ Input/Output – Interview Questions
1. What are cin and cout?
Global objects of type std::istream and std::ostream declared in <iostream>, connected to standard input and standard output.
2. What are << and >> called in stream I/O?
The insertion and extraction operators. They are overloaded for all built-in types and std::string, and return the stream so that calls can be chained.
3. What is the difference between cerr and clog?
Both write to standard error; cerr is unbuffered (immediate), clog is buffered (efficient for logs).
4. Why does getline return an empty string after cin >>?
>> stops before the newline and leaves it in the buffer; getline reads up to that newline immediately. Use std::ws or cin.ignore().
5. What is the difference between cin >> s and getline(cin, s)?
>> skips leading whitespace and reads one word; getline reads the whole line including spaces and discards the newline.
6. Which manipulators are not sticky?
std::setw – it applies only to the next output (or input) field. Fill, precision, base, alignment and notation remain in effect.
7. What happens when cin reads letters into an int?
The extraction fails, failbit is set, the variable becomes 0 (C++11) and all further reads fail until clear() is called; the bad characters stay in the buffer.
8. How do you read input until end of file?
while (std::cin >> x) { … } – the loop ends when a read fails, at end of input or on mismatched data.
9. What do sync_with_stdio(false) and cin.tie(nullptr) do?
The first stops synchronising C++ streams with C stdio, allowing independent buffering; the second stops cin from flushing cout before each input. Together they greatly speed up I/O.
10. What is a stringstream?
A stream that reads from or writes to a std::string (istringstream, ostringstream, stringstream in <sstream>). Used for parsing and building strings.
11. How does setprecision behave with and without fixed?
Without fixed it sets the number of significant digits; with fixed or scientific it sets the number of digits after the decimal point.
12. What is the difference between cin.get() and cin >> for a char?
cin.get(c) reads the next character even if it is a space or newline; cin >> c skips whitespace first.
13. What does cin.ignore(n, '\n') do?
Discards up to n characters, stopping after the first newline. With std::numeric_limits<std::streamsize>::max() it discards the rest of the line.
14. Is std::endl the same as '\n'?
No – endl writes a newline and flushes the buffer.
15. What advantages does std::format have over iostream manipulators?
Formats are compact and readable, checked at compile time, stateless (nothing sticky leaks), positional arguments are possible and user types can be made formattable.
16. What is std::quoted?
A manipulator that writes a string surrounded by quotes with embedded quotes escaped, and reads such a string back, spaces included.
17. How can you check whether a stream is in a good state?
Use it in a condition (if (std::cin)), or call good(), fail(), eof() and bad() to inspect individual flags.
18. What does cin.peek() return?
The next character without extracting it, or EOF if there is none.