Question 61
HardWhat is the output of this code?
class A:
def ():
.__v =
():
():
().__init__()
.__v =
b = B()
((k k (b)))
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:
def ():
.__v =
():
():
().__init__()
.__v =
b = B()
((k k (b)))
['_A__v']
['__v']
['_A__v', '_B__v']
['_B__v']
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Account:
def __init__(self):
self._bal = 0
@property
def bal(self):
return self._bal
a = Account()
try:
a.bal = 100
except AttributeError:
print("read-only", a.bal)
read-only 100
0
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Temp:
def __init__(self, c):
self.c = c
@property
def f(self):
return self.c * 9 / 5 + 32
@f.setter
def f(self, v):
self.c = (v - 32) * 5 / 9
t = Temp(100)
t.f = 50
print(round(t.c, 2), t.f)
50 122.0
10.0 50.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;
class Box {
int w = 0;
friend class Inspector;
};
class Inspector {
public:
static void set(Box &b, int v) { b.w = v; }
static int get(const Box &b) { return b.w; }
};
int main() {
Box b;
Inspector::set(b, 9);
cout << Inspector::get(b);
}
9
Garbage value
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
#include <vector>
using namespace std;
#include <vector>
class Bag {
vector<int> items{1, 2, 3};
public:
const vector<int>& view() const { return items; }
vector<int> copy() const { return items; }
};
int main() {
Bag b;
vector<int> c = b.copy();
c.push_back(4);
cout << b.view().size() << c.size();
}
43
34
Sign up free to see the answer and a step-by-step explanation.
A Java class has a private List<String> field and a getter that returns it directly. What is the safest fix?
Return Collections.unmodifiableList(list) or a copy
Make the getter static
Remove the field
Make the field public
Sign up free to see the answer and a step-by-step explanation.
Why do Java beans' setters sometimes violate encapsulation?
They hide fields completely
They prevent serialisation
They expose every field for modification, often without validation
They make fields final
Sign up free to see the answer and a step-by-step explanation.
Which access level in Java is the most restrictive that still allows subclasses in other packages to use a member?
public
protected
default
private
Sign up free to see the answer and a step-by-step explanation.
In Kotlin and C#, what does an 'internal' member mean?
It is private
It is public
It is visible only to subclasses
It is visible only within the same module or assembly
Sign up free to see the answer and a step-by-step explanation.
What is the 'tell, don't ask' principle?
Never call methods
Ask the user for input
Ask objects to do work with their data instead of pulling the data out and deciding for them
Always use getters
Sign up free to see the answer and a step-by-step explanation.
read-only 0
100
100 212.0
10 50
0
Compilation error
44
33