C++ Programming Questions and Answers – Derived Classes

C++ programming questions and answers focuses on “Derived Classes”. One shall practice these questions to improve their C++ programming skills needed for various interviews entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ programming questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ programming questions on “Derived Classes” along with answers, explanations and/or solutions:

Q 1.Where is the derived class is derived from?
A. derived
B. class
C. base
D. both derived & base

Show Answer Answer:-C. base
Explanation Because derived inherits functions and variables from base.

Q 2. Pick out the correct statement.
A. A derived class’s constructor cannot explicitly invokes its base class’s constructor
B. A derived class’s destructor can invoke its base class’s destructor
C. A derived class’s destructor cannot invoke its base class’s destructor
D. A derived class’s destructor can invoke its base & derived class’s destructor

Show Answer Answer:-C. A derived class’s destructor cannot invoke its base class’s destructor
Explanation Destructors are automatically invoked when an object goes out of scope or when a dynamically allocated object is deleted. Inheritance does not change this behavior. This is the reason a derived destructor cannot invoke its base class destructor.

Q 3. Which of the following can derived class inherit?
A. members
B. functions
C. both members & functions
D. classes

Show Answer Answer:-C. both members & functions
Explanation Both data members and member functions are inherited by derived class in C++. advertisement

Q . 4 What will be the output of the following C++ code? include using namespace std;
class A
{
public:
A(int n )
{
cout << n;
}
};
class B: public A
{
public:
B(int n, double d)
: A(n)
{
cout << d;
}
};
class C: public B
{
public:
C(int n, double d, char ch)
: B(n, d)
{
cout <<ch;
}
};
int main()
{
C c(5, 4.3, ‘R’);
return 0;
}
A. R4.35
B. 54.3R
C. 4.3R5
D. R2.6

Show Answer Answer:-B. 54.3R
Explanation In this program, We are passing the value and manipulating by using the derived class. Output: Note: Join free Sanfoundry classes at Telegram or Youtube $ g++ der.cpp $ a.out 54.3R

Q 5. What will be the output of the following C++ code? include using namespace std;
class BaseClass
{
protected:
int i;
public:
BaseClass(int x)
{
i = x;
}
~BaseClass()
{
}
};
class DerivedClass: public BaseClass
{
int j;
public:
DerivedClass(int x, int y): BaseClass(y)
{
j = x;
}
~DerivedClass()
{
}
void show()
{
cout << i << ” ” << j << endl;
}
};
int main()
{
DerivedClass ob(3, 4);
ob.show();
return 0;
}
A. 3
B. 4
C. 43
D. 53

Show Answer Answer:-B. 4
Explanation In this program, We are passing the values and assigning it to i and j and we are printing it. Output: $ g++ der1.cpp $ a.out 4 3

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

include using namespace std;
class Base
{
public:
int m;
Base(int n=0)
: m(n)
{
cout << “Base” << endl;
}
};
class Derived: public Base
{
public:
double d;
Derived(double de = 0.0)
: d(de)
{
cout << “Derived” << endl;
}
};
int main()
{
cout << “Instantiating Base” << endl;
Base cBase;
cout << “Instantiating Derived” << endl;
Derived cDerived;
return 0;
}
A. Instantiating Base
Base
Instantiating Derived
Base
Derived
B. Instantiating Base
Instantiating Derived
Base
Derived
C. Instantiating Base
Base
Instantiating Derived
Base
D. Instantiating Base

Show Answer Answer:-A. Instantiating Base Base Instantiating Derived Base Derived
Explanation In this program, We are printing the execution order of the program. Output: $ g++ der2.cpp $ a.out Instantiating Base Base Instantiating Derived Base Derived

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

include using namespace std;
class Parent
{
public:
Parent (void)
{
cout << “Parent()\n”;
}
Parent (int i)
{
cout << “Parent(“<< i << “)\n”;
};
Parent (void)
{
cout << “~Parent()\n”;
};
};
class Child1 : public Parent { };
class Child2 : public Parent
{
public:
Child2 (void)
{
cout << “Child2()\n”;
}
Child2 (int i) : Parent (i)
{
cout << “Child2(” << i << “)\n”;
}
~Child2 (void)
{
cout << “~Child2()\n”;
}
};
int main (void)
{
Child1 a;
Child2 b;
Child2 c(42);
return 0;
}
A. Parent()
Parent()
Child2()
~Child2()
~Parent()
~Parent()
B. error
C. runtime error
D. Parent(42)

Show Answer Answer:-B. error
Explanation In this program, We got an error in overloading because we didn’t invoke the destructor of parent.

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

include using namespace std;
class X
{
int m;
public:
X() : m(10)
{
}
X(int mm): m(mm)
{
}
int getm()
{
return m;
}
};
class Y : public X
{
int n;
public:
Y(int nn) : n(nn) {}
int getn() { return n; }
};
int main()
{
Y yobj( 100 );
cout << yobj.getm() << ” ” << yobj.getn() << endl;
}
A. 10 100
B. 100 10
C. 10 10
D. 100 100

Show Answer Answer:-A. 10 100
Explanation In this program, We are passing the value and getting the result by derived class. Output: $ g++ der5.cpp $ a.out 10 100

Q 9. Which operator is used to declare the destructor?
A. #
B. ~
C. @
D. $

Show Answer Answer:-B. ~
Explanation tilde(~) is used to declare destructor of a class.

Q 10. Which constructor will initialize the base class data member?
A. derived class
B. class
C. base class
D. derived & base class

Show Answer Answer:-C. base class
Explanation Because it is having the proper data set to initialize, Otherwise it will throw an error.

Q 11. Which of the following is true about virtual functions in derived classes?

A. They must be redefined in the derived class.

B. They allow runtime polymorphism.

C. They are resolved at compile time.

D. They cannot be declared private.

Show Answer Answer:-B. They allow runtime polymorphism.

Q 12. What happens if a class has no explicitly defined constructor and it’s a base class?

A. The compiler generates a default constructor.

B. It cannot be used as a base class.

C. Derived classes must provide their own constructor to initialize base class members.

D. An error occurs.

Show Answer Answer:-A. The compiler generates a default constructor.

Q 13. Multiple inheritance means a class can inherit from:

A. Multiple interfaces

B. Multiple abstract classes

C. Multiple base classes

D. Multiple virtual functions

Show Answer Answer:-C. Multiple base classes

Q 14. The virtual keyword is used in the base class function declaration to achieve:

A. Compile-time polymorphism

B. Data hiding

C. Runtime polymorphism

D. Encapsulation

Show Answer Answer:-C. Runtime polymorphism

Q 15. What is the order of destruction in inheritance?

A. Base class destructor then derived class destructor.

B. Derived class destructor then base class destructor.

C. Both destructors are called simultaneously.

D. The order is undefined.

Show Answer Answer:-B. Derived class destructor then base class destructor.

Q 16. If a base class destructor is not virtual, what can happen when deleting a derived class object through a base class pointer?

A. Only the base class destructor is called.

B. Only the derived class destructor is called.

C. Both destructors are called correctly.

D. Undefined behavior.

Show Answer Answer:-D. Undefined behavior.

Q 17. A pure virtual function is declared by:

A. Assigning it 0 in the declaration.

B. Using the virtual keyword without a body.

C. Declaring it abstract.

D. Making it private.

Show Answer Answer:-A. Assigning it 0 in the declaration.

Q 18. A class containing at least one pure virtual function is called a/an:

A. Concrete class

B. Abstract class

C. Derived class

D. Interface class

Show Answer Answer:-B. Abstract class

Q 19. Can you create an object of an abstract class?

A. Yes, always.

B. No, never directly.

C. Only if it has no pure virtual functions.

D. Only using dynamic allocation.

Show Answer Answer:-B. No, never directly.

Q 20. Which of the following describes the “is-a” relationship in object-oriented programming?

A. Association

B. Aggregation

C. Composition

D. Inheritance

Show Answer Answer:-D. Inheritance

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page