Classes, __init__ & self — Concept Check
Questions on Classes, __init__ & self. Classes bundle data and behaviour: __init__ initializes each instance, self is the explicit instance reference, and attributes live per-instance or shared on the class.
Questions in this quiz
7 questions on Classes, __init__ & self — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer creates a class attribute as a list to track enrolled students. What issue might arise?
- AThe list will be immutable, preventing modifications.
- BThe list will automatically reset with each new instance.
- CEach instance will have its own separate list, leading to inconsistencies.
- DThe list will be shared across all instances, causing data leakage.
2.In the code snippet, `acc.deposit(500)` is called. What does this imply?
- AThe `deposit` method is a class method and doesn't require an instance.
- BThe `deposit` method is called with `acc` as the self parameter.
- CThe `deposit` method is static and doesn't need self.
- DThe `deposit` method is called without any parameters.
3.A developer uses `self.students.append(who)` in `enroll`. What's the potential problem?
- AIt mutates a shared class attribute, causing data to leak across instances.
- BIt throws an error because `self.students` is not initialized.
- CIt appends to a non-existent list, causing a crash.
- DIt creates a new instance attribute, isolating data per instance.
4.Why might `BankAccount.deposit(acc, 100)` work in Python?
- APython automatically converts it to `acc.deposit(100)`.
- B`deposit` is a static method, so `self` isn't needed.
- C`deposit` is a class method that doesn't require an instance.
- DThe method explicitly receives `acc` as the `self` parameter.
5.What happens if an instance attribute is missing during access?
- AThe class attribute with the same name is accessed.
- BAn AttributeError is raised immediately.
- CA new instance attribute is created with a default value.
- DThe program crashes due to undefined behavior.
6.A developer mistakenly believes `__init__` creates the object. What's the truth?
- A`__init__` is a class method that sets up class attributes.
- B`__init__` is responsible for creating the object.
- C`__init__` initializes an already created object.
- D`__init__` is a static method that initializes data.
7.Which of the following is true about class attributes in Python?
- AThey are automatically converted to instance attributes.
- BThey are shared across all instances of the class.
- CThey are immutable by default.
- DThey are unique to each instance of the class.