Bird
0
0

You are designing a School class that contains many Student objects. Which approach best models this relationship in Python?

hard📝 Application Q8 of 15
Python - Object-Oriented Programming Foundations
You are designing a School class that contains many Student objects. Which approach best models this relationship in Python?
ADefine <code>Student</code> and <code>School</code> as unrelated classes
BMake <code>Student</code> inherit from <code>School</code>
CInclude a list attribute in <code>School</code> to store <code>Student</code> objects
DStore student names as strings inside <code>School</code> without using <code>Student</code> objects
Step-by-Step Solution
Solution:
  1. Step 1: Understand composition

    A School contains many Student objects, so it should hold them in a collection like a list.
  2. Step 2: Evaluate inheritance

    Inheritance (Make Student inherit from School) is incorrect because a student is not a type of school.
  3. Step 3: Consider unrelated classes

    Define Student and School as unrelated classes ignores the relationship, and Store student names as strings inside School without using Student objects loses the benefits of object modeling.
  4. Final Answer:

    Include a list attribute in School to store Student objects -> Option C
  5. Quick Check:

    Use composition to model 'has many' relationships [OK]
Quick Trick: Use lists to hold related objects inside classes [OK]
Common Mistakes:
  • Using inheritance for 'has many' relationships
  • Ignoring object composition
  • Storing data as plain strings instead of objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes