Question 61
HardWhat is the output of this code?
#include <iostream>
using std;
{
:
( i) { cout << << i << ; }
};
{
M b, a;
:
() : (), () { cout << ; }
};
{
X x;
}
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
What is the output of this code?
#include <iostream>
using std;
{
:
( i) { cout << << i << ; }
};
{
M b, a;
:
() : (), () { cout << ; }
};
{
X x;
}
M2 M1 X
M1 M2 X
X M1 M2
M2 X M1
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
#include <utility>
using namespace std;
class P {
public:
P() { cout << "d "; }
P(const P&) { cout << "c "; }
P(P&&) noexcept { cout << "m "; }
P& operator=(const P&) { cout << "ca "; return *this; }
P& operator=(P&&) noexcept { cout << "ma "; return *this; }
};
int main() {
P a;
P b = a;
P c = std::move(a);
b = c;
c = P();
}
d c m ca d ma
d c c ca d ca
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 R {
int id;
public:
R(int i) : id(i) { cout << "+" << id; }
~R() { cout << "-" << id; }
};
R g(1);
int main() {
R a(2);
static R s(3);
R b(4);
}
+1+2+3+4-4-2-3-1
+1+2+3+4-1-2-3-4
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
#include <memory>
using namespace std;
#include <memory>
class N {
public:
int v;
N(int x) : v(x) { cout << "N" << v; }
~N() { cout << "~N" << v; }
};
int main() {
auto p = make_shared<N>(1);
{
auto q = p;
cout << p.use_count();
}
cout << p.use_count();
p.reset();
cout << "end";
}
N11~N1end
N121end~N1
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
def __enter__(self):
print("in", self.n, end=" ")
return self
def __exit__(self, et, ev, tb):
print("out", self.n, end=" ")
return True
with R(1), R(2):
raise ValueError
print("after")
in 1 in 2 out 1 out 2 after
in 1 out 1 in 2 out 2 after
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):
self.ready = False
self.setup()
def setup(self):
self.ready = True
class B(A):
def __init__(self):
self.extra = []
super().__init__()
def setup(self):
self.extra.append("x")
super().setup()
b = B()
print(b.ready, b.extra)
True ['x']
False []
Sign up free to see the answer and a step-by-step explanation.
When does C++ generate a move constructor automatically?
Never
Only for structs
Always
Only if the class declares no copy operations, no move operations and no destructor of its own
Sign up free to see the answer and a step-by-step explanation.
Why should move constructors usually be marked noexcept?
It makes them faster to compile
It prevents copying
It is required by the language
Standard containers such as vector only move elements during reallocation if the move can't throw
Sign up free to see the answer and a step-by-step explanation.
What is the copy-and-swap idiom?
Swapping two objects' names
Copying memory with memcpy
Implementing assignment by copying the argument and swapping its contents with *this, giving strong exception safety
A way to avoid constructors
Sign up free to see the answer and a step-by-step explanation.
What is RAII?
A garbage collector
Random access to arrays
Tying a resource's lifetime to an object: acquire it in the constructor and release it in the destructor
A design pattern for UIs
Sign up free to see the answer and a step-by-step explanation.
d c m ma d ma
d d d ca d ma
+1+2+3+4-4-3-2-1
+2+3+4-4-3-2+1-1
N12~N11end
N121~N1end
in 1 in 2 out 2 out 1 after
in 1 in 2 ValueError
True []
AttributeError