• Tutorials
  • Courses
  • Tracks

Constructors, Destructors, and this Pointer

Last Updated :
Discuss
Comments

Question 1

When a copy constructor may be called?

  • When an object of the class is returned by value.

  • When an object of the class is passed (to a function) by value as an argument.

  • When an object is constructed based on another object of the same class

  • When compiler generates a temporary object.

  • All of the above

Question 2

Predict the output of the following program?

C++
#include <iostream>
using namespace std;

class GfG {
    GfG() {
        cout << "Constructor Called" << endl;
    }
};

int main() {
    cout << "Main Started" << endl;
    return 0;
}
  • Compiler Error

  • Main Started
    Constructor Called

  • Constructor Called
    Main Started

  • Main Started

Question 3

Can destructors be private in C++?

  • Yes

  • No

Question 4

What is the use of this pointer?

  • When local variable’s name is same as member’s name, we can access member using this pointer.

  • To return reference to the calling object

  • Can be used for chained function calls on an object

  • All of the above

Question 5

Predict the output of following C++ program CPP
#include<iostream>
using namespace std;

class Test
{
private:
  int x;
  int y;
public:
  Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
  static void fun1() { cout << "Inside fun1()"; }
  static void fun2() { cout << "Inside fun2()"; this->fun1(); }
};

int main()
{
  Test obj;
  obj.fun2();
  return 0;
}
  • Inside fun2() Inside fun1()
  • Inside fun2()
  • Inside fun1() Inside fun2()
  • Compiler Error

Question 6

Predict the output of following C++ program?

CPP
#include<iostream>
using namespace std;

class Test
{
private:
  int x;
public:
  Test() {x = 0;}
  void destroy()  { delete this; }
  void print() { cout << "x = " << x; }
};

int main()
{
  Test obj;
  obj.destroy();
  obj.print();
  return 0;
}
  • x = 0

  • undefined behavior

  • compiler error

  • runtime error

Question 7

What is the purpose of a default constructor in C++?
  • To create an object with default values
  • To create an object with specific initial values
  • To create a copy of an existing object
  • To transfer resources from a temporary object

Question 8

What will be the output?

C++
#include <iostream>
class Test {
public:
    ~Test() {
        std::cout << "Destructor called\n";
    }
};

int main() {
    Test t1;
}


  • No output

  • Destructor called

  • Compilation error

  • Runtime Error

Question 9

What will be the output?

C++
#include <iostream>
class Sample {
public:
    ~Sample() {
        std::cout << "Destructor executed\n";
    }
};

void createObject() {
    Sample s;
}

int main() {
    createObject();
    std::cout << "Main done\n";
}


  • Main done

  • Destructor executed

  • Destructor executed
    Main done

  • Main done
    Destructor executed

Tags:

There are 9 questions to complete.

Take a part in the ongoing discussion