Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ Inheritance aptitude questions and answers
C++ Polymorphism & Function Overloading Aptitude: This section contains C++ Polymorphism & Function Overloading Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on March 01, 2021
1) Which concept of OOP creates a hierarchy of classes?
- Enumeration
- Abstraction
- Inheritance
- None of the above
Correct Answer - 3
Inheritance
3) How many specifiers are used in to derived a class?
- 1
- 2
- 3
- 4
Correct Answer - 3
4 (They are private, protected and public.)
5) What will be the output of the following code?
#include <iostream>
#include <string>
using namespace std;
class A {
int a, b;
float d;
public:
void change(int i)
{
a = i;
}
void value_of_a()
{
cout << a;
}
};
class B : private A {
};
int main(int argc, char const* argv[])
{
B b;
cout << sizeof(B);
return 0;
}
Options:
- 1
- 12
- ERROR
- Segmentation fault
Correct Answer - 2
12
Explanation:
Class B is derived from A class and Class A has three members of size 4 bytes then Size of B =4*3=12 Bytes.
6) Choose the correct answer (not-output) with the following code.
#include <iostream>
using namespace std;
class polygon {
protected:
int width, height;
public:
void set_values(int a, int b)
{
width = a;
height = b;
}
};
class output1 {
public:
void output(int i);
};
void output1::output(int i)
{
cout << i << endl;
}
class rectangle : public polygon, public output1 {
public:
int area()
{
return (width * height);
}
};
class triangle : public polygon, public output1 {
public:
int area()
{
return (width * height / 2);
}
};
int main()
{
rectangle rect;
triangle trgl;
rect.set_values(4, 5);
trgl.set_values(4, 5);
rect.output(rect.area());
trgl.output(trgl.area());
return 0;
}
Options:
- Inherited
- Compile-time Error
- Runtime Error
- None of the mentioned
Correct Answer - 1
Inherited
Explanation:
There is no error (neither compile-time or runtime) in the code, classes polygon and output1 are inheriting on classes rectangle and triangle.
The output would be...
20
10
7) Which of the following are correct types of inheritance in C++?
- Single Inheritance
- Multiple Inheritance
- Canonical Inheritance
- Hybrid Inheritance
Options:
- A and B
- A, B, and C
- A, B, and D
- A, B, C, and D
Correct Answer - 3
A, B, and D
Explanation:
Options A, B, and D are the correct types of inheritance in C++.
8) What is the correct output of the following code?
#include <iostream>
using namespace std;
class Base {
public:
Base()
{
cout << "Base Ctor Called" << endl;
}
};
class Derived::Base {
public:
Derived()
{
cout << "Derived Ctor Called" << endl;
}
};
int main()
{
Derived D;
return 0;
}
Options:
- Derived Ctor Called
- Base Ctor Called
Derived Ctor Called
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate syntax error, because colon ':' is used for inheritance but in above code scope resolution operator "::" is used for inheritance.
9) What is the correct output of the following code?
#include <iostream>
using namespace std;
class Base {
public:
Base()
{
cout << "Base Ctor Called" << endl;
}
};
class Derived : Base {
public:
Derived()
{
cout << "Derived Ctor Called" << endl;
}
};
int main()
{
Derived D;
return 0;
}
Options:
- Derived Ctor Called
- Base Ctor Called
Derived Ctor Called
- Compile-time error
- Runtime exception
Correct Answer - 2
Base Ctor Called
Derived Ctor Called
Explanation:
The 2nd option is the correct output of the above code.
10) What is the correct output of the following code?
#include <iostream>
using namespace std;
class Base {
public:
Base()
{
cout << "Base Ctor Called" << endl;
}
~Base()
{
cout << "Base Dtor Called" << endl;
}
};
class Derived : Base {
public:
Derived()
{
cout << "Derived Ctor Called" << endl;
}
~Derived()
{
cout << "Derived Dtor Called" << endl;
}
};
int main()
{
Derived D;
return 0;
}
Options:
- Base Ctor Called
Derived Ctor Called
Base Dtor Called
Derived Dtor Called
- Base Ctor Called
Derived Ctor Called
Derived Dtor Called
Base Dtor Called
- Compile-time error
- Runtime error
Correct Answer - 2
Base Ctor Called
Derived Ctor Called
Derived Dtor Called
Base Dtor Called
Explanation:
The 2nd option is the correct output of the above code.
11) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Base {
public:
void fun1()
{
cout << "Fun1() called ";
}
};
class Derived : Base {
public:
void fun2()
{
cout << "Fun2() called ";
}
};
int main()
{
Derived D;
D.fun2();
return 0;
}
Options:
- Fun2() called
- Fun1() called Fun2() called
- Compile-time error
- Runtime error
Correct Answer - 1
Fun2() called
Explanation:
The above code will print "Fun2() called" on the console screen.
12) What is the correct output of the following code?
#include <iostream>
using namespace std;
class Base {
public:
void fun1()
{
cout << "Base Fun1() called ";
}
};
class Derived : Base {
public:
void fun1()
{
cout << "Derived Fun1() called ";
}
};
int main()
{
Derived D;
D.fun1();
return 0;
}
Options:
- Base Fun1() called
- Derived Fun1() called
- Compile-time error
- Runtime error
Correct Answer - 2
Derived Fun1() called
Explanation:
The above code will print "Derived Fun1() called" on the console screen.
13) What is the correct output of the following code?
#include <iostream>
using namespace std;
class Base {
int ABC;
};
class Derived1 : Base {
};
class Derived2 : Derived1 {
};
int main()
{
Derived2 D;
cout << sizeof(D);
return 0;
}
Options:
- 4
- 8
- 16
- Error
Correct Answer - 1
4
Explanation:
The above code will print "4" on the console screen.
14) What is the correct output of the following code?
#include <iostream>
using namespace std;
class Base {
};
class Derived1 : Base {
};
class Derived2 : Base {
};
class Derived3 : Derived1, Derived2 {
};
int main()
{
cout << sizeof(Derived3);
return 0;
}
Options:
- 0
- 2
- 4
- Error
Correct Answer - 2
2
Explanation:
The above code will print "2" on the console screen.
15) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Base {
int A;
};
class Derived1 : Base {
};
class Derived2 : Base {
};
class Derived3 : Derived1, Derived2 {
};
int main()
{
cout << sizeof(Derived3);
return 0;
}
Options:
- 2
- 4
- 8
- 16
Correct Answer - 3
8
Explanation:
The above code will print "8" on the console screen.
16) What is the correct output of the given code snippets?
#include <iostream>
using namespace std;
class Base {
int A;
};
class Derived1 : Base {
int B;
};
class Derived2 : Base {
};
class Derived3 : Derived1, Derived2 {
};
int main()
{
cout << sizeof(Derived3);
return 0;
}
Options:
- 8
- 12
- 16
- 32
Correct Answer - 2
12
Explanation:
The above code will print "12" on the console screen.
17) What is the correct output of the following code?
#include <iostream>
using namespace std;
class Base1 {
public:
void fun()
{
cout << "Base1 fun() called";
}
};
class Base2 {
public:
void fun()
{
cout << "Base2 fun() called";
}
};
class Derived : Base1, Base2 {
};
int main()
{
Derived D;
D.fun();
return 0;
}
Options:
- Base1 fun() called
- Base2 fun() called
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time error due to an ambiguous function call.
18) What is the correct output of the following code?
#include <iostream>
using namespace std;
class Base1 {
public:
void fun()
{
cout << "Base1 fun() called";
}
};
class Base2 {
public:
void fun()
{
cout << "Base2 fun() called";
}
};
class Derived : public Base1, public Base2 {
};
int main()
{
Derived D;
D.Base1::fun();
return 0;
}
Options:
- Base1 fun() called
- Base2 fun() called
- Compile-time error
- Runtime error
Correct Answer - 1
Base1 fun() called
Explanation:
The code will print "Base1 fun() called" on the console screen.
19) The protected and public member of the base class becomes protected members in the child class?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, the protected and public member of the base class becomes protected members in the child class.
20) What is the correct output of the following code?
#include <iostream>
using namespace std;
class Sample {
protected:
void fun()
{
cout << "fun() called";
}
};
int main()
{
Sample S;
cout << S.fun();
return 0;
}
Options:
- fun() called
- Compile-time error
- Syntax error
- Runtime error
Correct Answer - 2
Compile-time error
Explanation:
The above code will generate a compile-time error.
21) Hybrid inheritance is the combination of any two types of inheritance?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, Hybrid inheritance is the combination of any two types of inheritance.