This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Run Time Type Information”.
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
View Answer
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[]
View Answer
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
View Answer
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<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
View Answer
Explanation: In this program, We apply the dynamic cast to pd. Based on the value in the pd, it produces the output.
Output:
$ g++ rtti.cpp $ a.out Null pointer on second type-cast
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
View Answer
Explanation: In this program, We are finding the typeid of the given variables.
Output:
$ g++ rtti1.cpp $ a.out Pii
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
View Answer
Explanation: In this program, We apply the typeid to the polymorphic class.
Output:
$ g++ rtti2.cpp $ a.out 4base 7derived
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
View Answer
Explanation: In this program, We are using the bad typeid() for a. So it is arising an exception.
Output:
$ g++ rtti3.cpp $ a.out object is NULL
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<B*>(arg);
C* cp = dynamic_cast<C*>(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
View Answer
Explanation: In this program, We applied the dynamic casting to structure and produced the output.
Output:
$ g++ rtti4.cpp $ a.out Class C Class A
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
View Answer
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
View Answer
Explanation: Static_cast can be applied to only compile-time construct and not during run time construct.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Check C++ Books
- Apply for C++ Internship
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check Programming Books