0
0
C++programming~20 mins

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

Choose your learning style9 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.