Challenge - 5 Problems
Basic formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of formatted floating-point number
What is the output of this C++ code snippet?
C++
#include <iostream> #include <iomanip> int main() { double pi = 3.1415926535; std::cout << std::fixed << std::setprecision(3) << pi << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Look at how many digits are shown after the decimal point with setprecision.
✗ Incorrect
std::fixed with std::setprecision(3) formats the number to show exactly 3 digits after the decimal point, rounding the value. 3.1415926535 rounded to 3 decimals is 3.142.
❓ Predict Output
intermediate2:00remaining
Output of formatted integer with width and fill
What is the output of this C++ code?
C++
#include <iostream> #include <iomanip> int main() { int num = 42; std::cout << std::setw(5) << std::setfill('0') << num << std::endl; return 0; }
Attempts:
2 left
💡 Hint
setw sets the total width, setfill sets the padding character.
✗ Incorrect
setw(5) sets the total width to 5 characters. setfill('0') fills empty spaces with zeros on the left by default. So 42 becomes 00042.
❓ Predict Output
advanced2:00remaining
Output of mixed formatting flags
What is the output of this C++ code snippet?
C++
#include <iostream> #include <iomanip> int main() { int num = 255; std::cout << std::hex << std::uppercase << std::setw(6) << std::setfill('*') << num << std::endl; return 0; }
Attempts:
2 left
💡 Hint
hex converts to base 16, uppercase makes letters capital, setw and setfill control width and fill.
✗ Incorrect
255 in hex is FF. setw(6) means total width 6, setfill('*') fills with '*'. The output is ****FF because the number is right aligned and the fill fills the left side.
❓ Predict Output
advanced2:00remaining
Output of left and right alignment with width
What is the output of this C++ code?
C++
#include <iostream> #include <iomanip> int main() { std::cout << std::left << std::setw(6) << 123 << "X" << std::endl; std::cout << std::right << std::setw(6) << 123 << "X" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
left aligns text, right aligns text, setw sets width.
✗ Incorrect
First line uses std::left with setw(6), so 123 is printed left aligned in 6 spaces, then X. Second line uses std::right with setw(6), so 123 is right aligned in 6 spaces, then X.
🧠 Conceptual
expert2:00remaining
Effect of std::boolalpha on boolean output
Which option shows the output of this C++ code?
C++
#include <iostream> int main() { bool flag = true; std::cout << std::boolalpha << flag << std::endl; std::cout << std::noboolalpha << flag << std::endl; return 0; }
Attempts:
2 left
💡 Hint
std::boolalpha prints true/false, std::noboolalpha prints 1/0.
✗ Incorrect
std::boolalpha causes bool values to print as 'true' or 'false'. std::noboolalpha resets this, so bool prints as 1 or 0. So first line prints 'true', second line prints '1'.