0
0
C++programming~20 mins

Constants and literals in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Constants and Literals
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
AUndefined behavior
B510
CCompilation error
D15
Attempts:
2 left
💡 Hint
Remember that const and constexpr values can be used in expressions.
Predict Output
intermediate
2: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;
}
ACompilation error
BUnsigned integer wrap-around value
C-5
D5
Attempts:
2 left
💡 Hint
Check the types of a and b and how subtraction works.
Predict Output
advanced
2: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;
}
A1.23456788 1.23456789
BCompilation error
C1.23456789 1.23456788
D1.23456789 1.23456789
Attempts:
2 left
💡 Hint
Float has less precision than double.
Predict Output
advanced
2: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;
}
AA1
BCompilation error
C66
D65
Attempts:
2 left
💡 Hint
Character literals are stored as integer ASCII codes.
🧠 Conceptual
expert
2:00remaining
Constant expression evaluation
Which of the following declarations is NOT a valid constant expression in C++?
Aconstexpr int z = rand();
Bconst int y = 10 * 2;
Cconstexpr int x = 5 + 3;
Dconstexpr int w = 7 - 4;
Attempts:
2 left
💡 Hint
Constant expressions must be evaluable at compile time.