0% found this document useful (0 votes)
247 views7 pages

MCQ-7 Constructors

This document contains 25 multiple choice questions about constructors and destructors in C++. The questions cover topics such as the return type and calling of constructors, how constructors provide initial values to objects, when destructors are invoked, the syntax of copy constructors, and more. Example code snippets are provided to demonstrate the behavior of different constructors.

Uploaded by

Catalyst Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
247 views7 pages

MCQ-7 Constructors

This document contains 25 multiple choice questions about constructors and destructors in C++. The questions cover topics such as the return type and calling of constructors, how constructors provide initial values to objects, when destructors are invoked, the syntax of copy constructors, and more. Example code snippets are provided to demonstrate the behavior of different constructors.

Uploaded by

Catalyst Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Page (1/7)

MCQ-5 Constructors And Destructors

1. What should be the return type of a constructor?


(a) void
(b) int
(c) It cannot have any return type
(d) It can have any return type

2. What type of values do constructors provide to the object?


(a) New values
(b) Initial values
(c) Dynamic values
(d) Constant values

3. How is the constructor function called?


(a) It is automatically called when the object is created.
(b) It is to be called soon after the object is created.
(c) It can be called anywhere in the program.
(d) It needs to be explicitly called before object creation.

4. Unless a copy constructor or an assignment operator is defined for an object,


__________ and
__________ work in a similar way.
(a) object declaration and creation
(b) constructor and member function
(c) initialization and assignment
(d) ordinary and default parameters

5. Whenever possible, the compiler takes a shortcut of copying the objects


__________.
(a) byte by byte, bit by bit
(b) char by char
(c) Member by member
(d) None

6. Destructors are invoked __________.


(a) explicitly when needed
(b) explicitly when object goes out of scope
(c) automatically when object goes out of scope
(d) automatically at the end of a program

7. How many constructors are possible to define?


(a) Only one
(b) Exactly two
(c) Any number of constructors
(d) Any number of constructors, though it is advisable to have as few constructors as
possible
Page (2/7)

8. Which of the following is the correct syntax of a copy constructor?


(a) Player (Player) ;
(b) Player (Player &, Player & ) ;
(c) Player (Player *) ;
(d) Player (Player &) ;

9. The constructor body cannot have __________.


(a) malloc statement
(b) new statement
(c) return statement
(d) exit statement

10. What will happen if we write following two statements in a program for a class
named Time?
Time (int hours = 0, int minutes = 0, int seconds =0) { }
Time ( ) { }
(a) Only second one is permissible
(b) Only fi rst one is permissible
(c) Compiler gives an error
(d) Linker gives an error

11. 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

12. 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

13. 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
Page (3/7)

14. #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

15. 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

16. #include<iostream>
using namespace std;
class X
{
public:
int x;
};
int main()
{
Page (4/7)

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

17. 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

18. What is the output of following program?


#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

19. What is the output of following program?


#include<iostream>
Page (5/7)

#include<string.h>
using namespace std;
class String
{
char *str;
public:
String(const char *s);
void change(int index, char c) { str[index] = c; }
char *get() { return str; }
};
String::String(const char *s)
{
int l = strlen(s);
str = new char[l+1];
strcpy(str, s);
}
int main()
{
String s1("matrix");
String s2 = s1;
s1.change(0, 'M');
cout << s1.get() << " ";
cout << s2.get();
}
a. Matrix matrix
b. Matrix Matrix
c. matrix matrix
d. Matrix Matrix

20. Predict the output of following program.


#include<iostream>
using namespace std;
class Point {
int x;
public:
Point(int x) { this->x = x; }
Point(const Point p) { x = p.x;}
int getX() { return x; }
};
int main()
{
Point p1(10);
Point p2 = p1;
cout << p2.getX();
return 0;
}
a. 10
b. Compiler Error: p must be passed by reference
c. Garbage value
d. None of the above

21. We must use initializer list in a constructor when


Page (6/7)

a. There is a reference variable in class


b. There is a constant variable in class
c. There is an object of another class. And the other class doesn't have default
constructor.
d. All of the above

22. Which of the following is true about constructors. 1) They cannot be virtual. 2)
They cannot be private. 3) They are automatically called by new operator.
a. All 1, 2, and 3
b. Only 1 and 3
c. Only 1 and 2
d. Only 2 and 3

23. Predict the output of following program.


#include<iostream>
using namespace std;
class Test
{
public:
Test();
};
Test::Test() {
cout << " Constructor Called. ";
}
void fun() {
static Test t1;
}
int main() {
cout << " Before fun() called. ";
fun();
fun();
cout << " After fun() called. ";
return 0;
}
a. Constructor Called. Before fun() called. After fun() called.
b. Before fun() called. Constructor Called. Constructor Called. After fun() called.
c. Before fun() called. Constructor Called. After fun() called.
d. Constructor Called. Constructor Called. After fun() called.Before fun() called.

24. Predict the output of following C++ program.


#include<iostream>
using namespace std;
class Test
{
private:
int x;
public:
Test(int x = 0) { this->x = x; }
void change(Test *t) { this = t; }
void print() { cout << "x = " << x << endl; }
};
int main()
Page (7/7)

{
Test obj(5);
Test *ptr = new Test (10);
obj.change(ptr);
obj.print();
return 0;
}
a. x = 5
b. x = 10
c. Compiler Error
d. Runtime Error

25. Predict the output of following program?


#include<iostream>
using namespace std;
class Test
{
public:
Test(Test &t) { }
Test() {}
};
Test fun()
{
cout << "fun() Called\n";
Test t;
return t;
}
int main()
{
Test t1;
Test t2 = fun();
return 0;
}
a. fun() Called
b. Empty Output
c. Compiler Error: Because copy constructor argument is non-const

You might also like