Challenge - 5 Problems
Master of Constants and Literals
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of const and literal usage
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { const int x = 5; constexpr int y = 10; int z = x + y; std::cout << z << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that const and constexpr values can be used in expressions.
✗ Incorrect
The const int x and constexpr int y are both constants. Adding them results in 15, which is printed.
❓ Predict Output
intermediate2:00remaining
Integer literal suffix effect
What is the output of this C++ code?
C++
#include <iostream> int main() { auto a = 10u; auto b = 5; std::cout << (a - b) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the types of a and b and how subtraction works.
✗ Incorrect
a is unsigned int (10u), b is int (5). Subtracting 5 from 10u results in unsigned arithmetic, which can cause wrap-around if b is larger. Here, 10u - 5 is 5, so the output is 5.
❓ Predict Output
advanced2:00remaining
Floating-point literal precision
What is the output of this C++ code snippet?
C++
#include <iostream> #include <iomanip> int main() { float f = 1.23456789f; double d = 1.23456789; std::cout << std::setprecision(9) << f << " " << d << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Float has less precision than double.
✗ Incorrect
The float variable loses precision and prints 1.23456788, while double keeps 1.23456789.
❓ Predict Output
advanced2:00remaining
Character literal and integer conversion
What is the output of this C++ code?
C++
#include <iostream> int main() { char c = 'A'; int i = c + 1; std::cout << i << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Character literals are stored as integer ASCII codes.
✗ Incorrect
'A' has ASCII code 65, adding 1 results in 66 printed as integer.
🧠 Conceptual
expert2:00remaining
Constant expression evaluation
Which of the following declarations is NOT a valid constant expression in C++?
Attempts:
2 left
💡 Hint
Constant expressions must be evaluable at compile time.
✗ Incorrect
rand() is a runtime function, so constexpr int z = rand(); is invalid as a constant expression.