DigiBacc

Practice questionsIB Computer ScienceObject-oriented programming
IB Computer Science

Object-oriented programming practice questions.

39 exam-style questions on Object-oriented programming with answers and explanations. Six real samples below — the full set is free with an account.

Syllabus

Topics covered

Try it

Sample questions

A program defines the class Book and then stores the details of 40 different books. Which statement describes what the class and the books are?

  • The class is a template defining the attributes and methods; each book is an object created from it.
  • The class is a container that already holds the details of all 40 books before any object is created.
  • Each book is its own class, and the object is the single record that stores all 40 of them together.
  • The class is built in memory at the moment a method is called, and the 40 books then share its values.

Answer: The class is a template defining the attributes and methods; each book is an object created from it.

A class is written once and describes state and behaviour; every object is a separate instance holding its own attribute values. The commonest error is treating the class as if it already stored data — nothing is stored until an object is instantiated.

A class Student declares its attributes as public and other parts of the program change them directly. Which change applies encapsulation to this class?

  • Move the attributes into a second class so that the original class keeps only its methods.
  • Declare the attributes private and add public methods that read and change them.
  • Rename the attributes so that no other part of the program can guess what they are called.
  • Keep the attributes public but add a comment stating that they must not be changed directly.

Answer: Declare the attributes private and add public methods that read and change them.

Encapsulation means the attributes are private and every read or change goes through a method, so the class can validate a value before storing it. Hiding a name or writing a comment is a convention, not a mechanism.

A program executes P = new Player("Sam", 0). What happens?

  • The class Player is loaded into memory so that its methods can be called later.
  • The values are stored in the class Player itself and shared by every Player object.
  • A new object is created and its constructor stores the two values in its attributes.
  • The method Player is called on an object that already exists and returns its state.

Answer: A new object is created and its constructor stores the two values in its attributes.

The instantiation creates the object and immediately runs the constructor, which puts the arguments into that object's own attributes. Nothing is stored in the class, so a second Player is completely unaffected.

A class Sensor has the private attribute READING. Which of these methods is a mutator?

  • getReading(), which returns the current value of READING
  • printReading(), which outputs the current value of READING on the screen
  • hasReading(), which returns true when READING has been set
  • setReading(VALUE), which stores VALUE in READING after checking it

Answer: setReading(VALUE), which stores VALUE in READING after checking it

A mutator changes the state of the object, so it takes a value and writes it into the attribute; the other three only read it. Validation belongs in the mutator because it is the single way in.

A UML class diagram shows the line -balance : integer in the middle compartment of the class Account. What does this line state?

  • Account has a private attribute called balance holding an integer.
  • Account has a public attribute called balance holding an integer.
  • Account has a private method called balance that returns an integer.
  • Account subtracts an integer called balance from its other attributes.

Answer: Account has a private attribute called balance holding an integer.

The middle compartment holds attributes and the bottom one methods, while a minus sign marks private visibility and a plus sign marks public. Reading the minus as an arithmetic operator is the usual mistake.

Two objects L1 and L2 are instantiated from the class Lamp, which stores the private attribute STATE. L1's mutator is then called to change STATE. What is the effect on L2?

  • L2's STATE changes as well, because both objects were created from the same class.
  • L2's STATE is unchanged, because each object holds its own copy of the attribute.
  • L2's STATE becomes undefined until its own mutator is called for the first time.
  • L2's STATE changes only if L2 was instantiated after L1 in the same program.

Answer: L2's STATE is unchanged, because each object holds its own copy of the attribute.

Attributes belong to the object, so a mutator writes only into the object it was called on. Believing that objects of one class share values confuses instance attributes with the class definition.

39 Object-oriented programming questions — free with an account. Spaced repetition, streaks and full exam simulations included.

Practise them free
All subjects Pricing For teachers

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.