Clastify logo
Subjects
Features
Review
HOT
Tutoring

B3.2 Fundamentals of OOP for multiple classes

Practice exam-style IB Computer Science questions for Fundamentals of OOP for multiple classes, aligned with the syllabus and grouped by topic.

Paper
Difficulty
Status
Level
Java

This content can't be printed from the browser. Use Export PDF with Pro for an offline copy.

Question 1
HL • Paper 2
Hard
HL • Paper 2
Hard

The Fenmere community centre groups portable power banks into charging carts. Each power bank is registered independently and can be used without a cart or moved between carts. Assume each power bank is constructed with a non-negative charge, measured in whole units.

The following program shows two classes used by the system. A cart holds at most three power banks.

 1  class PowerBank {  
 2      private int charge;  
 3      public PowerBank(int charge) {  
 4          this.charge = charge;  
 5      }  
 6      public int getCharge() {  
 7          return charge;  
 8      }  
 9  }  
10  class ChargingCart {  
11      private PowerBank[] banks = new PowerBank[3];  
12      private int count;  
13      public ChargingCart(PowerBank first) {  
14          banks[0] = first;  
15          count = 1;  
16      }  
17      public int size() {  
18          return count;  
19      }  
20  }  
21  public class Main {  
22      public static void main(String[] args) {  
23          PowerBank bank = new PowerBank(7);  
24          ChargingCart cart = new ChargingCart(bank);  
25          System.out.println(cart.size());  
26      }  
27  }  
A
I.

Describe the operations carried out by line 23.

[3]
II.

Outline two consequences of declaring the charge field private.

[2]
B

Identify the relationship between ChargingCart and PowerBank, and justify your answer using their lifetimes in this system.

[2]
C

Construct the public method supply in PowerBank, with return type int and one int parameter requested. The parameter is non-negative. The method supplies the requested units, or all remaining charge if there is insufficient charge. It reduces charge by the units supplied and returns that number.

[4]
D

Construct the public method addBank in ChargingCart, with return type boolean and one PowerBank parameter bank. If the cart is full, it returns false without changing the cart. Otherwise, it stores a reference to bank in the next unused array element, updates count and returns true. Assume bank is not null and is not already in the cart.

[4]
E

A new ChargingCart method receives a non-negative request for units. It uses the stored power banks in array order to supply as much of the request as possible and returns the total units supplied. Without writing code, describe its steps. Use the supply method from part (c); the cart must not directly change a power bank's charge.

[5]
Question 2
HL • Paper 2
Hard
HL • Paper 2
Hard

The Reedwick rehearsal studio calculates booking fees in whole credits. A standard booking costs two credits per minute, while an assisted booking adds a support charge.

The program shows the current classes.

 1  class Booking {  
 2      private String reference;  
 3      protected int minutes;  
 4      public Booking(String reference, int minutes) {  
 5          this.reference = reference;  
 6          this.minutes = minutes;  
 7      }  
 8      public int fee() {  
 9          return minutes * 2;  
10      }  
11  }  
12  class AssistedBooking extends Booking {  
13      private int supportCharge;  
14      public AssistedBooking(String reference, int minutes, int supportCharge) {  
15          super(reference, minutes);  
16          this.supportCharge = supportCharge;  
17      }  
18      public int fee() {  
19          return super.fee() + supportCharge;  
20      }  
21  }  
22  public class Main {  
23      public static void main(String[] args) {  
24          Booking booking = new AssistedBooking("R8", 10, 4);  
25          System.out.println(booking.fee());  
26      }  
27  }  
A
I.

Describe how the object constructed on line 24 is initialized, including the role of line 15.

[3]
II.

Distinguish between the access that AssistedBooking methods have to the inherited state represented by reference and minutes.

[2]
B

Identify the method that is overridden and outline why the call on line 25 demonstrates dynamic polymorphism.

[2]
C

The studio changes the assisted-booking fee rule. Construct a replacement public fee method in AssistedBooking, returning int and receiving no parameters. It must obtain the standard fee by calling the parent method, subtract six credits if minutes is at least 30, then add supportCharge and return the result.

[4]
D

Construct a static method totalFees in Main, with return type int and one parameter bookings of type java.util.ArrayList. The list may contain Booking objects and objects of any Booking subclass. Return the sum of their fees, or zero for an empty list, using polymorphic method calls without checking an object's class.

[4]
E

The studio introduces RemoteBooking. Its fee is the standard booking fee plus a connection charge stored separately in each remote booking. Without writing code, describe how to add this subclass and use it with totalFees without changing that method.

[5]
Question 3
HL • Paper 2
Hard
HL • Paper 2
Hard

The Mossbridge sign workshop uses cutting-plan objects to calculate the material units needed for an order. Client code obtains a plan through makePlan and uses the common operation units without needing to know the plan's concrete class.

The current program supports strip plans.

 1  abstract class CutPlan {  
 2      public abstract int units(int count);  
 3  }  
 4  class StripPlan extends CutPlan {  
 5      public int units(int count) {  
 6          return count * 2;  
 7      }  
 8  }  
 9  public class Main {  
10      public static CutPlan makePlan(String kind) {  
11          if (kind.equals("strip")) {  
12              return new StripPlan();  
13          }  
14          return null;  
15      }  
16      public static void main(String[] args) {  
17          CutPlan plan = makePlan("strip");  
18          System.out.println(plan.units(3));  
19      }  
20  }  
A
I.

Describe the operations carried out by line 17, including what occurs inside makePlan.

[3]
II.

Outline two consequences of defining CutPlan as an abstract class with the abstract operation units.

[2]
B

Identify the design pattern represented by makePlan and outline one benefit of this pattern for the workshop's client code.

[2]
C

A new concrete subclass GridPlan will extend CutPlan. Construct its public units method, returning int and receiving one int parameter count. Assume count is non-negative. Each sign needs three material units; an additional two units are needed for setup only when count is greater than zero.

[4]
D

Construct a replacement static makePlan method in Main, with return type CutPlan and one String parameter kind. It must return a new StripPlan for "strip", a new GridPlan for "grid", and null for any other value. Assume kind is not null and GridPlan has a public constructor receiving no parameters.

[4]
E

The workshop adds an Order subject that stores its current cutting plan. A Preview object must redraw the layout and a StockTracker object must recalculate material requirements whenever that plan changes. Using the observer pattern, describe the steps from registering these objects to responding to a plan change, without writing code.

[5]

B3.1 Fundamentals of OOP for a single class