Questions and Answers


  1. What is the output of this program?
    #include <iostream>
    #include <typeinfo>
    #include <exception>
    using namespace std;
    class BaseClass
    {
    virtual void f(){}
    };
    class DerivedClass : public BaseClass {};
    int main ()
    {
    try
    {
    BaseClass* ptr1 = new BaseClass;
    BaseClass* ptr2 = new DerivedClass;
    cout << typeid(*ptr1).name() << " ";
    cout << typeid(*ptr2).name();
    }
    catch (exception& excep)
    {
    cout << "Exception: " << excep.what() << endl;
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    In this program, We apply the typeid to the polymorphic class.


  1. What is the output of this program?
    #include <iostream>
    #include <typeinfo>
    using namespace std;
    int main ()
    {
    int * num1;
    int num2;
    num1 = 0; num2 = 3;
    if (typeid(num1) != typeid(num2))
    {
    cout << typeid(num1).name() << " ";
    cout << typeid(num2).name();
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    In this program, We are finding the typeid of the given variables.



  1. What is the output of this program?
    #include <iostream>
    #include <exception>
    using namespace std;
    class BaseClass
    {
    virtual void dummy() {}
    };
    class DerivedClass: public BaseClass
    {
    int num;
    };
    int main ()
    {
    try
    {
    BaseClass * ptr1 = new DerivedClass;
    BaseClass * ptr2 = new BaseClass;
    DerivedClass * ptr3;
    ptr3 = dynamic_cast(ptr1);
    if (ptr3 == 0)
    cout << "Null pointer on first type-cast" << endl;
    ptr3 = dynamic_cast(ptr2);
    if (ptr3 == 0)
    cout << "Null pointer on second type-cast" << endl;
    }
    catch (exception& excep)
    {
    cout << "Exception: " << excep.what();
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    In this program, We apply the dynamic cast to ptr3. Based on the value in the ptr3, it produces the output.


  1. To which type of class, We can apply RTTI?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    RTTI is available only for classes which are polymorphic, which means they have at least one virtual method.



  1. Which operators are part of RTTI?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    The dynamic_cast<> operation and typeid operator in C++ are part of RTTI.