39 exam-style questions on Object-oriented programming with answers and explanations. Six real samples below — the full set is free with an account.
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?
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?
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?
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?
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?
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?
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 freeThis 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.