C++ Programming Questions and Answers – vtable and vptr

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “vtable and vptr”.

1. What is vtable in C++?
a) Lookup table to resolve function calls in dynamic manners
b) Lookup table to resolve function calls in static manners
c) Lookup table to see which are the functions available for calls throughout the program
d) Lookup table to check how many functions are there int he program
View Answer

Answer: a
Explanation: vtable is a lookup table that is used to resolve the function calls in dynamic/late binding manners.

2. Which classes can have vtable?
a) Classes having friend functions
b) Classes having virtual functions
c) Classes having static functions
d) All classes have a vtable
View Answer

Answer: b
Explanation: Classes having virtual functions only need vtable because in those cases only we need to resolve function calls in a dynamic manner.

3. What is the full form of vtable?
a) V type table
b) Vector table
c) Virtual table
d) Virtual-vector table
View Answer

Answer: c
Explanation: Full form of vtable is a virtual table. This is called so because it stores the information about virtual functions of a class.
advertisement
advertisement

4. What is vptr?
a) A hidden pointer in a class that points to a virtual table of that class
b) A hidden pointer in a class that points to virtual functions of that class
c) A hidden pointer in a class that points to virtual members of the class of that class
d) A pointer in a class that points to other class
View Answer

Answer: a
Explanation: vptr is a hidden pointer available with classes which are used to point to the virtual table of a class.

5. What is the full form of vptr?
a) Vector Pointer
b) Virtual Pointer
c) V type Pointer
d) Virtual-vector Pointer
View Answer

Answer: b
Explanation: vptr is abbreviated for a virtual pointer which is used to point virtual tables of a class.

6. vptr is _______________
a) a real pointer
b) like this pointer of class
c) passed as a parameter to all functions of class
d) used to resolve self-references
View Answer

Answer: a
Explanation: Unlike this pointer, vptr is a real pointer that points to the virtual table of a class.

7. What will be the output of the following C++ code?

advertisement
#include <iostream>
#include<type_traits>
using namespace std;
class Base
{
    public:
};
int main()
{
	Base b;
	cout<<sizeof(b);
	return 0;
}

a) 16
b) 4
c) 8
d) 1
View Answer

Answer: d
Explanation: As the class is simple containing no variables therefore no member that requires size so the size of class is 1 hence the output is 1.
advertisement

8. What will be the output of the following C++ code?

#include <iostream>
#include<type_traits>
using namespace std;
class Base
{
  public:
    void function1() {};
    void function2() {};
};
int main()
{
	Base b;
	cout<<sizeof(b);
	return 0;
}

a) 16
b) 4
c) 1
d) 8
View Answer

Answer: c
Explanation: In ths case the class has two member functions but no variable sinside classes which requires space hence the size of class is 1 ans so the output.

9. What will be the output of the following C++ code?

#include <iostream>
#include<type_traits>
using namespace std;
class Base
{
   public:
    virtual void function1() {};
    virtual void function2() {};
};
int main()
{
	Base b;
	cout<<sizeof(b);
	return 0;
}

a) 1
b) 4
c) 8
d) 16
View Answer

Answer: c
Explanation: This class has two virtual functions defined. A class having virtual functions by default has a real pointer vptr. Therefore though not mentioned the class has a real pointer ptr which is of size 8 hence the output is 8. The pointer size differs depending on the system, therefore the output may vary in different systems.

10. Given below classes which of the following are the possible row entries in vtable of Base class?

class Base
{
    public:
    virtual void function1() {};
    virtual void function2() {};
};
class D1: public Base
{
    public:
    virtual void function1() {};
};
class D2: public Base
{
    public:
    virtual void function2() {};
};

a) Base::function1() and Base::function2()
b) Base::function1() and D1::function2()
c) D1::function1() and Base::function2()
d) D1::function1() and D1::function2() or D2::function1() and D2::function2()
View Answer

Answer: a
Explanation: Base class cannot access the members of derived classes therefore there is no conflict of function in Base class. The vtable of this class will contain two entries for both the virtual functions Base::function1() and Base::function2(). Here Base:: tells that the call will use the definition of Base class.

11. Given below classes which of the following are the possible row entries in vtable of D1 class?

class Base
{
    public:
    virtual void function1() {};
    virtual void function2() {};
};
class D1: public Base
{
    public:
    virtual void function1() {};
};
class D2: public Base
{
    public:
    virtual void function2() {};
};

a) Base::function1() and Base::function2()
b) D1::function1() and Base::function2()
c) Base::function1() and D1::function2()
d) D1::function1() and D1::function2()
View Answer

Answer: b
Explanation: In this program as D1 class is inherited from Base class it inherits both the virtual function of Base class but as the class has not overriden the function2() therefore the Base class definition of function2() will be followed. Therefore the class will have 2 entries corresponding to each function as D1::function1() because it is defined in Class D1 and Base::function2() because function2() is not defined in Class D1.

12. Given below classes which of the following are the possible row entries in vtable of D2 class?

class Base
{
    public:
    virtual void function1() {};
    virtual void function2() {};
};
class D1: public Base
{
    public:
    virtual void function1() {};
};
class D2: public Base
{
    public:
    virtual void function2() {};
};

a) Base::function1() and Base::function2()
b) D2::function1() and Base::function2()
c) Base::function1() and D2::function2()
d) D2::function1() and D2::function2()
View Answer

Answer: c
Explanation: In this program as D2 class is inherited from Base class it inherits both the virtual function of Base class but as the class has not overriden the function1() therefore the Base class definition of function1() will be followed. Therefore the class will have 2 entries corresponding to each function as Base::function1() because it is not defined in Class D2 and D2::function2() because function2() is defined in Class D2.

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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.