Python · Question 28 · Hard

What will be the output of the following Python code?

class Team:
    members = []

a = Team()
b = Team()
a.members.append("Isha")
b.members = ["Kabir"]
print(Team.members, a.members, b.members)
  1. ['Isha'] ['Isha'] ['Isha', 'Kabir']
  2. ['Isha'] ['Isha'] ['Kabir']
  3. ['Isha', 'Kabir'] ['Isha', 'Kabir'] ['Isha', 'Kabir']
  4. [] ['Isha'] ['Kabir']
Answer and explanation

Answer: B. ['Isha'] ['Isha'] ['Kabir']

Explanation: a.members.append changes the shared class-level list, so Team.members and a.members both show ['Isha']. b.members = [...] creates a new instance attribute on b only, which hides the class list for b.

Previous: What will be the output of the following Python code? · Next: What will be the output of the following Python code?

Practise python

Try a fresh set of python questions with an explanation after every answer, free and without signing up: Python practice. Or sit a full placement mock test.

More python questions

More technical & programming practice

Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks · Software Engineering

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge