Bird
Raised Fist0
C++programming~20 mins

Basic formatting in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Basic formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
A3.1416
B3.14159
C3.141
D3.142
Attempts:
2 left
💡 Hint
Look at how many digits are shown after the decimal point with setprecision.
❓ Predict Output
intermediate
2: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;
}
A00042
B0042
C43
D 42
Attempts:
2 left
💡 Hint
setw sets the total width, setfill sets the padding character.
❓ Predict Output
advanced
2: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;
}
A****FF
B***FF
C***0FF
DFF0***
Attempts:
2 left
💡 Hint
hex converts to base 16, uppercase makes letters capital, setw and setfill control width and fill.
❓ Predict Output
advanced
2: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;
}
A
   123X
123   X
B
123   X
   123X
C
123X
123X
D
123   X
123   X
Attempts:
2 left
💡 Hint
left aligns text, right aligns text, setw sets width.
🧠 Conceptual
expert
2: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;
}
A
1
1
B
1
true
C
true
1
D
true
true
Attempts:
2 left
💡 Hint
std::boolalpha prints true/false, std::noboolalpha prints 1/0.

Practice

(1/5)
1. What does the setw manipulator do in C++ output formatting?
easy
A. Sets the number of decimal places for floating numbers
B. Sets the width of the next output field
C. Changes the text color in the console
D. Aligns text to the center automatically

Solution

  1. Step 1: Understand the purpose of setw

    The setw manipulator sets the width of the next output field in characters.
  2. Step 2: Compare with other manipulators

    setprecision controls decimal places, not width. Color and alignment are not handled by setw.
  3. Final Answer:

    Sets the width of the next output field -> Option B
  4. Quick Check:

    setw = width setting [OK]
Hint: Remember: setw sets width, setprecision sets decimals [OK]
Common Mistakes:
  • Confusing setw with setprecision
  • Thinking setw changes text color
  • Assuming setw aligns text automatically
2. Which of the following is the correct way to include setw in a C++ program?
easy
A. #include <string>
B. #include <format>
C. #include <iomanip>
D. #include <iostream>

Solution

  1. Step 1: Identify the header for setw

    The setw manipulator is defined in the iomanip header.
  2. Step 2: Check other headers

    iostream is for input/output streams but does not define setw. format and string are unrelated here.
  3. Final Answer:

    #include <iomanip> -> Option C
  4. Quick Check:

    setw needs iomanip header [OK]
Hint: Use <iomanip> for manipulators like setw [OK]
Common Mistakes:
  • Including only <iostream> and expecting setw to work
  • Confusing <format> with <iomanip>
  • Forgetting to include any header for setw
3. What is the output of this code?
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
  cout << setw(5) << 42 << endl;
  cout << setw(5) << 1234 << endl;
  return 0;
}
medium
A. 42 1234
B. 42 1234
C. 4321 24
D. 42 1234

Solution

  1. Step 1: Understand setw effect on output

    setw(5) sets the width to 5 characters, padding with spaces on the left by default.
  2. Step 2: Apply setw to numbers

    Number 42 is printed right-aligned in 5 spaces: 3 spaces + '42'. Number 1234 is 4 digits, so 1 space + '1234'.
  3. Final Answer:

    42 1234 -> Option A
  4. Quick Check:

    setw pads left with spaces [OK]
Hint: setw pads on left by default for numbers [OK]
Common Mistakes:
  • Assuming setw pads on the right
  • Ignoring spaces added by setw
  • Thinking setw truncates numbers
4. Identify the error in this code snippet:
#include <iostream>
using namespace std;

int main() {
  cout << setw(10) << 3.14159 << endl;
  return 0;
}
medium
A. No error, code runs fine
B. Using cout without std:: prefix
C. Incorrect use of endl
D. Missing <iomanip> header for setw

Solution

  1. Step 1: Check header includes

    The code uses setw but does not include <iomanip>, which is required.
  2. Step 2: Verify other parts

    cout is used with using namespace std;, so no prefix needed. endl usage is correct.
  3. Final Answer:

    Missing <iomanip> header for setw -> Option D
  4. Quick Check:

    setw needs iomanip header [OK]
Hint: Always include <iomanip> when using setw [OK]
Common Mistakes:
  • Forgetting to include <iomanip>
  • Thinking cout needs std:: with using namespace std;
  • Assuming endl usage causes error
5. How would you format the output of a floating-point number to always show exactly 3 decimal places and a total width of 8 characters, right-aligned?
hard
A. cout << fixed << setprecision(3) << setw(8) << number;
B. cout << setprecision(3) << setw(8) << number;
C. cout << setw(8) << fixed << number << setprecision(3);
D. cout << fixed << setw(3) << setprecision(8) << number;

Solution

  1. Step 1: Set fixed decimal places

    Use fixed and setprecision(3) to show 3 decimals exactly.
  2. Step 2: Set total width and alignment

    Use setw(8) to set total width to 8 characters, right-aligned by default.
  3. Final Answer:

    cout << fixed << setprecision(3) << setw(8) << number; -> Option A
  4. Quick Check:

    fixed + setprecision + setw(8) = correct format [OK]
Hint: Use fixed before setprecision, then setw for width [OK]
Common Mistakes:
  • Omitting fixed causes variable decimals
  • Placing setw before fixed and setprecision incorrectly
  • Using wrong values for setw or setprecision
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard