Type casting is the process of converting a variable from one data type to another, like changing an integer to a float.
Click to reveal answer
beginner
What is the difference between implicit and explicit type casting?
Implicit casting happens automatically by the compiler, while explicit casting is done manually by the programmer using cast operators.
Click to reveal answer
intermediate
How do you perform explicit type casting using C++ style casts?
You can use static_cast<new_type>(expression) to convert types explicitly and safely in C++.
Click to reveal answer
advanced
What does reinterpret_cast do in C++?
reinterpret_cast converts one pointer type to another, treating the bits as a different type without changing them. Use carefully as it can be unsafe.
Click to reveal answer
intermediate
Why should you avoid C-style casts in modern C++?
C-style casts are less safe because they can perform many conversions without warning. Modern C++ prefers static_cast, dynamic_cast, const_cast, and reinterpret_cast for clarity and safety.
Click to reveal answer
Which cast is safest for converting between related types in C++?
Astatic_cast
Breinterpret_cast
CC-style cast
Dconst_cast
✗ Incorrect
static_cast is safe for converting between related types like int to float or base class pointer to derived class pointer (with care).
What type of cast should you use to remove constness from a variable?
Areinterpret_cast
Bstatic_cast
Cconst_cast
Ddynamic_cast
✗ Incorrect
const_cast is used to add or remove const or volatile qualifiers.
What happens during implicit type casting?
AProgrammer manually converts types
BTypes cannot be converted
CProgram crashes
DCompiler automatically converts types
✗ Incorrect
Implicit casting is automatic conversion done by the compiler when safe.
Which cast is used to safely convert pointers in an inheritance hierarchy at runtime?
Adynamic_cast
Breinterpret_cast
Cstatic_cast
Dconst_cast
✗ Incorrect
dynamic_cast checks types at runtime and safely converts pointers or references in class hierarchies.
Why is reinterpret_cast considered unsafe?
AIt only works with integers
BIt treats bits as a different type without changing them
CIt changes the actual bits of data
DIt automatically converts constness
✗ Incorrect
reinterpret_cast treats the bits as a different type without changing them, which can cause undefined behavior if used incorrectly.
Explain the main types of type casting in C++ and when to use each.
Think about automatic vs manual conversions and safety.
You got /6 concepts.
Describe why modern C++ prefers specific cast operators over C-style casts.
Consider how each cast operator has a clear role.
You got /4 concepts.
Practice
(1/5)
1. What does static_cast<int>(3.14) do in C++?
easy
A. Leaves the value unchanged as 3.14
B. Converts the integer 3 to the floating-point number 3.14
C. Causes a runtime error
D. Converts the floating-point number 3.14 to the integer 3
Solution
Step 1: Understand static_cast usage
static_cast<int> converts a value to an integer type explicitly.
Step 2: Apply static_cast to 3.14
The floating-point number 3.14 is converted to integer by dropping the decimal part, resulting in 3.
Final Answer:
Converts the floating-point number 3.14 to the integer 3 -> Option D
Quick Check:
static_cast<int>(3.14) = 3 [OK]
Hint: static_cast truncates decimals when converting float to int [OK]
Common Mistakes:
Thinking it rounds the number instead of truncating
Confusing static_cast with dynamic_cast
Expecting a runtime error from static_cast
2. Which of the following is the correct syntax for casting a double variable d to int using static_cast?
easy
A. int i = (int)static_cast(d);
B. int i = static_cast<int>(d);
C. int i = static_cast(d, int);
D. int i = static_cast<int>d;
Solution
Step 1: Recall static_cast syntax
The correct syntax is static_cast<type>(expression).
Step 2: Match syntax with options
int i = static_cast<int>(d); matches the correct syntax: int i = static_cast<int>(d);.
Final Answer:
int i = static_cast<int>(d); -> Option B
Quick Check:
Correct static_cast syntax = int i = static_cast<int>(d); [OK]
Hint: Use angle brackets for type in static_cast [OK]
Common Mistakes:
Using parentheses instead of angle brackets for type
Placing type after variable instead of inside <>
Mixing C-style cast syntax with static_cast
3. What is the output of the following C++ code?
double x = 9.99;
int y = static_cast<int>(x);
std::cout << y << std::endl;
medium
A. 9.99
B. 10
C. 9
D. Compilation error
Solution
Step 1: Understand static_cast behavior on double to int
static_cast truncates the decimal part, so 9.99 becomes 9.
Step 2: Predict output of std::cout
The integer 9 is printed to the console.
Final Answer:
9 -> Option C
Quick Check:
static_cast<int>(9.99) = 9 [OK]
Hint: Casting double to int drops decimals, no rounding [OK]
Common Mistakes:
Expecting rounding to 10 instead of truncation
Confusing output type with original double
Thinking static_cast causes compile error here
4. Identify the error in this code snippet:
float f = 3.5f;
int i = static_cast<int> f;
std::cout << i;
medium
A. Missing parentheses after static_cast<int>
B. Cannot cast float to int using static_cast
C. Variable i is not declared
D. Missing semicolon after float declaration
Solution
Step 1: Check static_cast syntax
static_cast requires parentheses around the value to cast, like static_cast<int>(f).
Step 2: Identify missing parentheses
The code misses parentheses after static_cast<int>, causing syntax error.
Final Answer:
Missing parentheses after static_cast<int> -> Option A
Quick Check:
static_cast<int>(f) needs parentheses [OK]
Hint: Always use parentheses after static_cast type [OK]
Common Mistakes:
Omitting parentheses after static_cast
Thinking static_cast can't convert float to int
Ignoring compiler error messages
5. Given the code below, what will be the output?
int a = 65;
char c = static_cast<char>(a);
std::cout << c << std::endl;
Note: ASCII value 65 corresponds to 'A'.
hard
A. 'A'
B. Compilation error
C. 65
D. Undefined behavior
Solution
Step 1: Understand static_cast from int to char
static_cast converts the integer 65 to its corresponding ASCII character 'A'.
Step 2: Predict output of std::cout
Printing the char variable outputs the character 'A'.
Final Answer:
'A' -> Option A
Quick Check:
static_cast<char>(65) = 'A' [OK]
Hint: Casting int to char prints ASCII character [OK]
Common Mistakes:
Expecting numeric 65 to print instead of 'A'
Thinking casting int to char causes error
Confusing char with string output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.