Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ Virtual Function Aptitude Questions and Answers
C++ Virtual Function Aptitude: This section contains C++ Virtual Function Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on March 06, 2021
1) There are the following statements that are given below, which of them are correct about virtual function in C++?
- The virtual function is the type of runtime polymorphism in C++.
- The virtual function is used in inheritance where a function is declared in the base and it is overridden in child class.
- For virtual function implementation we need did not require any special keyword.
- All of the above
Options:
- A
- A and B
- A, B, and C
- D
Correct Answer - 2
A and B
Explanation:
Statements A and B are correct about virtual function.
2) Which of the following types of binding used in C++?
- Static binding
- Dynamic binding
- Pointer binding
- Inheritance binding
Options:
- A and B
- C and D
- A, B, and C
- A, B, C, and D
Correct Answer - 1
A and B
Explanation:
Static and Dynamic binding is used in C++.
3) The dynamic binding is also known as?
- Early binding
- Late binding
- Static binding
- None of the above
Correct Answer - 2
Late binding
Explanation:
The dynamic binding is also known as late binding.
4) There are the following statements that are given below, which of them are correct about dynamic binding in C++?
- The dynamic binding is performed at runtime.
- In the dynamic binding, function calls are not resolved until runtime.
- Dynamic binding is a type of runtime polymorphism.
- All the above
Options:
- A and B
- A and C
- B and C
- D
Correct Answer - 4
D
Explanation:
All given statements are correct about dynamic binding.
5) There is a code snippet that is given below, which is the example of dynamic binding?
#include <iostream>
using namespace std;
class Sample {
public:
void display(int X, int Y)
{
cout << X << " " << Y << " ";
}
void display(int X, int Y, int Z)
{
cout << X << " " << Y << " " << Z << " ";
}
};
int main()
{
Sample S;
S.display(10, 20);
S.display(10, 20, 30);
return 0;
}
Options:
- Yes
- No
Correct Answer - 2
No
Explanation:
No, the given code snippet is an example of static binding.
6) A virtual function can be static?
- Yes
- No
Correct Answer - 2
No
Explanation:
No, a virtual function cannot be static.
7) A virtual function can be a friend function of another class?
- Yes
- No
Correct Answer - 2
No
Explanation:
No, a virtual function cannot be a friend function of another class.
8) There are the following statements that are given below, which of them are correct about the virtual keyword in C++?
- The signature of a virtual function must be the same in both base and derived class.
- We can create a virtual constructor in a class.
- We can create virtual destructor in a class.
- All the above
Options:
- A and B
- A and C
- B and C
- D
Correct Answer - 2
A and C
Explanation:
Statements A and C are correct about the virtual keyword in C++.
9) What is the correct output of the given code snippet?
#include <iostream>
using namespace std;
class Base {
public:
virtual void display()
{
cout << "Base class function";
}
};
class Child : public Base {
public:
virtual int display()
{
cout << "Child class function";
}
};
int main()
{
Child C;
C.display();
return 0;
}
Options:
- Base class function
- Child class function
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate syntax error, because the signature of the virtual function must be the same in both base and child class.
10) What is the correct output of the given code snippet?
#include <iostream>
using namespace std;
class Base {
public:
virtual void display()
{
cout << "Base class function";
}
};
class Child : public Base {
public:
virtual void display()
{
cout << "Child class function";
}
};
int main()
{
Child C;
C.display();
return 0;
}
Options:
- Base class function
- Child class function
- Syntax error
- Runtime error
Correct Answer - 2
Child class function
Explanation:
The above code will print the "Child class function" on the console screen.
11) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Base {
public:
void display()
{
cout << "Base class function";
}
};
class Child : public Base {
public:
virtual int display()
{
cout << "Child class function";
}
};
int main()
{
Child C;
C.display();
return 0;
}
Options:
- Base class function
- Child class function
- Syntax error
- Runtime error
Correct Answer - 2
Child class function
Explanation:
The above code will print the "Child class function" on the console screen.
12) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
class Base {
public:
virtual void display()
{
cout << "Base class function" << endl;
}
};
class Child : public Base {
public:
void display()
{
cout << "Child class function" << endl;
}
};
int main()
{
Base* bPtr;
bPtr->display();
return 0;
}
Options:
- Base class function
- Child class function
- Syntax error
- Runtime error
Correct Answer - 4
Runtime error
Explanation:
The above code will generate a runtime error. Because we are using an un-initialized pointer.
13) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
class Base {
public:
virtual void display()
{
cout << "Base class function" << endl;
}
};
class Child : public Base {
public:
void display()
{
cout << "Child class function" << endl;
}
};
int main()
{
Base* bPtr;
Base B;
bPtr = B;
bPtr->display();
return 0;
}
Options:
- Base class function
- Child class function
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate syntax error because we need to use '&' to assign the address of an object of base class to the pointer.
14) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Base {
public:
virtual void display()
{
cout << "Base class function" << endl;
}
};
class Child : public Base {
public:
void display()
{
cout << "Child class function" << endl;
}
};
int main()
{
Base* bPtr;
Base B;
bPtr = &B;
bPtr->display();
return 0;
}
Options:
- Base class function
- Child class function
- Syntax error
- Runtime error
Correct Answer - 1
Base class function
Explanation:
The above code will print the "Base class function" on the console screen.
15) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Base {
public:
virtual void display()
{
cout << "Base class function" << endl;
}
};
class Child : public Base {
public:
void display()
{
cout << "Child class function" << endl;
}
};
int main()
{
Base* bPtr;
Child C;
bPtr = &C;
bPtr->display();
return 0;
}
Options:
- Base class function
- Child class function
- Syntax error
- Runtime error
Correct Answer - 2
Child class function
Explanation:
The above code will print the "Child class function" on the console screen.
16) There are the following statements that are given below, which of them are correct about the virtual table in C++?
- A virtual table is used to resolve virtual function calls to achieve dynamic binding.
- Every class has its own virtual table for virtual functions.
- A virtual table is also known as the dispatch table.
- A virtual table is also known as VTABLE.
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct Answer - 4
A, B, C, and D
Explanation:
All statements are correct about the virtual table in C++.
17) There are the following statements that are given below, which of them are correct about virtual pointer in C++?
- A virtual pointer is used for virtual function calls.
- A virtual pointer is inserted as data member to point virtual function address in the virtual table.
- Every object of a class has its own virtual pointer.
- A virtual pointer is also known as VPTR.
Options:
- A and B
- A and C
- A, B, and D
- A, B, C, and D
Correct Answer - 4
A, B, C, and D
Explanation:
All given statements are correct about virtual pointer in C++.
18) There are the following statements that are given below, which of them are correct about pure virtual function in C++?
Options:
- A pure virtual function is a virtual function that is not defined in the class where it is declared.
- A pure virtual function is declared by assigning 0.
- Both of the above
- None of the above
Correct Answer - 3
Both of the above
Explanation:
Statements 1st and 2nd are correct about pure virtual function.
19) To create pure virtual function we need to use a "pure" keyword?
- Yes
- No
Correct Answer - 2
No
Explanation:
No, we did not require a "pure" keyword to create pure virtual function.
20) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() = 0
{
}
};
class Child : public Base {
public:
void display()
{
cout << "Child class function" << endl;
}
};
int main()
{
Base* bPtr;
Child C;
bPtr = &C;
bPtr->display();
return 0;
}
Options:
- Child class function
- Base class function
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.
21) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() = 0;
};
class Child1 : public Base {
public:
void display()
{
cout << "Child1 class function" << endl;
}
};
class Child2 : public Base {
public:
void display()
{
cout << "Child2 class function" << endl;
}
};
int main()
{
Base* bPtr = new Child1();
bPtr->display();
return 0;
}
Options:
- Child1 class function
- Child2 class function
- Syntax error
- Runtime error
Correct Answer - 1
Child1 class function
Explanation:
The code will print the "Child1 class function" on the console screen.
22) A class contains pure virtual functions is known as?
- Abstract class
- Concrete class
- Pure virtual class
- Virtual base class
Correct Answer - 1
Abstract class
Explanation:
An abstract class contains pure virtual functions.
23) We cannot create the object of an abstract class in C++?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
We cannot create the object of an abstract class in C++.
24) There are the following statements that are given below, which of them are correct about abstract class in C++?
- We can create a pointer of an abstract class, but we cannot create an object of an abstract class.
- An abstract class can contain variables, pure virtual functions, and other functions.
- Both of the above
- None of the above
Correct Answer - 3
Both of the above
Explanation:
Statements 1st and 2nd are correct about abstract class in C++.
25) The virtual base class is used to prevent the ambiguity of multiple inheritance?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, the virtual base class is used to prevent the ambiguity of multiple inheritance.