MCQ On Virtual Functions in C++: Multiple Choice Questions and Answers on Virtual Functions in C++

MCQ on virtual functions in C++ with answers and explanations for placement tests and job interviews. This C++ MCQ on virtual functions is useful for the campus placement for all freshers including Engineering Students, MCA students, Computer and IT Engineers, etc.

Our C++ virtual functions quiz (C++ virtual functions multiple Choice Questions ) focuses on all areas of C++ virtual functions and its concept. We will regularly update the Quiz on the C++ virtual functions and most interesting thing is that questions come in a random sequence. So every time you will feel new questions.

You may have come across several C++ courses during your search for learning the C++ language. Our team of experts has carefully analyzed some C++ courses for you. You can check the courses, Trial of some courses is free.

 

Guideline of MCQ on virtual functions in C++:

This MCQ on virtual functions in C++ is intended for checking your knowledge of C++ virtual functions. It takes 20 minutes to pass the C++ quiz on virtual functions. If you don’t finish the C++ virtual functions quiz within the mentioned time, all the unanswered questions will count as wrong. Every unanswered question will count as wrong. Quiz on virtual functions in C++ has randomization features that give you a new question set at every attempt.

In this MCQ on virtual functions in C++, we have also implemented a feature that not allowed the user to see the next question or finish the quiz without attempting the current C++ virtual functions quiz.

1 votes, 5 avg

You have 20 minutes to take the MCQs on Virtual Functions in C++

Your time has been Over.


MCQ On Virtual Functions in C++

C++ MCQ

MCQ On Virtual Functions in C++

MCQ On Virtual Functions in C++: Multiple Choice Questions and Answers on Virtual Functions in C++

1 / 14

Output of following program

#include<iostream>
using namespace std;
 
class Base
{
public:
    virtual void show() { cout<<" In Base n"; }
};
 
class Derived: public Base
{
public:
    void show() { cout<<"In Derived n"; }
};
 
int main(void)
{
    Base *bp, b;
    Derived d;
    bp = &d;
    bp->show();
    bp = &b;
    bp->show();
    return 0;
}

 

1. 

In Base 
In Base

2. 

In Base 
In Derived

3. 

In Derived
In Derived

4. 

In Derived
In Base

2 / 14

#include<iostream>
using namespace std;
 
class Base
{
public:
    virtual void show() = 0;
};
 
int main(void)
{
    Base b;
    Base *bp;
    return 0;
}

 

3 / 14

#include <iostream>
using namespace std;
  
class A
{
public:
    virtual void fun() { cout << "A::fun() "; }
};
  
class B: public A
{
public:
   void fun() { cout << "B::fun() "; }
};
  
class C: public B
{
public:
   void fun() { cout << "C::fun() "; }
};
  
int main()
{
    B *bp = new C;
    bp->fun();
    return 0;
}

 

4 / 14

Can a constructor be virtual? Will the following program compile?

#include <iostream>
using namespace std;
class Base {
public:
  virtual Base() {}   
};
int main() {
   return 0;
}

 

5 / 14

Predict the output of following program.

#include<iostream>
using namespace std;
class Base
{
public:
    virtual void show() = 0;
};
 
class Derived : public Base { };
 
int main(void)
{
    Derived q;
    return 0;
}

 

6 / 14

Which of the following is true about virtual functions in C++.

7 / 14

#include<iostream>
using namespace std;
 
class Base
{
public:
    virtual void show() = 0;
};
 
class Derived: public Base
{
public:
    void show() { cout<<"In Derived n"; }
};
 
int main(void)
{
    Derived d;
    Base &br = d;
    br.show();
    return 0;
}

 

8 / 14

Can a destructor be virtual? Will the following program compile?

#include <iostream>
using namespace std;
class Base {
public:
  virtual ~Base() {}   
};
int main() {
   return 0;
}

 

9 / 14

Predict output of the following program

#include<iostream>
using namespace std;
 
class Base
{
public:
    virtual void show() { cout<<" In Base n"; }
};
 
class Derived: public Base
{
public:
    void show() { cout<<"In Derived n"; }
};
 
int main(void)
{
    Base *bp = new Derived;
    bp->show();
 
    Base &br = *bp;
    br.show();
 
    return 0;
}

 

1.

In Base 
In Base

2. 

In Base 
In Derived

3. 

In Derived
In Derived

4. 

In Derived
In Base 

10 / 14

Which of the following is true about pure virtual functions? 1) Their implementation is not provided in a class where they are declared. 2) If a class has a pure virtual function, then the class becomes abstract class and an instance of this class cannot be created.

11 / 14

Can static functions be virtual? Will the following program compile?

#include<iostream> 
using namespace std;    
  
class Test
{
   public:
      virtual static void fun()  { }
};

 

12 / 14

Predict the output of following C++ program. Assume that there is no alignment and a typical implementation of virtual functions is done by the compiler.

#include <iostream>
using namespace std;
 
class A
{
public:
    virtual void fun();
};
 
class B
{
public:
   void fun();
};
 
int main()
{
    int a = sizeof(A), b = sizeof(B);
    if (a == b) cout << "a == b";
    else if (a > b) cout << "a > b";
    else cout << "a < b";
    return 0;
}

 

13 / 14

#include<iostream>
using namespace std;
class Base  {
public:
    Base()    { cout<<"Constructor: Base"<<endl; }
    virtual ~Base()   { cout<<"Destructor : Base"<<endl; }
};
class Derived: public Base {
public:
    Derived()   { cout<<"Constructor: Derived"<<endl; }
    ~Derived()  { cout<<"Destructor : Derived"<<endl; }
};
int main()  {
    Base *Var = new Derived();
    delete Var;
    return 0;
}

 

1. 

Constructor: Base
Constructor: Derived
Destructor : Derived
Destructor : Base

2. 

Constructor: Base
Constructor: Derived
Destructor : Base

3. 

Constructor: Base
Constructor: Derived
Destructor : Derived

4. 

Constructor: Derived
Destructor : Derived

14 / 14

Predict the output of following C++ program

#include<iostream>
using namespace std;
 
class Base
{
public:
    virtual void show() { cout<<" In Base n"; }
};
 
class Derived: public Base
{
public:
    void show() { cout<<"In Derived n"; }
};
 
int main(void)
{
    Base *bp = new Derived;
    bp->Base::show();  // Note the use of scope resolution here
    return 0;
}

 

Your score is

The average score is 57%

0%

Recommended Articles for you: