Which of the following is the main reason programmers use object-oriented programming (OOP)?
Think about how OOP helps manage complexity by grouping data and actions.
OOP helps programmers organize code by bundling data and related actions into objects, making programs easier to understand and reuse.
What will be the output of this Python code?
class Dog: def __init__(self, name): self.name = name def speak(self): return f"{self.name} says Woof!" my_dog = Dog("Buddy") print(my_dog.speak())
Look at how the speak method uses the name attribute.
The speak method returns the dog's name followed by 'says Woof!'. Since the dog's name is 'Buddy', the output is 'Buddy says Woof!'.
Consider this Python code using inheritance. What is printed?
class Animal: def speak(self): return "Some sound" class Cat(Animal): def speak(self): return "Meow" class Dog(Animal): pass cat = Cat() dog = Dog() print(cat.speak()) print(dog.speak())
Check which classes override the speak method.
The Cat class overrides speak to return 'Meow'. The Dog class does not override speak, so it uses Animal's speak returning 'Some sound'.
Which statement best explains why encapsulation is important in object-oriented programming?
Think about how encapsulation helps keep data safe inside objects.
Encapsulation hides the internal state of an object and only exposes what is necessary, protecting data from accidental changes.
What is the key benefit of polymorphism in object-oriented programming?
Think about how polymorphism helps write flexible code that works with many object types.
Polymorphism lets different classes use the same method names, so code can work with objects of different types without knowing their exact class.