Question 61
HardWhat is the output of this code?
class A:
x = []
def ():
.x = .x + [v]
a, b = A(), A()
(a.x, b.x, A.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?
class A:
x = []
def ():
.x = .x + [v]
a, b = A(), A()
(a.x, b.x, A.x)
[1] [2] [1, 2]
[1, 2] [1, 2] [1, 2]
[1] [1, 2] [1, 2]
[1] [2] []
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class A:
x = []
def __init__(self, v):
self.x += [v]
a, b = A(1), A(2)
print(a.x, b.x, A.x)
[1, 2] [1, 2] [1, 2]
[1] [2] [1, 2]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class P:
__slots__ = ("x",)
def __init__(self):
self.x = 1
p = P()
try:
p.y = 2
print("ok")
except AttributeError:
print("blocked", p.x)
blocked 1
blocked 2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class C:
count = 0
def __new__(cls, *a):
cls.count += 1
return super().__new__(cls)
def __init__(self, v):
self.v = v
objs = [C(i) for i in range(3)]
print(C.count, objs[2].v)
3 2
1 2
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 {
public:
static int live;
T() { live++; }
T(const T&) { live++; }
~T() { live--; }
};
int T::live = 0;
T make() { T t; return t; }
int main() {
T a;
{
T b = a;
T c;
cout << T::live << " ";
}
T d = make();
cout << T::live;
}
3 2
3 3
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 { char c; int i; char d; };
struct U { char c; char d; int i; };
int main() {
cout << (sizeof(S) > sizeof(U));
}
Compilation error
0
Sign up free to see the answer and a step-by-step explanation.
Two objects of a class compare equal with == in Java but are different objects. What must the class do for HashSet to treat them as the same element?
Override only equals()
Override both equals() and hashCode() consistently
Make all fields public
Override only toString()
Sign up free to see the answer and a step-by-step explanation.
What is the difference between a shallow and a deep copy of an object that holds a list?
A shallow copy shares the list with the original; a deep copy gets its own copy of the list
They are the same
A shallow copy copies nothing
A deep copy shares the list
Sign up free to see the answer and a step-by-step explanation.
Why should a value class like Money be immutable?
It can be shared safely between threads and used as a map key without risk of changing
It makes methods faster
It uses less memory
Java requires it
Sign up free to see the answer and a step-by-step explanation.
What does the Java keyword 'this' refer to inside a static method?
Nothing; static methods have no 'this', so using it is a compile error
The first object created
The class
null
Sign up free to see the answer and a step-by-step explanation.
[1] [2] []
[1] [1, 2] [1, 2]
ok
AttributeError
0 2
3 3
2 2
3 4
It depends on the values
1