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)
- ['Isha'] ['Isha'] ['Isha', 'Kabir']
- ['Isha'] ['Isha'] ['Kabir']
- ['Isha', 'Kabir'] ['Isha', 'Kabir'] ['Isha', 'Kabir']
- [] ['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
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
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