1. What is the Run-Time Type Information?
A. Information about an object’s data type at runtime
B. Information about the variables
C. Information about the given block
D. Information about the functions
Answer: A
Explanation:
: With the help of RTTI, We can get the information about the data type at the runtime.
2. Which operators are part of RTTI?
A. dynamic_cast()
B. typeid
C. both dynamic_cast<> & typeid
D. dynamic_cast[]
Answer: C
Explanation:
Explanation: The dynamic_cast<> operation and typeid operator in C++ are part of RTTI.
3. To which type of class, We can apply RTTI?
A. Encapsulation
B. Polymorphic
C. Derived
D. Static
Answer: B
Explanation:
: RTTI is available only for classes which are polymorphic, which means they have at least one virtual method.
4. 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(pbA.;
if (pd == 0)
cout << "Null pointer on first type-cast" << endl;
pd = dynamic_cast(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: B
Explanation:
: In this program, We apply the dynamic cast to pd. Based on the value in the pd, it produces the output.
5. What will be the output of the following C++ code?
#include <iostream>
#include <typeinfo>
using namespace std;
int main ()
{
int * a;
int b;
a = 0; b = 0;
if (typeid(A. != typeid(B.)
{
cout << typeid(A..name();
cout << typeid(B..name();
}
return 0;
}
A. Pi
B. i
C. Both pi & i
D. f
Answer: C
Explanation:
: In this program, We are finding the typeid of the given variables.
6. What will be the output of the following C++ code?
#include <iostream>
#include <typeinfo>
#include <exception>
using namespace std;
class base
{
virtual void f(){}
};
class derived : public base {};
int main ()
{
try
{
base* a = new base;
base* b = new derived;
cout << typeid(*A..name() << '\t';
cout << typeid(*B..name();
}
catch (exception& e)
{
cout << "Exception: " << e.what() << endl;
}
return 0;
}
A. base*
B. derived*
C. 4base and 7derived
D. Exception:derived
Answer: C
Explanation:
: In this program, We apply the typeid to the polymorphic class.
7. What will be the output of the following C++ code?
#include<typeinfo>
#include <iostream>
using namespace std;
class A
{
public:
virtual ~A();
};
int main()
{
A* a = NULL;
try
{
cout << typeid(*A..name() << endl;
}
catch (bad_typeiD.
{
cout << "Object is NULL" << endl;
}
}
A. int
B. float
C. double
D. object is NULL
Answer: D
Explanation:
: In this program, We are using the bad typeid() for a. So it is arising an exception.
8. What will be the output of the following C++ code?
#include<iostream>
using namespace std;
struct A
{
virtual void f()
{
cout << "Class A" << endl;
}
};
struct B : A
{
virtual void f()
{
cout << "Class B" << endl;
}
};
struct C : A
{
virtual void f()
{
cout << "Class C" << endl;
}
};
void f(A* arg)
{
B* bp = dynamic_cast(arg);
C* cp = dynamic_cast(arg);
if (bp)
bp -> f();
else if (cp)
cp -> f();
else
arg -> f();
};
int main()
{
A aobj;
C cobj;
A* ap = &cobj;
A* ap2 = &aobj;
f(ap);
f(ap2);
}
A. Class C
B. Class A
C. Both Class C & A
D. Class D
Answer: C
Explanation:
: In this program, We applied the dynamic casting to structure and produced the output.
9. What is meant by type_info?
A. Used to hold the type information returned by the typeid operator
B. Used to hold the type information returned by the dynamic_cast
C. Used to hold the type information returned by the static_cast
D. Used to hold the type information returned by the static_id
Answer: A
Explanation:
: type_info is used to hold the type information returned by the typeid operator.
10. At which time does the static_cast can be applied?
A. Compile-time construct
B. Runtime construct
C. Both Compile-time & Runtime construct
D. Runtime deconstruct
Answer: A
Explanation:
: Static_cast can be applied to only compile-time construct and not during run time construct.