📖 Scenario: You are creating a simple program to store and display fixed information about a book. This information should not change during the program.
🎯 Goal: Build a C++ program that uses constants and literals to store book details and then prints them.
📋 What You'll Learn
Use const keyword to declare constants
Use literals for string and numeric values
Print the constants to the console
💡 Why This Matters
🌍 Real World
Constants are used to store fixed information like configuration settings, fixed messages, or values that should not change during program execution.
💼 Career
Understanding constants and literals is essential for writing safe and readable code in software development, especially in embedded systems, finance, and configuration management.
Progress0 / 4 steps
1
Create constants for book title and author
Create two constants called bookTitle and bookAuthor with the exact string values "The Great Gatsby" and "F. Scott Fitzgerald" respectively.
C++
Hint
Use const char* const to declare string constants in C++.
2
Add a constant for the book's publication year
Add a constant integer called publicationYear and set it to the literal value 1925.
C++
Hint
Use const int to declare an integer constant.
3
Create a constant for the book price
Create a constant floating-point variable called bookPrice and set it to the literal value 10.99.
C++
Hint
Use const double for decimal numbers.
4
Print all constants to the console
Write std::cout statements to print bookTitle, bookAuthor, publicationYear, and bookPrice each on a new line.
C++
Hint
Use std::cout and std::endl to print each constant on its own line.
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.