Question 21
EasyWhat is the output of this code?
#include <iostream>
using std;
{
c;
( x) : (x) { cout << c; }
~() { cout << c; }
};
{
;
;
}
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;
{
c;
( x) : (x) { cout << c; }
~() { cout << c; }
};
{
;
;
}
12
1221
1212
2112
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 Box {
public:
int v;
Box() : v(1) {}
Box(int x) : v(x) {}
};
int main() {
Box a, b(5);
cout << a.v + b.v;
}
6
1
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 {
int x, y;
public:
P(int a) : x(a), y(a * 2) {}
int sum() { return x + y; }
};
int main() {
P p(4);
cout << p.sum();
}
8
12
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 S {
S() { cout << "C"; }
~S() { cout << "D"; }
};
int main() {
cout << "1";
{
S s;
cout << "2";
}
cout << "3";
}
12C3D
1C23D
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class K:
count = 0
def __init__(self):
K.count += 1
K()
K()
K()
print(K.count)
3
0
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 A {
A() {}
A(const A&) { cout << "copy "; }
};
void f(A a) {}
int main() {
A x;
f(x);
cout << "done";
}
copy done
copy copy done
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class R:
def __del__(self):
print("bye", end=" ")
r = R()
del r
print("end")
end bye
end
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 X {
char n;
X(char c) : n(c) { cout << (char)(n - 32) << " "; }
~X() { cout << n << " "; }
};
int main() {
X a('a');
X b('b');
return 0;
}
B A b a
A B a b
Sign up free to see the answer and a step-by-step explanation.
What is a copy constructor?
A constructor that creates an object as a copy of another object of the same class
A constructor that copies a file
A constructor that takes no parameters
A destructor that copies data
Sign up free to see the answer and a step-by-step explanation.
When is a copy constructor called in C++? (Select all that apply.)
When an object is initialised from another object of the same class
When an object is passed to a function by value
When an object is passed by reference
When an object is returned by value (unless the copy is elided)
Sign up free to see the answer and a step-by-step explanation.
15
5
16
4
C12D3
1C2D3
1
Error
done
Compilation error
bye end
bye
A B b a
A a B b