All Exams Test series for 1 year @ ₹349 only
Question

What is the output of code given below :

#include<iostream.h>

using namespace std;

class Base {

public:

Base(){

        cout<<"Base Const";

    }

virtual ~Base()

    {

        cout<<"Base dest";

    }

    };

class Derived : public Base {

public:

Derived() {

        cout<<"Derived Const";

    }

~Derived() {

        cout<<"Derived dest";

    }

    };

int main()

{

    Base *b=new Derived();

    delete b;

    return 0;

}

This question was previously asked in
UGC NET 2024 Management Question Paper (02-Sep-2024) (Shift 2)
The correct answer is

Base Const Derived Const Derived dest Base dest

When an object of a derived class is created, the base-class constructor runs first and then the derived-class constructor. Destruction happens in the reverse order. If the object is deleted through a base-class pointer, the derived destructor is reached only when the base destructor is declared virtual.

Step 1 — new Derived() first calls Base(), printing Base Const, and then Derived(), printing Derived Const.

Step 2 — delete b is applied to a Base *. Because ~Base() is virtual, the call is dispatched dynamically to ~Derived(), which prints Derived dest.

Step 3 — After the derived destructor finishes, the base destructor runs automatically and prints Base dest.

Printing Base dest before Derived dest would reverse the guaranteed destruction order. Printing Derived Const before Base Const reverses the construction order. Stopping after Base dest would happen only if the base destructor were not virtual, so that the derived part were never destroyed.

Base Const Derived Const Derived dest Base dest option is correct.

Was this answer helpful?

Similar Questions

  1. How to express that some person keeps animals as pets ?

  2. What is true about UML stereotypes ?

  3. Which of the following is/are correct with reference to Abstract class and interface ?

    (a) A class can inherit only one Abstract class but may inherit several interfaces.

    (b) An Abstract class can provide complete and default code but an interface has no code.

    Codes :

  4. Match the following with reference to object oriented modelling :

    List - IList - II
    (a) Polymorphism(i) Picking both operator and attributes with operations appropriate to model an object
    (b) Inheritance(ii) Hiding implementation details of methods from users of objects
    (c) Encapsulation(iii) Using similar operations to do similar things
    (d) Abstraction(iv) Create new classes from existing class

    Codes :

  5. Which of the following is used to make an Abstract class ?

  6. Match the following w.r.t. programming languages :

    List - IList - II
    (a) JAVA(i) Dynamically object oriented
    (b) Python(ii) Statically Non-object oriented
    (c) Prolog(iii) Statically object oriented
    (d) ADA(iv) Dynamically non-object oriented

    Codes :

  7. In C++, which system - provided function is called when no handler is provided to deal with an exception ?

  8. Consider the following two statements :

    (a) A publicly derived class is a subtype of its base class.

    (b) Inheritance provides for code reuse.

    Which one of the following statements is correct ?

  9. Module design is used to maximize cohesion and minimize coupling. Which of the following is the key to implement this rule ?

  10. When the inheritance is private, the private methods in base class are __________ in the derived class (in C++).


Important Questions from OOPs concept

  1. Which statements is correct regarding main () function in C++?

    I. It is a user defined function.

    II. It is a pre-defined function.

  2. Consider the following segment of C++ code.

    int j, n;

    for(j=1;j<=n; )

    j=j*2;

    What is the number of comparisons made in the execution of the loop for any n > 0?

  3. Given below are two statements:

    Statement (I): The friend function and the member functions of a friend class directly access the private and protected data.

    Statement (II): The friend function can access the private data through the member functions of the base class

    In the light of the above statements, choose the most appropriate answer from the options given below:

  4. If a constructor 'Date' is declared explicitly and has to be defined outside the class, which of the following is correct?

  5. Which of the following statements is TRUE ?

Need Expert Advice?
Upcoming Exams
CTET
December 12, 2026
Test Series
UGC NET img
Teaching
UGC NET (Paper 2) 2026 Mock Test Series
796 Tests 2 Tests Free
4.4(39)
English
More Questions from UGC NET

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App