Exercise: Questions on Constructors


Questions on Constructors : Question 1 :
Which of the followings is/are automatically added to every class, if we do not write our own.

Copy Constructor
Assignment Operator
A constructor without any parameter
All of the above
Show Answer
Questions on Constructors : Question 2 :
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
Show Answer
Questions on Constructors : Question 3 :
Output of following program?
#include<iostream>
using namespace std;
class Point {
    Point() { cout << "Constructor called"; }
};

int main()
{
   Point t1;
   return 0;
}

Compiler Error
Runtime Error
Constructor called
Show Answer
Questions on Constructors : Question 4 :
#include<iostream>
using namespace std;
class Point {
public:
    Point() { cout << "Constructor called"; }
};

int main()
{
   Point t1, *t2;
   return 0;
}

Compiler Error
Constructor called
Constructor called
Constructor called
Show Answer
Questions on Constructors : Question 5 :
Output of following program?
#include<iostream>
using namespace std;

class Point {
public:
    Point() { cout << "Normal Constructor called\n"; }
    Point(const Point &t) { cout << "Copy constructor called\n"; }
};

int main()
{
   Point *t1, *t2;
   t1 = new Point();
   t2 = new Point(*t1);
   Point t3 = *t1;
   Point t4;
   t4 = t3;
   return 0;
}

Normal Constructor called
Normal Constructor called
Normal Constructor called
Copy Constructor called
Copy Constructor called
Normal Constructor called
Copy Constructor called
Normal Constructor called
Copy Constructor called
Copy Constructor called
Normal Constructor called
Copy Constructor called
Normal Constructor called
Copy Constructor called
Copy Constructor called
Normal Constructor called
Show Answer
Questions on Constructors : Question 6 :
#include<iostream>
using namespace std;

class X 
{
public:
    int x;
};

int main()
{
    X a = {10};
    X b = a;
    cout << a.x << " " << b.x;
    return 0;
}


Compiler Error
10 followed by Garbage Value
10 10
10 0
Show Answer
Questions on Constructors : Question 7 :
What is the output of following program?
#include <iostream>
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;
}

x = garbage value y = garbage value
x = 0 y = 0
Compiler Error
Show Answer
Questions on Constructors : Question 8 :
#include <iostream>
using namespace std;

class Point
{
    int x, y;
public:
   Point(int i = 0, int j = 0) { x = i; y = j; }
   int getX() { return x; }
   int getY() { return y; }
};

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


Compiler Error
x = 0 y = 0
x = garbage value y = garbage value
Show Answer
Questions on Constructors : Question 9 :
Predict the output of following program.
#include<iostream>
#include<stdlib.h>
using namespace std;

class Test
{
public:
   Test()
   { cout << "Constructor called"; }
};

int main()
{
    Test *t = (Test *) malloc(sizeof(Test));
    return 0;
}

Constructor called
Empty
Compiler Error
Runtime error
Show Answer
Questions on Constructors : Question 10 :
#include <iostream>
using namespace std;

class Test
{
public:
      Test() { cout << "Hello from Test() "; }
} a;

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

Main Started
Main Started Hello from Test()
Hello from Test() Main Started
Compiler Error: Global objects are not allowed
Show Answer