OOPs Concepts In Python. MCQs on OOPs Concepts In Python Programming Language.
OOPs Concepts In Python
What will be the output of the following Python code ??
class A:
def __init__(self):
self._x = 5
class B(A):
def display(self):
print(self._x)
def main():
obj = B()
obj.display()
main()
a) Error, invalid syntax for object declaration
b) Nothing is printed
c) 5
d) Error, private class member can’t be accessed in a subclass
What will be the output ??
class Test:
def __init__(self):
self.x = 0
class Derived_Test(Test):
def __init__(self):
Test.__init__(self)
self.y = 1
def main():
b = Derived_Test()
print(b.x,b.y)
main()
a) Error because class B inherits A but variable x isn’t inherited
b) 0 0
c) 0 1
d) Error, the syntax of the invoking method is wrong
The purpose of name mangling is to avoid unintentional access of private class members ??
a) True
b) False
What will be the output of the code ??
class A:
def __init__(self):
self.multiply(15)
def multiply(self, i):
self.i = 4 * i;
class B(A):
def __init__(self):
super().__init__()
print(self.i)
def multiply(self, i):
self.i = 2 * i;
obj = B()
a) 15
b) 30
c) An exception is thrown
d) 60
OOPs Concepts In Python
What is the output ??
class A:
def __init__(self):
self.__x = 1
class B(A):
def display(self):
print(self.__x)
def main():
obj = B()
obj.display()
main()
a) 1
b) 0
c) Error, invalid syntax for object declaration
d) Error, private class member can’t be accessed in a subclass
Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write ??
a) A.__init__(self)
b) B.__init__(self)
c) A.__init__(B)
d) B.__init__(A)
Private members of a class cannot be accessed ??
a) True
b) False
What will be the output of the ??
class Demo:
def check(self):
return ” Demo’s check ”
def display(self):
print(self.check())
class Demo_Derived(Demo):
def check(self):
return ” Derived’s check “
Demo().display()
Demo_Derived().display()
a) Demo’s check Derived’s check
b) Demo’s check Demo’s check
c) Derived’s check Demo’s check
d) Syntax error
OOPs Concepts In Python
Method issubclass() checks if a class is a subclass of another class ??
a) True
b) False
When defining a subclass in Python that is meant to serve as a subtype, the subtype Python keyword is used ??
a) True
b) False
What will be the output of the following Python code ??
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def get(self):
return self.__b
obj = Demo()
obj.a=45
print(obj.a)
a) The program runs properly and prints 45
b) The program has an error because the value of members of a class can’t be changed from outside the class
c) The program runs properly and prints 1
d) The program has an error because the value of members outside a class can only be changed as self.a=45
What will be the output of the following Python ??
class A:
def __init__(self):
self.multiply(15)
print(self.i)
def multiply(self, i):
self.i = 4 * i;
class B(A):
def __init__(self):
super().__init__()
def multiply(self, i):
self.i = 2 * i;
obj = B()
a) 15
b) 60
c) An exception is thrown
d) 30
OOPs Concepts In Python
Which of the following statements is true ??
a) The __new__() method automatically invokes the __init__ method
b) The __init__ method is defined in the object class
c) The __eq(other) method is defined in the object class
d) The __repr__() method is defined in the object class
All subclasses are a subtype in object-oriented programming ??
a) True
b) False
What will be the output ??
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def get(self):
return self.__b
obj = Demo()
print(obj.get())
a) The program has an error because there isn’t any function to return self.a
b) The program has an error because b is private and display(self) is returning a private member
c) The program has an error because b is private and hence can’t be printed
d) The program runs fine and 1 is printed