← Quiz: Type Casting in C++: all 10 questions
Quiz: Type Casting in C++ · Question 1 of 10
What does this print? (code)
What does this print?
#include <iostream>
using namespace std;
int main() {
int a = 7, b = 2;
double x = a / b;
double y = static_cast<double>(a) / b;
cout << x << " " << y << "\n";
return 0;
}
- A.3.5 3.5
- B.3 3.5
- C.3 3
- D.3.5 3
Answer & explanation
Correct answer: 3 3.5
a / b is integer division (3) before it is stored in x; casting a first makes the division 7.0 / 2 = 3.5.
Can you get the rest right without peeking?
Take the timed Quiz: Type Casting in C++ quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →