Constructors & Destructor

Last Updated :
Discuss
Comments

Question 1

When a copy constructor may be called?

  • A

    When an object of the class is returned by value.

  • B

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

  • C

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

  • D

    When compiler generates a temporary object.

  • E

    All of the above

Question 2

What is the output of following program? CPP
#include 
using namespace std;

class Point
{
    int x, y;
public:
   Point(const Point &p) { x = p.x; y = p.y; }
   int getX() { return x; }
   int getY() { return y; }
};

int main()
{
    Point p1;
    Point p2 = p1;
    cout << "x = " << p2.getX() << " y = " << p2.getY();
    return 0;
}
  • A

    x = garbage value y = garbage value

  • B

    x = 0 y = 0

  • C

    Compiler Error

Question 3

Predict the output of the following program?

C++
#include 
using namespace std;

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

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

    Compiler Error

  • B

    Main Started
    Constructor Called

  • C

    Constructor Called
    Main Started

  • D

    Main Started

Question 4

Predict the output of following C++ progran CPP
#include 
using namespace std;
 
int i;
 
class A
{
public:
    ~A()
    {
        i=10;
    }
};
 
int foo()
{
    i=3;
    A ob;
    return i;
}
 
int main()
{
    cout << foo() << endl;
    return 0;
}
  • A
    0
  • B
    3
  • C
    10
  • D
    None of the above

Question 5

Like constructors, can there be more than one destructors in a class?
  • A
    Yes
  • B
    No

Question 6

CPP
#include 
using namespace std; 
class A
{
    int id;
    static int count;
public:
    A() {
        count++;
        id = count;
        cout << "constructor for id " << id << endl;
    }
    ~A() {
        cout << "destructor for id " << id << endl;
    }
};
 
int A::count = 0;
 
int main() {
    A a[3];
    return 0;
}
  • A
    constructor for id 1
    constructor for id 2
    constructor for id 3
    destructor for id 3
    destructor for id 2
    destructor for id 1
    
  • B
    constructor for id 1
    constructor for id 2
    constructor for id 3
    destructor for id 1
    destructor for id 2
    destructor for id 3
    
  • C
    Compiler Dependent.
    
  • D
    constructor for id 1
    destructor for id 1
    

Question 7

Which of the following is true about this pointer?
  • A
    It is passed as a hidden argument to all function calls
  • B
    It is passed as a hidden argument to all non-static function calls
  • C
    It is passed as a hidden argument to all static functions
  • D
    None of the above

Question 8

What is the use of this pointer?

  • A

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

  • B

    To return reference to the calling object

  • C

    Can be used for chained function calls on an object

  • D

    All of the above

Question 9

Predict the output of following C++ program CPP
#include
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;
}
  • A
    Inside fun2() Inside fun1()
  • B
    Inside fun2()
  • C
    Inside fun1() Inside fun2()
  • D
    Compiler Error

Question 10

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

There are 12 questions to complete.

Take a part in the ongoing discussion