📖 Scenario: You are working on a simple program that stores and displays information about a product's quantity and price. You want to use type modifiers to make sure the data uses the right amount of memory and behaves correctly.
🎯 Goal: Create variables with type modifiers to store product quantity and price, then display their values.
📋 What You'll Learn
Create an unsigned int variable called quantity with value 150.
Create a const float variable called price with value 19.99.
Create a long long int variable called total_stock with value 1000000.
Print the values of quantity, price, and total_stock.
💡 Why This Matters
🌍 Real World
Type modifiers help programmers use memory efficiently and prevent errors by restricting variable values, which is important in real-world software like inventory systems or financial apps.
💼 Career
Understanding type modifiers is essential for C++ developers to write safe, efficient, and clear code, especially in systems programming, embedded software, and performance-critical applications.
Progress0 / 4 steps
1
Create variables with type modifiers
Create an unsigned int variable called quantity and set it to 150. Also create a const float variable called price and set it to 19.99.
C++
Hint
Use unsigned int for quantity because it cannot be negative. Use const float for price because it should not change.
2
Add a long long int variable
Add a long long int variable called total_stock and set it to 1000000.
C++
Hint
Use long long int for total_stock because it can hold very large numbers.
3
Write code to print the variables
Write std::cout statements to print the values of quantity, price, and total_stock each on a new line.
C++
Hint
Use std::cout with << to print each variable followed by std::endl to move to the next line.
4
Run the program and check output
Run the program and ensure it prints the values 150, 19.99, and 1000000 each on its own line.
C++
Hint
The program should print the three values exactly as shown, each on its own line.
Practice
(1/5)
1. Which of the following type modifiers in C++ makes a variable value unchangeable after initialization?
easy
A. long
B. unsigned
C. volatile
D. const
Solution
Step 1: Understand the role of const
The const modifier makes a variable's value fixed after it is set.
Step 2: Compare with other modifiers
unsigned changes number sign, volatile affects optimization, long changes size, but none prevent value change.
Final Answer:
const -> Option D
Quick Check:
const = unchangeable value [OK]
Hint: const means value cannot be changed after set [OK]
Common Mistakes:
Confusing const with unsigned
Thinking volatile prevents changes
Assuming long affects constness
2. Which of the following declarations correctly uses the unsigned modifier for an integer variable named count?
easy
A. unsigned count int;
B. count unsigned int;
C. unsigned int count;
D. int count unsigned;
Solution
Step 1: Recall correct syntax for unsigned int
The correct order is unsigned int followed by the variable name.
Step 2: Check each option
unsigned count int; invalid order; count unsigned int; places variable before type, invalid; unsigned int count; correct; int count unsigned; invalid.
Final Answer:
unsigned int count; -> Option C
Quick Check:
unsigned int before variable name [OK]
Hint: Put unsigned before int and then variable name [OK]
Common Mistakes:
Placing unsigned after variable name
Mixing order of type and modifier
Using invalid syntax like unsigned count int
3. What is the output of the following C++ code?
unsigned int x = 10; int y = -5; if (x + y > 5) std::cout << "True"; else std::cout << "False";
medium
A. True
B. False
C. Compilation error
D. Undefined behavior
Solution
Step 1: Understand unsigned and signed addition
Adding signed -5 to unsigned 10: -5 promotes to unsigned (large value, e.g., 4294967291u on 32-bit).
Step 2: Calculate the result and compare
10u + 4294967291u = 4294967286u (wraps around mod 2^32). 4294967286u > 5u is true, so prints "True".
Final Answer:
True -> Option A
Quick Check:
unsigned + negative int promotes and wraps to large unsigned > 5 true [OK]
Hint: Mixing unsigned + signed negative promotes and wraps around [OK]
Common Mistakes:
Assuming -5 stays negative (ignores promotion)
Expecting True (ignores wraparound)
Thinking this causes compile error
4. Identify the error in this code snippet:
const int x; x = 5;
medium
A. Missing initialization of const variable
B. Incorrect use of int type
C. Assignment operator is wrong
D. No error, code is correct
Solution
Step 1: Check const variable initialization rules
A const variable must be initialized when declared.
Step 2: Analyze the code
Here, const int x; is declared without initialization, then assigned later, which is invalid.
Final Answer:
Missing initialization of const variable -> Option A
Quick Check:
const must be initialized immediately [OK]
Hint: const variables need immediate initialization [OK]
Common Mistakes:
Trying to assign const after declaration
Ignoring initialization requirement
Thinking assignment operator is wrong
5. You want to store a large positive number that can never be negative and might exceed 2 billion. Which type modifier combination is best for the variable score?
hard
A. const int score;
B. unsigned long score;
C. short unsigned score;
D. volatile int score;
Solution
Step 1: Understand the data requirements
We need a large positive number, no negatives, possibly > 2 billion.
Step 2: Match type modifiers to needs
unsigned ensures no negatives; long increases size to hold large values; const fixes value; short limits size; volatile affects optimization, not size.
Step 3: Choose best option
unsigned long fits the need best.
Final Answer:
unsigned long score; -> Option B
Quick Check:
unsigned long = large positive number [OK]
Hint: Use unsigned long for large positive numbers [OK]
Common Mistakes:
Choosing const which fixes value
Using short which limits size
Using volatile which doesn't affect size
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.