DigiBacc

Practice questions › IB Computer Science › Object-oriented programming › Fundamentals of OOP for multiple classes
IB Computer Science · Object-oriented programming

Fundamentals of OOP for multiple classes practice questions.

32 exam-style questions on Fundamentals of OOP for multiple classes — topic B3.2 of Object-oriented programming in the IB Computer Science syllabus — with answers and explanations. Samples below; the full set is free with an account.

At a glance

About this topic

Syllabus code
B3.2
Subject
IB Computer Science
Topic
B3 · Object-oriented programming
Questions
32 with worked answers
Question types
8 flashcards · 8 multiple-choice questions · 7 fill-in-the-gap questions · 4 matching tasks · 3 true/false questions · 1 diagram task · 1 structured question
Try it

Sample questions on Fundamentals of OOP for multiple classes

A program models Vehicle, Van, Wheel and Driver. Which pair is best modelled by inheritance?

  • Van and Vehicle, because a van is a kind of vehicle
  • Wheel and Vehicle, because a vehicle owns several wheels
  • Driver and Van, because a driver is assigned to a van
  • Wheel and Driver, because both take part in the same journey

Answer: Van and Vehicle, because a van is a kind of vehicle

Inheritance models an is-a relationship, so only the subclass/superclass pair qualifies. Owning or using another object is a has-a relationship and is modelled by aggregation or composition.

The superclass Shape defines area() and the subclasses Square and Circle each override it. A loop takes every object from a collection of Shape objects and executes output OBJ.area(). What happens?

  • Every object runs the version of area() defined in Shape, because the collection holds Shape objects.
  • Each object runs the version of area() defined in its own subclass.
  • The program cannot run, because a collection may hold objects of only one subclass.
  • Each object runs Shape's version first and its own subclass version afterwards.

Answer: Each object runs the version of area() defined in its own subclass.

This is polymorphism: the version that runs is chosen from the actual object, not from the type of the variable holding it. It is what lets one loop serve every shape without a decision for each kind.

A House object creates its Room objects when it is built, and those rooms are discarded when the house object is destroyed. Which relationship does this describe?

  • Inheritance, because a room is a kind of house
  • Aggregation, because the rooms exist independently of the house
  • Polymorphism, because rooms may behave in different ways
  • Composition, because the rooms cannot exist without the house

Answer: Composition, because the rooms cannot exist without the house

Composition is the strong form of has-a: the lifetime of the part is tied to the whole. If the rooms could be handed to another house and survive, it would be aggregation.

Which statement about an abstract class is correct?

  • It may be instantiated, but only once in a program.
  • It must contain a constructor and no other methods at all.
  • It cannot be instantiated and exists to be inherited from.
  • It may not declare attributes, because its subclasses declare them.

Answer: It cannot be instantiated and exists to be inherited from.

An abstract class collects what its subclasses have in common and may leave methods for them to supply, so it cannot be instantiated directly and is used only as a superclass. It may still hold attributes, a constructor and finished methods.

A subclass has to set up the attributes it inherits as well as the ones it adds itself. What does its constructor normally do first?

  • Call the constructor of its superclass
  • Copy every attribute of an existing object
  • Override each method it has inherited
  • Create one object of the superclass to hold beside it

Answer: Call the constructor of its superclass

The superclass constructor already contains the code that initialises the inherited attributes, so calling it avoids duplicating that code and guarantees the object is set up the same way whichever subclass is being built. Holding a separate superclass object beside the subclass object would be aggregation, not inheritance, and the subclass would then have two identities rather than one.

A library models Member and BannedMember. The same person is banned and reinstated many times while the program is running. Identify the fact in this scenario that rules out making BannedMember a subclass of Member.

  • Banned members and ordinary members would be kept in the same collection.
  • A banned member offers fewer methods than an ordinary member does.
  • An object cannot change the class it belongs to once it has been created.
  • Inheritance may be used only when the subclass adds at least one attribute of its own.

Answer: An object cannot change the class it belongs to once it has been created.

Inheritance fixes the class of an object for its whole life, so a status that switches back and forth has to be held as data inside one Member object, for example through a Ban object it refers to, rather than as a class the person is.

That was 6 of 32 Fundamentals of OOP for multiple classes questions.

Practise all 32 free
Exam tips

Command terms in this topic

Keep going

More topics in Object-oriented programming

All of Object-oriented programming →

32 Fundamentals of OOP for multiple classes questions — free with an account. Spaced repetition, streaks and full exam simulations included.

Practise them free

This platform is independently developed and is not endorsed by, affiliated with, or sponsored by the International Baccalaureate Organization. “International Baccalaureate” and “IB” are registered trademarks of the IBO.