40 exam-style questions on Fundamentals of OOP for a single class — topic B3.1 of Object-oriented programming in the IB Computer Science syllabus — with answers and explanations. Samples below; the full set is free with an account.
Inside a method of the class Timer, a variable COUNT is declared and used only within that method. What is true of COUNT?
Answer: It is a local variable and stops existing when the method finishes running.
A local variable lives for one call only, while an attribute belongs to the object and lasts as long as the object does. Keeping a running total in a local variable is why counters mysteriously reset.
A program declares the variable G, which is to hold a Gauge object, but never runs the line that creates the object. It then calls G.getReading(). Identify what is wrong.
Answer: G refers to no object, so there are no attribute values to return
Defining a class describes what an object will look like; it does not create one. Until the class is instantiated, G names nothing, so there is no set of attribute values for getReading() to read.
A class is rewritten so that every attribute is private and each one is reached through an accessor and a mutator. Identify the statement that names both what this gains and what it costs.
Answer: Every change can be checked before it is stored, but more methods must be written and called
Encapsulation is a trade: the class becomes the only route to its own data and can therefore defend its invariants, and the price is the extra methods and the extra calls. It changes neither the memory an object occupies nor the need for a constructor.
The mutator setPrice(P) is the only method that assigns the private attribute PRICE, and it rejects any value below zero. An object is nevertheless found holding a negative price. Identify the conclusion that follows.
Answer: The constructor stored the value passed to it without making the same check
The constructor is the second route to a private attribute and is easy to forget: it writes the starting values directly, so a check that lives only in the mutator is bypassed once, at creation. Objects never share attribute values, and an accessor changes nothing.
A program creates 100 objects from one class instead of 10. The class defines two attributes and six methods. Determine what grows tenfold and what does not.
Answer: The stored attribute values grow tenfold; the class still defines six methods
Each object needs its own set of attribute values, so ten times as many objects hold ten times as many values. The methods belong to the class and are written and stored once, however many objects are created from it.
The subprogram that runs automatically when an object is created and gives its attributes their starting values is called the ___. (one word)
Answer: constructor
The constructor runs once per object, immediately after memory is reserved, which is why an object never starts with undefined attributes. A subprogram that changes a value later is a mutator, not a constructor.
That was 6 of 40 Fundamentals of OOP for a single class questions.
Practise all 40 freeDistinguishAO2 · 2–4 marks
Make the difference clear for BOTH terms — a definition of one of them is only half an answer.
OutlineAO1 · 1–2 marks
A brief account or summary: the main points in two or three sentences, no evaluation.
StateAO1 · 1–2 marks
One word or one short sentence. No explanation, no working: the mark is for the fact itself.
40 Fundamentals of OOP for a single class 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.