Question 21
EasyA BankAccount class exposes deposit() and withdraw() but keeps balance private. What does this mainly protect against?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
A BankAccount class exposes deposit() and withdraw() but keeps balance private. What does this mainly protect against?
Code outside the class setting the balance to any value it likes
Compilation errors
The class being inherited
The program running too slowly
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.__x = 5
def get(self):
return self.__x
a = A()
print(a.get())
Error
__x
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.__x = 5
a = A()
try:
print(a.__x)
except AttributeError:
print("error")
5
error
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 Account {
int bal = 100;
public:
void deposit(int x) {
if (x > 0) bal += x;
}
int get() { return bal; }
};
int main() {
Account a;
a.deposit(50);
a.deposit(-30);
cout << a.get();
}
100
120
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class T:
def __init__(self):
self._c = 0
@property
def c(self):
return self._c
@c.setter
def c(self, v):
if v >= 0:
self._c = v
t = T()
t.c = 5
t.c = -2
print(t.c)
-2
5
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
def fahrenheit(self):
return self.__c * 9 / 5 + 32
print(Temp(100).fahrenheit())
Error
212
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._x = 3
print(A()._x)
None
3
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()
try:
print(a.__balance)
except AttributeError:
print("Error: attribute not found")
Error: attribute not found
None
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()
print(a._Account__balance)
Error: attribute not found
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 Person {
int age = 0;
public:
void setAge(int a) { if (a >= 0) age = a; }
int getAge() { return age; }
};
int main() {
Person p;
p.setAge(-50);
cout << p.getAge();
return 0;
}
-50
50
Sign up free to see the answer and a step-by-step explanation.
None
5
None
__x
150
180
Error
0
180.0
212.0
Error
_x
0
100
100
None
0
Error