DigiBacc

Practice questions › IB Computer Science › Object-oriented programming › Fundamentals of OOP for a single class
IB Computer Science · Object-oriented programming

Fundamentals of OOP for a single class practice questions.

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.

At a glance

About this topic

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

Sample questions on Fundamentals of OOP for a single class

Inside a method of the class Timer, a variable COUNT is declared and used only within that method. What is true of COUNT?

  • It is an attribute of Timer and keeps its value between calls to the method.
  • It is shared by every Timer object as soon as any one of them runs the method.
  • It is a local variable and stops existing when the method finishes running.
  • It is a parameter and must be supplied by the caller whenever the method runs.

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.

  • The class Gauge cannot have been defined, so the method does not exist
  • getReading() is an accessor, so it may not be called from outside the class
  • G refers to no object, so there are no attribute values to return
  • Gauge was given no constructor, so its attributes were never declared

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.

  • It removes the need for a constructor, but each object then starts empty
  • It makes each object occupy less memory, but every access needs an extra call
  • It hides the internal design from the rest of the program, but the class cannot then be reused
  • Every change can be checked before it is stored, but more methods must be written and called

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.

  • The constructor stored the value passed to it without making the same check
  • Two objects created from the class shared one copy of PRICE, so another changed it
  • The accessor getPrice() returned the value before the mutator had run
  • The mutator must have been called twice with the same negative value

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.

  • Both the stored attribute values and the number of method definitions grow tenfold
  • The stored attribute values grow tenfold; the class still defines six methods
  • The number of method definitions grows tenfold; the stored values do not
  • Neither grows, because the class is written once and describes every object

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 free
Exam tips

Command terms in this topic

Keep going

More topics in Object-oriented programming

All of Object-oriented programming →

40 Fundamentals of OOP for a single class 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.