Question 51
MediumWhat does a private constructor make possible?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
What does a private constructor make possible?
Faster object creation
Automatic garbage collection
Controlling object creation, as in a singleton or a static factory
Multiple inheritance
Sign up free to see the answer and a step-by-step explanation.
In what order are members and bases initialised in C++?
Members first, then base classes
Base classes first, then members in their declaration order, then the constructor body runs
In the order written in the initialiser list
In alphabetical order
Sign up free to see the answer and a step-by-step explanation.
What is a delegating constructor in C++11?
A constructor that creates threads
A destructor that calls a constructor
A constructor that calls another constructor of the same class in its initialiser list
A constructor of the base class
Sign up free to see the answer and a step-by-step explanation.
Which members must be initialised in the initialiser list, not in the constructor body?
static members
int members
const members and reference members
public members
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 B {
B() {}
B(const B&) { cout << "Copy"; }
B& operator=(const B&) { cout << "Assign"; return *this; }
};
int main() {
B a;
B b = a;
return 0;
}
Copy Assign
Assign
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 B {
B() {}
B(const B&) { cout << "Copy"; }
B& operator=(const B&) { cout << "Assign"; return *this; }
};
int main() {
B a, b;
b = a;
return 0;
}
Nothing
Assign
Sign up free to see the answer and a step-by-step explanation.
What is the risk of a shallow copy for a class that owns a raw pointer to heap memory?
The copy uses less memory
Both copies point to the same memory, so both destructors free it (double free)
There is no risk
The program won't compile
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 M { M(int v) { cout << v << " "; } };
struct S {
M a, b, c;
S() : c(3), b(2), a(1) {}
};
int main() {
S s;
return 0;
}
3 2 1
1 3 2
Sign up free to see the answer and a step-by-step explanation.
What does RAII (Resource Acquisition Is Initialisation) mean in C++?
A resource is acquired in a constructor and released in the destructor, tying its lifetime to an object's scope
Resources are allocated at compile time
All objects are created on the heap
Destructors are never called
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):
print("init", end=" ")
def __del__(self):
print("del")
r = R()
del r
del
del init
Sign up free to see the answer and a step-by-step explanation.
Nothing
Copy
Copy
Copy Assign
2 1 3
1 2 3
init del
init