Question 31
MediumCan a constructor be private?
- A.
No, never
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
Can a constructor be private?
No, never
Only if the class has no fields
Only in Python
Yes, e.g. to stop direct creation in the singleton pattern
Sign up free to see the answer and a step-by-step explanation.
What is constructor chaining in Java?
One constructor calling another using this(...) or super(...)
Calling a destructor from a constructor
Creating objects in a loop
Linking two classes
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
struct P {
int x, y;
P(int a = 0, int b = 0) : x(a), y(b) {}
};
int main() {
P p, q(5), r(5, 7);
cout << p.x << " " << p.y << " " << q.x << " " << q.y << " " << r.x << " " << r.y;
return 0;
}
Error
0 0 5 0 5 7
Sign up free to see the answer and a step-by-step explanation.
Why can't a constructor be virtual in C++?
The object's type (and vtable) isn't set up until the constructor runs, so there is nothing to dispatch on
Constructors are always private
Virtual functions must return values
It is allowed in C++
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
class T {
int id;
public:
T(int i) : id(i) { cout << "C" << id << " "; }
~T() { cout << "D" << id << " "; }
};
int main() {
T a(1);
{
T b(2);
}
T c(3);
}
C1 C2 D2 C3 D3 D1
C1 C2 C3 D3 D2 D1
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "D "; }
A(const A &) { cout << "C "; }
};
void f(A x) {}
int main() {
A a;
f(a);
A b = a;
}
D C C
D C
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
class M {
public:
int a, b;
M() : b(5), a(b + 1) {}
};
int main() {
M m;
cout << (m.b == 5);
}
1
Compilation error
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
class P {
public:
int v;
P() : v(0) { cout << "default "; }
P(int x) : v(x) { cout << "int "; }
};
int main() {
P a;
P b(4);
P c = 7;
P arr[2];
}
default int default
int int int default default
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class R:
def __init__(self, n):
self.n = n
print("open", n, end="; ")
def __enter__(self):
return self
def __exit__(self, *a):
print("close", self.n, end="; ")
with R(1) as r:
print("use", r.n, end="; ")
use 1; open 1; close 1;
open 1; use 1;
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class A:
def __init__(self, x=[]):
x.append(1)
self.x = x
a = A(); b = A()
print(len(b.x))
2
Error
Sign up free to see the answer and a step-by-step explanation.
5 7 5 7 5 7
0 0 5 5 5 7
C1 C2 C3 D1 D2 D3
C1 C2 D2 C3 D1 D3
D C D
D D D
0
6
default int int
default int int default default
open 1; close 1; use 1;
open 1; use 1; close 1;
0
1