0
0
Pythonprogramming~20 mins

Class definition syntax in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Class Syntax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple class attribute access
What is the output of this Python code?
Python
class Dog:
    sound = "bark"

print(Dog.sound)
Abark
BSound
Cdog
DNone
Attempts:
2 left
💡 Hint
Look at how class attributes are accessed directly from the class.
Predict Output
intermediate
2:00remaining
Output of instance method call
What is the output of this Python code?
Python
class Cat:
    def meow(self):
        return "meow"

c = Cat()
print(c.meow())
Ameow
BMeow
CNone
DTypeError
Attempts:
2 left
💡 Hint
Instance methods need to be called on an object instance.
Predict Output
advanced
2:00remaining
Output when __init__ is missing
What happens when you run this code?
Python
class Bird:
    def fly(self):
        return "flying"

b = Bird()
print(b.fly())
ANone
BTypeError: missing 1 required positional argument
CAttributeError: 'Bird' object has no attribute 'fly'
Dflying
Attempts:
2 left
💡 Hint
If __init__ is missing, Python uses a default constructor.
Predict Output
advanced
2:00remaining
Output of class with instance variable
What is the output of this code?
Python
class Fish:
    def __init__(self, name):
        self.name = name

f = Fish("Nemo")
print(f.name)
AAttributeError
BFish
CNemo
Dname
Attempts:
2 left
💡 Hint
Instance variables are set inside __init__ and accessed via self.
Predict Output
expert
2:00remaining
Output of class with method overriding and super()
What is the output of this code?
Python
class Animal:
    def speak(self):
        return "sound"

class Dog(Animal):
    def speak(self):
        return super().speak() + " bark"

d = Dog()
print(d.speak())
Abark
Bsound bark
Csound
DTypeError
Attempts:
2 left
💡 Hint
super() calls the parent class method inside the child method.