Challenge - 5 Problems
OOP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code using inheritance?
Consider this Python code that uses inheritance. What will it print when run?
Python
class Animal: def sound(self): return "Some sound" class Dog(Animal): def sound(self): return "Bark" pet = Dog() print(pet.sound())
Attempts:
2 left
💡 Hint
Think about which method is called when the child class overrides a method from the parent.
✗ Incorrect
The Dog class overrides the sound method from Animal. So calling pet.sound() uses Dog's version, printing Bark.
❓ Predict Output
intermediate2:00remaining
What does this code print demonstrating encapsulation?
Look at this Python class using encapsulation. What will be printed?
Python
class BankAccount: def __init__(self, balance): self.__balance = balance def get_balance(self): return self.__balance account = BankAccount(100) print(account.__balance)
Attempts:
2 left
💡 Hint
Check how private variables are accessed outside the class.
✗ Incorrect
The variable __balance is private and cannot be accessed directly outside the class, causing AttributeError.
🧠 Conceptual
advanced2:00remaining
Which principle does this code best demonstrate?
This code shows a class with a method that behaves differently depending on the object. Which OOP principle is this?
Python
class Shape: def area(self): return 0 class Square(Shape): def __init__(self, side): self.side = side def area(self): return self.side * self.side class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * self.radius * self.radius
Attempts:
2 left
💡 Hint
Think about how the same method name works differently for different classes.
✗ Incorrect
Polymorphism allows methods with the same name to behave differently depending on the object calling them.
❓ Predict Output
advanced2:00remaining
What is the output of this code demonstrating abstraction?
What will this code print when run?
Python
from abc import ABC, abstractmethod class Vehicle(ABC): @abstractmethod def start(self): pass class Car(Vehicle): def start(self): return "Car started" my_car = Car() print(my_car.start())
Attempts:
2 left
💡 Hint
Look at how abstract methods are implemented in child classes.
✗ Incorrect
The abstract method start is implemented in Car, so calling my_car.start() returns Car started.
❓ Predict Output
expert2:00remaining
What is the value of x after running this code with multiple inheritance?
Consider this code with multiple inheritance. What is the value of x after execution?
Python
class A: def __init__(self): self.x = 1 class B(A): def __init__(self): super().__init__() self.x = 2 class C(A): def __init__(self): super().__init__() self.x = 3 class D(B, C): def __init__(self): super().__init__() obj = D() x = obj.x
Attempts:
2 left
💡 Hint
Check the method resolution order (MRO) for class D.
✗ Incorrect
Class D inherits from B then C. super().__init__() in D calls B's __init__, which sets x=2. So x is 2.