Bird
Raised Fist0
Pythonprogramming~20 mins

Class attributes in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Class Attribute Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of class attribute access
What is the output of this code?
Python
class Dog:
    species = "Canis familiaris"

    def __init__(self, name):
        self.name = name

dog1 = Dog("Buddy")
dog2 = Dog("Max")
print(dog1.species)
print(dog2.species)
print(Dog.species)
AError: species is not defined
B
Canis familiaris
Canis familiaris
Canis familiaris
C
Canis familiaris
Max
Buddy
D
Buddy
Max
Canis familiaris
Attempts:
2 left
💡 Hint
Class attributes are shared by all instances unless overridden.
Predict Output
intermediate
2:00remaining
Changing class attribute affects instances
What is the output of this code?
Python
class Car:
    wheels = 4

car1 = Car()
car2 = Car()
Car.wheels = 6
print(car1.wheels)
print(car2.wheels)
A
4
4
BError: cannot assign to wheels
C
4
6
D
6
6
Attempts:
2 left
💡 Hint
Changing a class attribute changes it for all instances that don't override it.
Predict Output
advanced
2:00remaining
Instance attribute shadows class attribute
What is the output of this code?
Python
class Book:
    genre = "Fiction"

b1 = Book()
b2 = Book()
b2.genre = "Non-Fiction"
print(b1.genre)
print(b2.genre)
print(Book.genre)
A
Fiction
Fiction
Fiction
B
Non-Fiction
Non-Fiction
Non-Fiction
C
Fiction
Non-Fiction
Fiction
DError: genre attribute missing
Attempts:
2 left
💡 Hint
Instance attributes override class attributes with the same name.
Predict Output
advanced
2:00remaining
Modifying mutable class attribute affects all instances
What is the output of this code?
Python
class Team:
    members = []

team1 = Team()
team2 = Team()
team1.members.append("Alice")
print(team1.members)
print(team2.members)
print(Team.members)
A
['Alice']
['Alice']
['Alice']
B
['Alice']
[]
[]
C
[]
[]
[]
DError: cannot append to members
Attempts:
2 left
💡 Hint
Mutable class attributes are shared by all instances.
🧠 Conceptual
expert
2:30remaining
Class attribute behavior with inheritance
Given this code, what is the output?
Python
class Animal:
    sound = "Generic"

class Cat(Animal):
    pass

class Dog(Animal):
    sound = "Bark"

print(Animal.sound)
print(Cat.sound)
print(Dog.sound)
A
Generic
Generic
Bark
B
Generic
Bark
Bark
C
Generic
Generic
Generic
DError: sound attribute missing in Cat
Attempts:
2 left
💡 Hint
Child classes inherit class attributes unless they override them.

Practice

(1/5)
1. What is a class attribute in Python?
class Car:
wheels = 4
Here, what does wheels represent?
easy
A. A value shared by all Car objects
B. A value unique to each Car object
C. A method inside the Car class
D. A variable defined inside a method

Solution

  1. Step 1: Identify the attribute location

    The attribute wheels is defined inside the class but outside any method.
  2. Step 2: Understand class attribute behavior

    Attributes defined this way are shared by all instances of the class.
  3. Final Answer:

    A value shared by all Car objects -> Option A
  4. Quick Check:

    Class attribute = shared value [OK]
Hint: Class attributes are outside methods, shared by all instances [OK]
Common Mistakes:
  • Thinking class attributes are unique per object
  • Confusing class attributes with instance attributes
  • Assuming class attributes are methods
2. Which of the following is the correct way to define a class attribute color with value "red" inside a class Fruit?
easy
A. class Fruit: def __init__(self): color = "red"
B. class Fruit: def color(self): return "red"
C. class Fruit: def __init__(self, color): self.color = color
D. class Fruit: color = "red"

Solution

  1. Step 1: Identify class attribute syntax

    A class attribute is defined directly inside the class, outside any method.
  2. Step 2: Check each option

    class Fruit: color = "red" correctly defines color as a class attribute. class Fruit: def __init__(self): color = "red" defines a local variable inside __init__. class Fruit: def color(self): return "red" defines a method, not an attribute. class Fruit: def __init__(self, color): self.color = color defines an instance attribute.
  3. Final Answer:

    class Fruit: color = "red" -> Option D
  4. Quick Check:

    Class attribute = direct assignment inside class [OK]
Hint: Class attributes are assigned directly inside class, outside methods [OK]
Common Mistakes:
  • Defining attribute inside __init__ without self
  • Confusing methods with attributes
  • Using self for class attributes
3. What will be the output of this code?
class Dog:
    species = "Canine"

dog1 = Dog()
dog2 = Dog()
Dog.species = "Wolf"
print(dog1.species)
print(dog2.species)
medium
A. Canine\nCanine
B. Wolf\nCanine
C. Wolf\nWolf
D. Canine\nWolf

Solution

  1. Step 1: Understand class attribute change

    The attribute species is a class attribute shared by all instances.
  2. Step 2: Effect of changing class attribute

    Changing Dog.species to "Wolf" updates the value for all instances that don't have their own species attribute.
  3. Final Answer:

    Wolf\nWolf -> Option C
  4. Quick Check:

    Changing class attribute affects all instances [OK]
Hint: Changing class attribute changes it for all instances without override [OK]
Common Mistakes:
  • Thinking instances keep old class attribute values
  • Confusing instance and class attributes
  • Expecting different outputs for dog1 and dog2
4. Find the error in this code snippet:
class Book:
    pages = 100

    def __init__(self, pages):
        pages = pages

b = Book(200)
print(b.pages)
medium
A. The instance attribute pages is not set properly
B. The class attribute pages is overwritten incorrectly
C. Syntax error in __init__ method
D. Cannot print pages attribute directly

Solution

  1. Step 1: Analyze __init__ method

    The line pages = pages assigns the parameter to itself, not to the instance.
  2. Step 2: Understand instance attribute setting

    To set an instance attribute, it should be self.pages = pages. Without self., the instance attribute is not created.
  3. Final Answer:

    The instance attribute pages is not set properly -> Option A
  4. Quick Check:

    Use self.pages to set instance attribute [OK]
Hint: Use self.attribute to set instance attributes inside __init__ [OK]
Common Mistakes:
  • Forgetting self. when assigning instance attributes
  • Assuming parameter assignment sets instance attribute
  • Confusing class and instance attributes
5. You want to keep track of how many objects of class Student are created using a class attribute count. Which code correctly updates count each time a new Student is made?
hard
A. class Student: count = 0 def __init__(self): self.count += 1
B. class Student: count = 0 def __init__(self): Student.count += 1
C. class Student: def __init__(self): count = 0 count += 1
D. class Student: count = 0 def __init__(self): count += 1

Solution

  1. Step 1: Identify class attribute usage

    count is a class attribute, so it should be accessed via the class name inside methods.
  2. Step 2: Check each option for correct increment

    class Student: count = 0 def __init__(self): Student.count += 1 uses Student.count += 1, correctly updating the class attribute. class Student: count = 0 def __init__(self): self.count += 1 tries to increment self.count, which creates an instance attribute instead. Options C and D have syntax or scope errors.
  3. Final Answer:

    class Student: count = 0 def __init__(self): Student.count += 1 -> Option B
  4. Quick Check:

    Update class attribute via ClassName.attribute [OK]
Hint: Use ClassName.attribute to update class attributes inside methods [OK]
Common Mistakes:
  • Using self.attribute to update class attribute
  • Defining count inside __init__ instead of class
  • Forgetting to use class name to access class attribute