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
✗ Incorrect
The const keyword is used to declare constants in C++.
What is the type of the literal 3.14 in C++?
Afloat or double
Bchar
Cint
Dbool
✗ Incorrect
The literal 3.14 is a floating-point number, usually double by default.
Which of these is a valid constant declaration?
Aconst int x = 5;
Bint const x;
Cconst x = 5;
Dint x const = 5;
✗ Incorrect
The correct syntax is const int x = 5; or int const x = 5;. Option C and D are invalid.
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
✗ Incorrect
Constants cannot be changed once assigned.
Which of these is a character literal in C++?
A"a"
B'a'
Ca
Dchar('a')
✗ Incorrect
Character literals are enclosed in single quotes, like '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.
Practice
(1/5)
1. Which of the following best describes a constant in C++?
easy
A. A fixed value that cannot be changed after initialization
B. A variable that can change its value anytime
C. A function that returns a fixed value
D. A pointer to a variable
Solution
Step 1: Understand the meaning of constant
A constant in C++ is a value that once set cannot be changed during program execution.
Step 2: Compare options with definition
A fixed value that cannot be changed after initialization matches this definition exactly, while others describe variables or functions.
Final Answer:
A fixed value that cannot be changed after initialization -> Option A
Quick Check:
Constant = fixed value [OK]
Hint: Constants never change value after being set [OK]
Common Mistakes:
Confusing constants with variables
Thinking constants are functions
Assuming pointers are constants
2. Which of the following is the correct way to declare a constant integer named MAX_SIZE with value 100 in C++?
easy
A. int MAX_SIZE = 100;
B. constant int MAX_SIZE = 100;
C. int const MAX_SIZE;
D. const int MAX_SIZE = 100;
Solution
Step 1: Recall syntax for constant declaration
In C++, the keyword const before the type declares a constant.
Step 2: Check each option
const int MAX_SIZE = 100; uses const int MAX_SIZE = 100; which is correct. int MAX_SIZE = 100; misses const, C is incomplete without initialization, D uses invalid keyword.
Final Answer:
const int MAX_SIZE = 100; -> Option D
Quick Check:
Use const before type for constants [OK]
Hint: Use 'const' keyword before type to declare constants [OK]
Common Mistakes:
Omitting 'const' keyword
Using 'constant' instead of 'const'
Not initializing the constant
3. What is the output of the following C++ code?
const int x = 5;
int y = 10;
std::cout << x + y << std::endl;
medium
A. Runtime error
B. 15
C. Syntax error
D. 510
Solution
Step 1: Understand the code variables
Variable x is a constant with value 5, y is 10.
Step 2: Calculate the expression output
The expression x + y equals 5 + 10 = 15, which is printed.
Final Answer:
15 -> Option B
Quick Check:
5 + 10 = 15 [OK]
Hint: Add constant and variable values normally [OK]
Common Mistakes:
Thinking constants cause errors
Concatenating numbers as strings
Expecting runtime errors
4. Identify the error in the following C++ code:
const int a = 10;
a = 20;
std::cout << a << std::endl;
medium
A. Incorrect use of std::cout
B. Missing semicolon after declaration
C. Cannot assign a new value to a constant variable
D. Variable 'a' not declared
Solution
Step 1: Analyze constant assignment
The variable a is declared as constant with value 10.
Step 2: Check reassignment
Trying to assign 20 to a is illegal because constants cannot change value.
Final Answer:
Cannot assign a new value to a constant variable -> Option C
Quick Check:
Constants cannot be reassigned [OK]
Hint: Constants cannot be changed after initialization [OK]
Common Mistakes:
Trying to reassign constants
Ignoring compiler error messages
Confusing constants with variables
5. You want to define a constant literal for the value of pi (3.14159) that cannot be changed and can be used throughout your program. Which of the following is the best way to do this in C++?
hard
A. const double pi = 3.14159;
B. #define pi 3.14159
C. float pi = 3.14159;
D. double const pi;
Solution
Step 1: Understand constant literal requirements
The value should be fixed, unchangeable, and precise (double is better than float).
Step 2: Evaluate options
const double pi = 3.14159; declares a constant double with initialization, which is best practice. float pi = 3.14159; is not constant, C uses macro which is less safe, D misses initialization.
Final Answer:
const double pi = 3.14159; -> Option A
Quick Check:
Use 'const double' with initialization for constants [OK]
Hint: Use 'const double' with value for precise constants [OK]
Common Mistakes:
Using non-const variables for constants
Using #define instead of const
Not initializing constants
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.