0
0
C++programming~5 mins

Constants and literals in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a constant in C++?
A constant is a value that cannot be changed after it is set. It is declared using the const keyword.
Click to reveal answer
beginner
What is a literal in C++?
A literal is a fixed value written directly in the code, like 42, 3.14, or 'a'.
Click to reveal answer
beginner
How do you declare a constant integer with value 10 in C++?
You write: <code>const int number = 10;</code> This means <code>number</code> cannot be changed later.
Click to reveal answer
intermediate
What is the difference between a constant and a literal?
A literal is a fixed value written in code. A constant is a named variable that holds a fixed value and cannot be changed.
Click to reveal answer
intermediate
Why use constants instead of literals directly in code?
Constants make code easier to read and maintain. If the value changes, you only update it in one place.
Click to reveal answer
Which keyword declares a constant in C++?
Astatic
Bvar
Clet
Dconst
What is the type of the literal 3.14 in C++?
Afloat or double
Bchar
Cint
Dbool
Which of these is a valid constant declaration?
Aconst int x = 5;
Bint const x;
Cconst x = 5;
Dint x const = 5;
Can you change the value of a constant after it is set?
ANo, constants cannot be changed
BOnly inside functions
CYes, anytime
DOnly if declared static
Which of these is a character literal in C++?
A"a"
B'a'
Ca
Dchar('a')
Explain what constants and literals are in C++ and how they differ.
Think about fixed values and named fixed values.
You got /3 concepts.
    Describe why using constants is better than using literals directly in your code.
    Imagine changing a value used many times.
    You got /3 concepts.