Examveda

What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
class base { virtual void dummy() {} };
class derived: public base { int a; };
int main () 
{
    try 
    {
        base * pba = new derived;
        base * pbb = new base;
        derived * pd;
        pd = dynamic_cast<derived*>(pba);
        if (pd == 0) 
            cout << "Null pointer on first type-cast" << endl;
        pd = dynamic_cast<derived*>(pbb);
        if (pd == 0) 
            cout << "Null pointer on second type-cast" << endl;
    } 
    catch (exception& e) 
    {
        cout << "Exception: " << e.what();
    }
    return 0;
}

A. Null pointer on first type-cast

B. Null pointer on second type-cast

C. Exception

D. Null pointer on third type-cast

Answer: Option B


Join The Discussion

Related Questions on C plus plus miscellaneous

What is the difference between '++i' and 'i++' in C++?

A. None of the above

B. They both have the same effect

C. '++i' increments the value of 'i' before returning it, while 'i++' increments the value of 'i' after returning it

D. '++i' increments the value of 'i' after returning it, while 'i++' increments the value of 'i' before returning it