Skills101
← All practice quizzes

Quiz: Type Casting in C++

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

Converting one type to another in C++. Implicit conversions, why C-style casts are risky, the four named casts with examples, upcasting and downcasting with dynamic_cast, and explicit constructors.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    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;
    }
    
  2. Question 2

    Which cast should you use to convert an int to a double?

  3. Question 3

    What does dynamic_cast<Manager *>(p) return if p points to an Engineer object (not a Manager)?

  4. Question 4

    Why does dynamic_cast need the base class to have at least one virtual function?

  5. Question 5

    What does this print?

    #include <iostream>
    using namespace std;
    int main() {
        double d = 9.99;
        int i = static_cast<int>(d);
        char c = static_cast<char>('a' + i);
        cout << i << " " << c << "\n";
        return 0;
    }
    
🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →