1/1/2025 OOP Practice
ADP WDD – 2 (Python)
Syed Muhammad Zafar Rizvi
HAMDARD UNIVERISTY
Multiple Choice Questions (MCQs)
1. In Python, what is the purpose of a constructor?
A) To define methods in a class B) To initialize an object’s attributes
C) To destroy objects D) To inherit methods from a parent class
Answer: B
2. Which method is called automatically when an object is created in Python?
A) __str__() B) __init__()
C) __new__() D) __call__()
Answer: B
3. Encapsulation in Object-Oriented Programming is achieved using:
A) Interfaces B) Methods
C) Access Modifiers D) Constructors
Answer: C
4. What is __init__() function called?
A) Constructor B) Objects
C) Class D) None of These
Answer: A
5. What type of function does not have a body in Python?
A) Getter Function B) Abstract Function
C) Static Function D) Constructor
Answer: B
6. Which keyword is used to access a parent class in Python?
A) super B) parent
C) base D) class
Answer: A
7. What does the following code snippet do?
A) Prints “A” B) Prints “B”
C) Raises an error D) Prints both “A” and “B”
Answer: B
8. Which OOP concept does this code snippet use?
A) Method Over-Loading B) Inheritance
C) Method Over-Ridding D) Constructor
Answer: C
9. Which of the following statements is correct about the super() function?
A) It is used to initialize the child class constructor
B) It gives access to the parent class's methods and attributes
C) It can only be used in single inheritance
D) It must always be the first line in a method
Answer: B
10. Polymorphism allows:
A) Multiple objects to have the same name
B) One method to perform different tasks
C) Multiple methods to have the same name but different parameters
D) None of the above
Answer: B
True/False Questions
11. Private members of a class can only be accessed within the same class.
Answer: True
12. A constructor in Python can return a value.
Answer: False (True Answer: A constructor in Python cannot return a value; it is only for initializing
an object.)
13. Encapsulation ensures data hiding by making variables private.
Answer: True
14. Inheritance is a feature of OOP, which is based on reusability of code.
Answer: True
15. Class is an instance of an Object.
Answer: False (True Answer: An Object is an instance of a Class.)
16. Protected variables or functions can be accessed outside of the class.
Answer: False (True Answer: Protected variables or functions are intended to be accessed only
within the class and its subclasses.)
Note: In Python, protected variables or functions (indicated by a single underscore prefix) are intended to be
accessed only within the class and its subclasses, but they can still be accessed outside the class if needed, as
Python does not enforce strict access restrictions.
17. Private members of a class can be accessed by anyone outside the class, but not by other members of
the class.
Answer: False (True Answer: Private members of a class cannot be accessed directly outside the
class but can be accessed within the class and its methods. They can also be accessed outside the
class indirectly through public getter or setter methods.).
18. A child class in Python can access private members of its parent class directly.
Answer: False (True Answer: A child class in Python access only public members of its parent class
directly, private members can be accessed only with in the class.)
19. A method defined in child class overrides the method of parent class if they have same name.
Answer: True
20. Overloading methods is allowed in Python by defining multiple methods with the same name but
different parameters.
Answer: False (True Answer: Python does not support method overloading in the traditional sense. If
multiple methods with the same name are defined, the last one will overwrite the previous ones.
However, default arguments or variable-length arguments can be used to simulate overloading.)
Fill in the Blanks
21. In Python, all methods within a class must have at least one parameter, ____________ to access the
instance's properties.
Answer: self
22. Function ____________ occurs when multiple methods with the same name but different parameters
are used in a class, but Python does not support this feature directly.
Answer: Function Over-Loading / Compile-time Polymorphism
23. In OOP, the concept of ________ allows a class to define structure, without providing the
implementation of its functions.
Answer: Abstraction
24. Method Over-Riding is also called __________________.
Answer: Run-Time Polymorphism
25. We can access all _________ members (functions & variables) outside and inside the class.
Answer: Public
26. Changing a function in a derived class to provide a new function replacing the base class's function,
is called ________________.
Answer: Function Over-Riding / Run-time Polymorphism
27. When creating an object of a class, the auto-call function used to initialize the object is called _____.
Answer: Constructor
28. In Python, the _________ function is used to call a method from a parent class in a child class, even
when the method is overridden.
Answer: super
29. Method Over-Loading is also called ________________.
Answer: Compile-Time Polymorphism
30. There are ______ Pillars of OOP (Object Oriented Programming) _________, _________,
_________ and __________.
Answer: 4, Polymorphism, Inheritance, Abstraction and Encapsulation.
Kill the Bug
Find and correct the errors also show the output.
31.
Class Car:
def __init__(make, model):
self.make = make
self.model = model
car1 = Car("Toyota", "Camry")
print(car1.make)
Answer: in class initialization “Class” should be “class”, self parameter missing in constructor
Output: “Toyota”
32. class Calculator:
def add(self, a, b):
return a + b
def add(self, a, b, c):
return a + b + c
calc = Calculator()
print(calc.add(2, 3))
Answer: Method Overloading is not supported in Python. Therefore, we either need to remove the
second method or include a third parameter in the function call.
Output: 5 or depends on 3rd parameter given.
33. class Person:
def __init__(self, name):
self.name = name
p = Person("Alice")
print(p.Name)
Answer: in Last line >>> print(p.name)
Output: ‘Alice’
34. class Animal:
def __init__(self, sound):
self.sound = sound
def speak(self):
print(f"{self.sound} makes a sound")
class Dog(Animal):
def speak(self):
print(f"{self.sound} barks")
dog = Dog("Woof")
dog.speak()
Answer: No error
Output: ‘Woof Barks’
35. class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def display(self):
return f"Car make: {self.make}, Model: {self.model}"
car = Car("Toyota")
print(car.display())
Answer: second argument in Constructor call missing.
Output: ‘Woof Barks’
36. class Football(Ball):
def __init__(self, radius, material):
self.size = radius:
self.material = material:
class Ball:
def __init__(self):
pass
Answer: Parent class (Ball) should be before child class (Football), “:” will not come while
assigning values to variables
Output: No Output
37. class Phone(self, companyName):
def main():
print(“This is a ” + companyName + “ Phone”)
phoneObj = Phone(“Huawei”)
phoneObj.main()
Answer: Classes don’t have an argument, functions do.
Output: ‘This is a Huawei Phone’
Important Questions
38. What are getters/setters. Make a Program to show them.
Define the following with examples
39. Classes
40. Objects
41. Polymorphism
42. Abstraction
43. Encapsulation
44. Inheritance
45. Constructor
46. Base Class
47. Derived Class
48. Public
49. Private
50. Protected