Challenge your understanding of Python's Object-Oriented Programming with these multiple-choice questions on encapsulation. Ideal for beginners, students, and job seekers to reinforce key OOP concepts.
Answer: B Explanation: Encapsulation bundles data and methods into a class and restricts direct access. (Learn with Yasir)
#___@@Answer: C
class Test:
def __init__(self):
self.__x = 10
obj = Test()
print(obj.__x)
Answer: C
Answer: B
Answer: B (Learn with Yasir)
name_name__name#nameAnswer: B
class A:
def __init__(self):
self.__x = 5
def get_x(self):
return self.__x
obj = A()
print(obj.get_x())
Answer: C
Answer: D
Answer: B (Learn with Yasir)
class Bank:
def __init__(self):
self.__balance = 100
def deposit(self, amt):
self.__balance += amt
def get_balance(self):
return self.__balance
b = Bank()
b.deposit(50)
print(b.get_balance())
Answer: B
class Student:
def __init__(self):
self.__marks = 90
obj = Student()
print(obj.__marks)
What is the correct fix?
__marks to marksobj._Student__marksprint(__marks)Answer: B
class Test:
def __init__(self):
self.__x = 10
def getx():
return self.__x
What is the correction?
self parameter to getx()Answer: A
class Bank:
def __init__(self):
self.__balance = 100
b = Bank()
b.__balance = 200
print(b.__balance)
What is the fix to correctly update balance?
b._Bank__balance = 200b.balance = 200Answer: A
class A:
def __init__(self):
self.__x = 5
def get_x(self):
return self.__x
obj = A
print(obj.get_x())
What is the correction?
obj = A()__x directlyAnswer: A
class Car:
def __init__(self):
self.speed = 100
What should be done to encapsulate speed?
__speedAnswer: A
Tutorials, Roadmaps, Bootcamps & Visualization Projects