Quiz
C++ Virtual Functions
This quiz has a time limit, so it opens in the usual player.
Questions in this quiz (10)
1Which of the following is true about virtual functions in C++.
- AVirtual functions are functions that can be overridden in derived class with the same signature.
- BVirtual functions enable run-time polymorphism in a inheritance hierarchy.
- Cif a function is 'virtual' in the base class, the most-derived class's implementation of the function is called according to the actual type of the object referred to, regardless of the declared type of the pointer or reference. In non-virtual functions, the functions are called according to the type of reference or pointer.
- DAll of the above
2Predict 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;
}- AIn Base
In Base - BIn Base
In Derived - CIn Derived
In Derived - DIn Derived
In Base
- AIn Base
3Output 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;
}- AIn Base
In Base - BIn Base
In Derived - CIn Derived
In Derived - DIn Derived
In Base
- AIn Base
4Which 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.
- ABoth 1 and 2
- BOnly 1
- COnly 2
- DNeither 1 nor 2
5#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
int main(void)
{
Base b;
Base *bp;
return 0;
}- AThere are compiler errors in lines "Base b;" and "Base bp;
- BThere is compiler error in line "Base b;"
- CThere is compiler error in line "Base bp;"
- DNo compiler Error
6Predict 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;
}- ACompiler Error: there cannot be an empty derived class
- BCompiler Error: Derived is abstract
- CNo compiler Error
7#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;
}- ACompiler Error in line "Base &br = d;"
- BEmpty Output
- CIn Derived
8#include <iostream>
using namespace std;
class Base {
public:
virtual Base() {}
};
int main() {
return 0;
}- AYes
- BNo
9#include <iostream>
using namespace std;
class Base {
public:
virtual ~Base() {}
};
int main() {
return 0;
}- AYES
- BNO
10#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;
}- AConstructor: Base
Constructor: Derived
Destructor : Derived
Destructor : Base - BConstructor: Base
Constructor: Derived
Destructor : Base - CConstructor: Base
Constructor: Derived
Destructor : Derived - DConstructor: Derived
Destructor : Derived
- AConstructor: Base
Information
- Description
- C++ Quiz on C++ Virtual Functions, created by PathaPadha Support on 04/11/2020.
- Author
- PathaPadha Support
- Language
- English
- Views
- 199
- Tags
- Rating
- No ratings yet Rate this quiz