MCQ-7 Constructors
MCQ-7 Constructors
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
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
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
#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
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
{
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