- Assume the required headers are included. In each answer,
· represents one space. What does the following code print?
std::cout << std::setfill('.') << std::left << std::setw(6) << "cat"
<< '|' << std::right << std::setw(5) << 12 << '|' << 7; Answer: cat...|...12|7
When working with std::setw, std::setfill, std::left, and std::right, remember three critical rules: setfill and alignment (left/right) are sticky — they persist until changed — but setw resets to zero after each formatted output operation.
Here's what happens step by step. setfill('.') sets the padding character to '.' for the rest of the statement. std::left with setw(6) prints "cat" left-aligned in a field of width 6, padding the right side: cat.... Then '|' is printed directly — no width set, so no padding. Next, std::right (now active) with setw(5) prints 12 right-aligned in a field of 5: ...12. Another '|' is printed directly. Finally, 7 is printed with no active setw (it reset after printing 12), so it prints as just 7. The full output: **cat...|...12|7, which is answer A**.
Answer B reverses the alignment logic — ...cat would require right-alignment for "cat" and left-alignment for 12, but the code does the opposite. Answer C adds padding to 7, incorrectly assuming setw is sticky like setfill is — it's not. Answer D ignores formatting entirely, as if no setw or setfill were applied.
The key study tip: memorize which manipulators are sticky (setfill, left, right, setprecision) versus which reset after one use (setw). This distinction is a favorite exam trap.
- Assume the required headers are included. What does the following code print?
std::cout << std::showbase << std::uppercase << std::hex << 26
<< '|' << std::oct << 26 << '|' << std::dec << 26; Answer: 0X1A|032|26
When you see I/O manipulators chained together in C++, the key is understanding that they set persistent flags on the stream — each manipulator stays active for all subsequent output until changed.
Here's how to trace the code. std::showbase tells the stream to prefix bases (0x for hex, 0 for octal). std::uppercase converts hex letters and the 0X prefix to uppercase. std::hex switches to base-16, so 26 in hex is 1A — printed as 0X1A with both flags active. The '|' is just a character literal. Then std::oct switches to base-8; 26 in octal is 32, and showbase prepends a 0, giving 032. The flags from uppercase don't affect octal output visually. Finally, std::dec restores base-10, printing 26 plainly. The full output is 0X1A|032|26, which is A.
Choice B is wrong because C++ has no 0o octal prefix — that notation belongs to Python. The correct octal prefix under showbase is simply 0.
Choice C is wrong because after switching to std::dec, the stream prints in base-10, not hex. The manipulator change is real; 26 in decimal is 26, not 1A.
Choice D is wrong because it ignores showbase entirely, omitting both the 0X hex prefix and the 0 octal prefix, and also drops uppercase.
A good study habit: mentally "replay" the stream's flag state after each manipulator. Remember that showbase + uppercase specifically capitalizes the 0X prefix — 0x (lowercase) would appear without uppercase.
- Assume the required headers are included. What does the following code print?
std::cout << std::fixed << std::setprecision(2) << 9.876
<< '|' << std::defaultfloat << 9.876; Answer: 9.88|9.9
When working with stream formatting in C++, the key thing to remember is that format flags are sticky — they persist across subsequent output operations until explicitly changed. However, different flags interact in subtle ways.
std::fixed combined with std::setprecision(2) means "print exactly 2 digits after the decimal point." So 9.876 rounds to 9.88 — that part is straightforward and all four choices agree on it.
The interesting part is what happens after std::defaultfloat. This manipulator resets the float format back to the default style, but critically, it does not reset the precision. The precision value of 2 set by std::setprecision(2) remains in effect. However, in defaultfloat mode, precision means total significant digits, not digits after the decimal. With 2 significant digits, 9.876 becomes 9.9 — one digit before the decimal and one after, totaling two significant figures. That makes D the correct answer.
A (9.88|9.88) incorrectly assumes defaultfloat keeps the fixed-point behavior, so it just applies the same rounding both times. B (9.87|9.876) misapplies truncation instead of rounding for the first value, and assumes defaultfloat resets precision entirely, restoring the full representation. C (9.88|9.876) correctly handles the first value but wrongly assumes defaultfloat resets both the format and the precision, giving you the full default output.
A useful tip: on questions about C++ I/O manipulators, always ask yourself which flags are sticky and which reset others. defaultfloat resets the formatting mode but leaves precision alone — that asymmetry is a classic exam trap.
- Assume the required headers are included. What does the following code print?
std::cout << std::setfill('0') << std::internal << std::showpos
<< std::setw(7) << 42 << '|' << std::setw(5) << -7; Answer: +000042|-0007
When working with std::setw, std::setfill, std::internal, and std::showpos together, you need to think carefully about where padding is inserted and how each manipulator applies.
Here's what each manipulator does: std::showpos forces a + sign on positive numbers. std::internal places padding between the sign (or base prefix) and the digits — not after the value. std::setfill('0') makes that padding character '0'. Crucially, std::setw applies only to the next output operation, then resets to 0 — but setfill, internal, and showpos are sticky (they persist).
For std::setw(7) << 42: the value is +42 (3 characters with sign), so 4 zeros are inserted between the sign and digits, yielding +000042. For std::setw(5) << -7: the value is -7 (2 characters), so 3 zeros go between the minus and digit, yielding -0007. The '|' is printed as-is with no width. The full output is +000042|-0007, confirming D is correct.
Choice A (0000+42|000-7) places zeros before the sign — that's left-to-right fill without std::internal, not how it works. Choice B (+420000|-7000) looks like std::left alignment, padding after the digits. Choice C (000+42|00-7) is almost right but counts wrong — +42 needs width 7, which means 4 zeros, not 3, and -7 needs width 5, meaning 3 zeros, not 2.
A reliable study tip: memorize that std::internal is the only alignment flag that splits padding between the sign and digits. When you see it combined with setfill('0'), expect zero-padded signed numbers.
- Assume the required headers are included. What does the following code print?
std::cout << std::setfill('.') << std::right
<< std::setw(4) << "elephant" << '|' << "cat"
<< '|' << std::setw(5) << "cat"; Answer: elephant|cat|..cat
When working with std::setw and stream manipulators, the critical rule to remember is that setw applies only to the very next output operation — it resets to zero after a single use. Meanwhile, setfill and std::right are sticky: they persist for all subsequent output.
Walking through the code step by step confirms answer A. setfill('.') and std::right are set once and remain active throughout. The first output is "elephant" with setw(4). Since "elephant" has 8 characters — already wider than 4 — no padding is applied and the full string prints. Then '|' prints with no active setw (it expired), so it prints literally as |. Then "cat" prints with no setw, so it prints as cat. Then another '|' prints as |. Finally, setw(5) is applied to "cat" (3 characters), so 2 fill characters are prepended on the right-alignment side: ..cat. The full output is elephant|cat|..cat.
Answer B incorrectly assumes setw(4) truncates "elephant" to "elep" — setw never truncates; it only pads when the field width is larger than the content. Answer C wrongly applies setw(4) to the '|' or "cat" after "elephant", forgetting that setw was already consumed. Answer D applies padding to the wrong side, treating the alignment as left instead of right — but std::right was explicitly set.
Your study tip: memorize the "sticky vs. one-shot" rule. setw is the only standard manipulator that resets after one use — everything else sticks until changed.
- Assume the required headers are included. What does the following code print?
bool ready = true;
bool failed = false;
std::cout << std::boolalpha << ready << '|'
<< std::noboolalpha << failed << '|' << ready; Answer: true|0|1
When you see std::boolalpha and std::noboolalpha in the same output statement, the key insight is that these are stream manipulators that act as toggles — they change the stream's formatting state at the exact point they appear, and that state persists for subsequent output until changed again.
Here's how the output builds step by step: std::boolalpha is applied first, switching the stream to text mode, so ready prints as true. Then std::noboolalpha is applied mid-statement, switching back to numeric mode, so failed prints as 0. Crucially, the stream remains in numeric mode for the final ready, which also prints as 1. The full output is true|0|1, confirming C is correct.
A (true|false|true) is tempting because it assumes boolalpha stays active for the entire expression — but noboolalpha explicitly turns it off halfway through. B (1|0|1) assumes boolalpha never takes effect, as if the manipulator only applies to one value or is ignored entirely. D (true|false|1) is the most seductive trap: it correctly applies boolalpha to ready and noboolalpha to affect ready at the end, but mistakenly prints failed as false — forgetting that noboolalpha takes effect before failed is output, not after.
A good study tip: treat stream manipulators like light switches. When you toggle one mid-expression, everything after that point in the stream is affected. Practice tracing output left-to-right, applying each manipulator the moment you encounter it.
- Assume the required headers are included and the stream initially uses
std::defaultfloat. What does the following code print?
std::cout << std::setprecision(3) << 123456 << '|'
<< 12.3456 << '|' << std::fixed << 12.3456; Answer: 123456|12.3|12.346
When working with std::setprecision and floating-point formatting in C++, you need to track two things: what the precision setting means in each format mode, and whether manipulators are sticky (persistent) or one-shot.
std::setprecision(3) is sticky — it stays in effect for all subsequent output. However, its interpretation changes depending on the active float format. In std::defaultfloat mode (the initial state), precision means the total number of significant digits. In std::fixed mode, precision means digits after the decimal point.
Walking through the code: first, 123456 is an integer, so float precision has no effect — it prints as 123456. Next, 12.3456 is printed in defaultfloat with precision 3, meaning 3 significant digits total, giving 12.3. Then std::fixed is applied, switching the interpretation of precision to 3 digits after the decimal point. Now 12.3456 rounds to 12.346. So the full output is 123456|12.3|12.346, confirming B.
Choice A is wrong because it assumes setprecision(3) truncates the integer 123456 to 123 — but precision manipulators only affect floating-point output. Choice C is wrong because it applies fixed-style rounding (3 decimal places) to the second number too, ignoring that std::fixed wasn't active yet at that point. Choice D is wrong on two counts: it incorrectly truncates the integer and misrounds the final value.
The key study tip: always ask yourself which format flag is currently active when a floating-point value is printed, since defaultfloat and fixed interpret setprecision differently. Also remember that precision never affects integer types.