Question 31
MediumWhat is the default access level of a class member in Java when no modifier is written?
- 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 is the default access level of a class member in Java when no modifier is written?
private
protected
public
Package-private (visible within the same package)
Sign up free to see the answer and a step-by-step explanation.
What is a friend function in C++?
A function that is always public
A member function of two classes
A virtual function
A non-member function given access to a class's private and protected members
Sign up free to see the answer and a step-by-step explanation.
Which design best follows encapsulation for a bank account's balance?
A static public balance
A private balance with deposit() and withdraw() methods that check the amount
A public balance field anyone can set
A global balance variable
Sign up free to see the answer and a step-by-step explanation.
What is data hiding?
Restricting direct access to an object's fields from outside the class
Storing data in a database
Encrypting the program
Deleting unused data
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.__balance = 100
a = Account()
a.__balance = 500
print(a._Account__balance)
AttributeError
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):
self._c = 0
@property
def c(self):
return self._c
@c.setter
def c(self, v):
self._c = max(-273, v)
t = Temp()
t.c = -500
print(t.c)
0
AttributeError
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):
self._items = [1, 2]
def items(self):
return list(self._items)
r = R()
r.items().append(3)
print(len(r.items()))
2
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 Secret {
int code = 42;
friend void reveal(const Secret &s);
};
void reveal(const Secret &s) { cout << s.code; }
int main() {
Secret s;
reveal(s);
}
42
Garbage value
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 Wallet {
int money;
public:
Wallet() : money(0) {}
bool spend(int x) {
if (x > money) return false;
money -= x;
return true;
}
void add(int x) { if (x > 0) money += x; }
int get() const { return money; }
};
int main() {
Wallet w;
w.add(50);
w.add(-20);
bool ok = w.spend(70);
cout << ok << " " << w.get();
}
0 50
1 -40
Sign up free to see the answer and a step-by-step explanation.
A class has private int age and a public setAge(int a) that rejects values below 0. What benefit of encapsulation does this show?
Run-time polymorphism
Multiple inheritance
Faster execution
The object protects its own data from invalid values
Sign up free to see the answer and a step-by-step explanation.
500
100
-273
-500
3
Error
Compilation error
0
0 30
1 50