1/18/2018 Constructors in C++ - GeeksQuiz
GeeksforGeeks Custom Search
A computer science portal for geeks
Practice GATE CS Placements Videos Contribute
Login/Register
Constructors
1 2
Question 1 WRONG
Which of the followings is/are automatically added to every class, if we do not write our own.
A Copy Constructor
Assignment Operator
C A constructor without any parameter
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 WRONG
When a copy constructor may be called? ▲
A When an object of the class is returned by value.
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 1/13
1/18/2018 Constructors in C++ - GeeksQuiz
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
When compiler generates a temporary object.
All of the above
Constructors
Discuss it
Question 2 Explanation:
See When is copy constructor called?
Question 3 WRONG
Output of following program?
#include<iostream>
using namespace std;
class Point {
Point() { cout << "Constructor called"; }
};
int main()
{
Point t1;
return 0;
}
Run on IDE
Compiler Error
Runtime Error
C Constructor called
Constructors
Discuss it ▲
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 2/13
1/18/2018 Constructors in C++ - GeeksQuiz
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 WRONG
#include<iostream>
using namespace std;
class Point {
public:
Point() { cout << "Constructor called"; }
};
int main()
{
Point t1, *t2;
return 0;
}
Run on IDE
A Compiler Error
Constructor called
Constructor called
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 WRONG
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"; }
};
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 3/13
1/18/2018 Constructors in C++ - GeeksQuiz
int main()
{
Point *t1, *t2;
t1 = new Point();
t2 = new Point(*t1);
Point t3 = *t1;
Point t4;
t4 = t3;
return 0;
}
Run on IDE
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
B
Normal Constructor called
Copy Constructor called
Normal Constructor called
Copy Constructor called
Copy Constructor called
Normal Constructor called
Constructors
▲
Discuss it
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 4/13
1/18/2018 Constructors in C++ - GeeksQuiz
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 WRONG
#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;
}
Run on IDE
Compiler Error
B 10 followed by Garbage Value
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
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 5/13
1/18/2018 Constructors in C++ - GeeksQuiz
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 WRONG
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;
}
Run on IDE
x = garbage value y = garbage value
B x=0y=0
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 CORRECT
#include <iostream>
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 6/13
1/18/2018 Constructors in C++ - GeeksQuiz
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;
}
Run on IDE
A Compiler Error
x=0y=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 WRONG
Predict the output of following program.
#include<iostream>
#include<stdlib.h>
using namespace std;
class Test
{
public:
Test()
{ cout << "Constructor called"; } ▲
};
int main()
{
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 7/13
1/18/2018 Constructors in C++ - GeeksQuiz
Test *t = (Test *) malloc(sizeof(Test));
return 0;
}
Run on IDE
A Constructor called
Empty
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 WRONG
#include <iostream>
using namespace std;
class Test
{
public:
Test() { cout << "Hello from Test() "; }
} a;
int main()
{
cout << "Main Started ";
return 0;
}
Run on IDE
Main Started
B Main Started Hello from Test() ▲
Hello from Test() Main Started
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 8/13
1/18/2018 Constructors in C++ - GeeksQuiz
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.
You have completed 10/18 questions .
Your accuracy is 10%.
1 2
Company Wise Coding Practice Topic Wise Coding Practice
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Share this post !
9 Comments GeeksforGeeks
1 Login
Sort by Newest
Recommend 4 ⤤ Share
Join the discussion…
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
Peter John Morley • a month ago
Question #5 I have been playing around in Visual Studio 2015 and noticed that the t4 = t3 line
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 9/13
1/18/2018 Constructors in C++ - GeeksQuiz
invokes a copy constructor and then an assignment operator call. Is this a visual studio bug?
The output ends up being choice B but when I run it in your IDE the output ends up being
choice C. Any ideas why?
△ ▽ • Reply • Share ›
Arpit • 3 months ago
Question 12:Line 7 :: how come p is able to access x, as x is private variable, shoudn't it be
p.getX() instead of p.x ??
△ ▽ • Reply • Share ›
Aniruddha Katkar • 4 months ago
Question no: 17, if it is compiling then it must be because of copy elision. Read about it here:
http://www.geeksforgeeks.or...
△ ▽ • Reply • Share ›
digneshpr • 7 months ago
not sure which compiler you guys are using. Program errors out without const keyword in c++
14 and 98 .
059c9dafc0aee960c3227ca075fa3fd3.cpp:21:18: error: invalid initialization of non-const
reference of type 'Test&' from an rvalue of type 'Test'
Test t2 = fun();
△ ▽ • Reply • Share ›
Abhinandan Deka > digneshpr • 3 months ago
@digneshpr, I am using VS2013; Are you using GCC?
△ ▽ • Reply • Share ›
digneshpr > Abhinandan Deka • 3 months ago
Yes. I was running on ideone's online compiler which is gcc
△ ▽ • Reply • Share ›
Abhinandan Deka • 8 months ago
Question no: 17 is wrong.
The program compiles well. It is one of the best practice to declare copy c'tor as,
Test(const Test& rhs) but not mandatory to declare const.
1△ ▽ • Reply • Share ›
Jamey Siddiqui • 8 months ago
Answer to Q 17 is wrong, it compiles without Const as well
3△ ▽ • Reply • Share ›
Shamoel Ahmad > Jamey Siddiqui • 6 months ago ▲
Works fine without const in VS2015
△ ▽ • Reply • Share ›
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 10/13
1/18/2018 Constructors in C++ - GeeksQuiz
✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Privacy
@geeksforgeeks, Some rights reserved Contact Us! About Us! Careers! Privacy Policy
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 11/13
1/18/2018 Constructors in C++ - GeeksQuiz
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 12/13
1/18/2018 Constructors in C++ - GeeksQuiz
https://www.geeksforgeeks.org/c-plus-plus-gq/constructors-gq/ 13/13