C++ Constructors

Question 1

Predict output of the following program 

C

#include <stdio.h>
int main()
{
   printf("new_c_questionbr");
   printf("geeksforgeeks"); 
   getchar();
   return 0;
}
Cross

ew_c_question
geeksforgeeks
 

Cross

new_c_ques
geeksforgeeks
 

Cross


geeksforgeeks
 

Tick

Depends on terminal configuration



Question 2
Which of the followings is/are automatically added to every class, if we do not write our own.
Cross
Copy Constructor
Cross
Assignment Operator
Cross
A constructor without any parameter
Tick
All of the above


Question 2-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 3

When a copy constructor may be called?

Cross
When an object of the class is returned by value.
Cross
When an object of the class is passed (to a function) by value as an argument.
Cross
When an object is constructed based on another object of the same class
Cross
When compiler generates a temporary object.
Tick
All of the above


Question 3-Explanation: 

All the given options are correct as copy constructor can be called in any of the situation.

Question 4

Output of following program? 
 

C++

#include <iostream>
using namespace std;

class Point {
    Point() { cout << "Constructor called"; }
};

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

Runtime Error

Cross

None of these

Cross

Constructor called

Tick

Compiler Error



Question 4-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 5
#include<iostream>
using namespace std;
class Point {
public:
    Point() { cout << "Constructor called"; }
};

int main()
{
   Point t1, *t2;
   return 0;
}
Cross
Compiler Error
Cross
Constructor called
Constructor called
Tick
Constructor called


Question 5-Explanation: 
Only one object t1 is constructed here. t2 is just a pointer variable, not an object
Question 6
Output of following program?
#include<iostream>
using namespace std;

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

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

Normal Constructor called

Copy Constructor called

Copy Constructor called

Normal Constructor called

Copy Constructor called
Cross
Normal Constructor called
Copy Constructor called

Copy Constructor called

Normal Constructor called

Copy Constructor called
Tick
Normal Constructor called
Copy Constructor called

Copy Constructor called

Normal Constructor called


Question 6-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 7

Output of following C++ code will be?

C++

#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;
}
Cross

Compiler Error

Cross

10 followed by Garbage Value

Tick

10 10

Cross

10 0



Question 7-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 8
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;
}
Cross
x = garbage value y = garbage value
Cross
x = 0 y = 0
Tick
Compiler Error


Question 8-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 9
#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;
}

Cross
Compiler Error
Tick
x = 0 y = 0
Cross
x = garbage value y = garbage value


Question 9-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 10
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;
}
Cross
Constructor called
Tick
Empty
Cross
Compiler Error
Cross
Runtime error


Question 10-Explanation: 
Unlike new, malloc() doesn't call constructor (See this) If we replace malloc() with new, the constructor is called, see this.
There are 26 questions to complete.

  • Last Updated : 28 Sep, 2023

Share your thoughts in the comments
Similar Reads