PCAP Object-Oriented Programming 2 — Questions and Answers
Question 1: What does the `super()` function do in Python?
- Creates a superclass
- Calls a method from the parent class (Correct answer)
- Deletes the parent class
- Converts to a supertype
Correct answer: Calls a method from the parent class
`super()` returns a proxy object that delegates method calls to the parent class.
Question 2: What is method overriding in Python OOP?
- Defining a method with the same name in a subclass (Correct answer)
- Deleting a parent class method
- Adding extra parameters to a method
- Calling a method twice
Correct answer: Defining a method with the same name in a subclass
Method overriding means redefining a parent class method in a child class with the same name.
Question 3: Which of the following correctly defines a subclass `Dog` inheriting from `Animal`?
- class Dog extends Animal:
- class Dog(Animal): (Correct answer)
- class Dog inherits Animal:
- class Dog: Animal
Correct answer: class Dog(Animal):
Python uses parentheses after the class name to specify the parent class for inheritance.
Question 4: What is polymorphism in Python?
- A class with many attributes
- Multiple classes responding to the same interface (Correct answer)
- A method with default parameters
- Using multiple inheritance
Correct answer: Multiple classes responding to the same interface
Polymorphism allows different classes to be used through the same interface by implementing the same methods.
Question 5: How do you define a class variable (shared by all instances) in Python?
- Declare it inside __init__ using self
- Declare it outside any method, inside the class body (Correct answer)
- Use the global keyword
- Use the shared keyword
Correct answer: Declare it outside any method, inside the class body
Class variables are defined in the class body outside any method and are shared by all instances.
Question 6: What will `isinstance(obj, MyClass)` return if `obj` is an instance of a subclass of `MyClass`?
- False
- True (Correct answer)
- None
- TypeError
Correct answer: True
`isinstance()` returns `True` if the object is an instance of the class or any of its subclasses.
What does the `super()` function do in Python?