Challenge - 5 Problems
Object Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Python code creating an object?
Consider this class and object creation code. What will be printed when running it?
Python
class Car: def __init__(self, brand, year): self.brand = brand self.year = year my_car = Car('Toyota', 2020) print(my_car.brand, my_car.year)
Attempts:
2 left
💡 Hint
Look at how the __init__ method assigns values to self.brand and self.year.
✗ Incorrect
The __init__ method sets the brand and year attributes. Printing them shows 'Toyota 2020'.
❓ Predict Output
intermediate2:00remaining
What is the output of this code creating multiple objects?
This code creates two objects from the Person class. What will it print?
Python
class Person: def __init__(self, name): self.name = name p1 = Person('Alice') p2 = Person('Bob') print(p1.name, p2.name)
Attempts:
2 left
💡 Hint
Each object has its own name attribute set during creation.
✗ Incorrect
p1.name is 'Alice' and p2.name is 'Bob', so printing both shows 'Alice Bob'.
🔧 Debug
advanced2:00remaining
What error does this code raise when creating an object?
This code tries to create an object but has a mistake. What error will it raise?
Python
class Animal: def __init__(self, species): self.species = species pet = Animal()
Attempts:
2 left
💡 Hint
Check if the constructor requires any arguments and if they are provided.
✗ Incorrect
The __init__ method requires 'species' argument, but none was given, causing TypeError.
❓ Predict Output
advanced2:00remaining
What is the output of this code using a method in an object?
This code defines a class with a method. What will it print when calling greet()?
Python
class Greeter: def __init__(self, name): self.name = name def greet(self): return f'Hello, {self.name}!' g = Greeter('Sam') print(g.greet())
Attempts:
2 left
💡 Hint
The greet method returns a greeting string using the object's name attribute.
✗ Incorrect
Calling g.greet() returns 'Hello, Sam!' which is printed.
🧠 Conceptual
expert2:00remaining
How many objects are created after running this code?
Consider this code snippet. How many distinct objects are created in memory?
Python
class Box: def __init__(self, content): self.content = content b1 = Box('apple') b2 = Box('banana') b3 = b1 b4 = Box('apple')
Attempts:
2 left
💡 Hint
Remember that assigning b3 = b1 does not create a new object.
✗ Incorrect
b1, b2, and b4 create new Box objects. b3 just points to b1's object. So total 3 objects.