12

Constructors

Question 1
Which of the followings is/are automatically added to every class, if we do not write our own.
A
Copy Constructor
B
Assignment Operator
C
A constructor without any parameter
D
All of the above
Constructors    
Discuss it


Question 1 Explanation: 
In C++, if we do not write our own, then compiler automatically creates a default constructor, a copy constructor and a assignment operator for every class.
Question 2
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
Constructors    
Discuss it


Question 2 Explanation: 
Question 3
Output of following program?
#include<iostream>
using namespace std;
class Point {
    Point() { cout << "Constructor called"; }
};

int main()
{
   Point t1;
   return 0;
}
A
Compiler Error
B
Runtime Error
C
Constructor called
Constructors    
Discuss it


Question 3 Explanation: 
By default all members of a class are private. Since no access specifier is there for Point(), it becomes private and it is called outside the class when t1 is constructed in main.
Question 4
#include<iostream>
using namespace std;
class Point {
public:
    Point() { cout << "Constructor called"; }
};

int main()
{
   Point t1, *t2;
   return 0;
}
A
Compiler Error
B
Constructor called
Constructor called
C
Constructor called
Constructors    
Discuss it


Question 4 Explanation: 
Only one object t1 is constructed here. t2 is just a pointer variable, not an object
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;
}
A
Normal Constructor called
Normal Constructor called

Normal Constructor called

Copy Constructor called

Copy Constructor called

Normal Constructor called

Copy Constructor called
B
Normal Constructor called
Copy Constructor called

Copy Constructor called

Normal Constructor called

Copy Constructor called
C
Normal Constructor called
Copy Constructor called

Copy Constructor called

Normal Constructor called
Constructors    
Discuss it


Question 5 Explanation: 
See following comments for explanation:
Point *t1, *t2;   // No constructor call
t1 = new Point(10, 15);  // Normal constructor call
t2 = new Point(*t1);   // Copy constructor call 
Point t3 = *t1;  // Copy Constructor call
Point t4;   // Normal Constructor call
t4 = t3;   // Assignment operator call 
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;
}

A
Compiler Error
B
10 followed by Garbage Value
C
10 10
D
10 0
Constructors    
Discuss it


Question 6 Explanation: 
The following may look like an error, but it works fine. X a = {10}; Like structures, class objects can be initialized. The line "X b = a;" calls copy constructor and is same as "X b(a);". Please note that, if we don't write our own copy constructor, then compiler creates a default copy constructor which assigns data members one object to other object.
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;
}
A
x = garbage value y = garbage value
B
x = 0 y = 0
C
Compiler Error
Constructors    
Discuss it


Question 7 Explanation: 
There is compiler error in line "Point p1;". The class Point doesn't have a constructor without any parameter. If we write any constructor, then compiler doesn't create the default constructor. It is not true other way, i.e., if we write a default or parameterized constructor, then compiler creates a copy constructor. See the next question.
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;
}

A
Compiler Error
B
x = 0 y = 0
C
x = garbage value y = garbage value
Constructors    
Discuss it


Question 8 Explanation: 
Compiler creates a copy constructor if we don't write our own. Compiler writes it even if we have written other constructors in class. So the above program works fine. Since we have default arguments, the values assigned to x and y are 0 and 0.
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;
}
A
Constructor called
B
Empty
C
Compiler Error
D
Runtime error
Constructors    
Discuss it


Question 9 Explanation: 
Unlike new, malloc() doesn't call constructor (See this) If we replace malloc() with new, the constructor is called, see this.
Question 10
#include <iostream>
using namespace std;

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

int main()
{
    cout << "Main Started ";
    return 0;
}
A
Main Started
B
Main Started Hello from Test()
C
Hello from Test() Main Started
D
Compiler Error: Global objects are not allowed
Constructors    
Discuss it


Question 10 Explanation: 
Output is
Hello from Test() Main Started
There is a global object 'a' which is constructed before the main functions starts, so the constructor for a is called first, then main()' execution begins.
There are 17 questions to complete.
12
 

Company Wise Coding Practice    Topic Wise Coding Practice