Clastify logo
Subjects
Features
Review
HOT
Tutoring

B3.1 Fundamentals of OOP for a single class

Practice exam-style IB Computer Science questions for Fundamentals of OOP for a single class, 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
SL • Paper 2
Medium
SL • Paper 2
Medium

Fernwick Nursery records moisture readings for six positions in each growing bed. A reading is an integer from 0 to 10 inclusive, and a shared dry limit determines which readings are classified as dry.

The following textual UML class diagram specifies the planned class. The annotation {static} represents an underlined UML member.

GrowBed  
-------------------------------------------------------  
- label: String  
- moisture: int[6]  
- dryLimit: int {static}  
-------------------------------------------------------  
+ GrowBed(label: String, initial: int)  
+ getDryLimit(): int {static}  
+ setDryLimit(newLimit: int): void {static}  
+ countDry(): int  

The following executable prototype contains the attributes and the implemented getter. The constructor and the other methods in the diagram have not yet been added.

 1  public class GrowBed {  
 2      private String label;  
 3      private int[] moisture;  
 4      private static int dryLimit = 3;  
 5      public static int getDryLimit() {  
 6          return dryLimit;  
 7      }  
 8      public static void main(String[] args) {  
 9          System.out.println(GrowBed.getDryLimit());  
10      }  
11  }  
A

State the visibility of moisture, the data type of label, the return type of countDry, and the significance of the underlining represented by {static} on dryLimit.

[4]
B

A separate, one-off program only needs to classify six supplied readings and then terminate. Explain one disadvantage of introducing a class for that program.

[2]
C

State two consequences of changing moisture from private to public.

[2]
D

Construct the public static method setDryLimit. It receives the integer parameter newLimit and updates the shared dry limit. You may assume that the parameter is between 0 and 10 inclusive.

[2]
E

Construct the public constructor specified by the diagram. It receives label and initial, stores the label, and creates a separate six-element moisture array for the new object. Every element must contain initial if it is between 0 and 10 inclusive; otherwise every element must contain 0.

[5]
F

Construct the public instance method countDry. It receives no parameters and returns the number of this object's moisture readings that are strictly less than the current shared dry limit. The method must not change the readings.

[5]
Question 2
SL • Paper 2
Medium
SL • Paper 2
Medium

Orrin Community Kitchens uses refill stations that each hold at most eight whole units of cleaning liquid. Each station starts empty, and the system also maintains the total number of units dispensed by all stations since the program started.

The following textual UML class diagram specifies the planned class. The annotation {static} represents an underlined UML member.

RefillStation  
---------------------------------------------------------  
- code: String  
- available: int  
- dispensed: int {static}  
---------------------------------------------------------  
+ RefillStation(code: String)  
+ refill(amount: int): boolean  
+ dispenseRequests(requests: int[]): int  
+ getDispensed(): int {static}  

The following executable prototype contains the attributes and the implemented total getter. The constructor and the remaining methods have not yet been added.

 1  public class RefillStation {  
 2      private String code;  
 3      private int available;  
 4      private static int dispensed = 0;  
 5      public static int getDispensed() {  
 6          return dispensed;  
 7      }  
 8      public static void main(String[] args) {  
 9          System.out.println(RefillStation.getDispensed());  
10      }  
11  }  
A

Describe four details of the public interface specified by the diagram: the constructor's argument, the result returned by refill, the argument received by dispenseRequests, and the parameters required by getDispensed.

[4]
B

Explain one advantage of modelling each physical refill station as an object that combines its state with the operations on that state.

[2]
C

A developer proposes changing getDispensed so that it returns the current shared total and then resets that total to 0. State two consequences for code that expects the method to report the total since the program started.

[2]
D

Construct the public constructor specified by the diagram. It receives the station's code, stores it, and explicitly initialises that station's available quantity to 0. It must not reset the shared total.

[2]
E

Construct the public instance method refill. It receives the integer parameter amount and returns a boolean. A refill is accepted only if the amount is positive and adding it would not increase the station's available quantity above eight. An accepted refill updates the available quantity and returns true. A rejected refill leaves all attributes unchanged and returns false. Refilling must not change the shared dispensed total.

[5]
F

Construct the public instance method dispenseRequests. It receives an integer array named requests and returns the total number of units supplied during this call. Process requests in their given order. Supply a request in full only if it is positive and does not exceed this station's currently available quantity; otherwise ignore it. For each supplied request, reduce the available quantity. Also increase the shared dispensed total by the total supplied during this call. An empty array must return 0 without changing the totals.

[5]
Question 3
SL • Paper 2
Medium
SL • Paper 2
Medium

Veyra Field Services uses portable battery banks whose charge is measured in whole units. Each bank has its own charge, while a shared reserve specifies the charge that must be retained when supplying equipment.

The following program shows the implemented members of BatteryBank. New banks receive a valid initial charge from 0 to 9 inclusive.

 1  public class BatteryBank {  
 2      private int charge;  
 3      private static int reserve = 2;  
 4      public BatteryBank(int initial) {  
 5          charge = initial;  
 6      }  
 7      public int getCharge() {  
 8          return charge;  
 9      }  
10      public static void setReserve(int value) {  
11          if (value >= 0 && value <= 9) {  
12              reserve = value;  
13          }  
14      }  
15      public static void main(String[] args) {  
16          BatteryBank first = new BatteryBank(7);  
17          BatteryBank second = new BatteryBank(4);  
18          BatteryBank.setReserve(3);  
19          System.out.println(first.getCharge() + reserve);  
20      }  
21  }  
A

Describe what happens when each of lines 16 to 19 is executed. Refer to object creation, the shared variable, and the instance method call in your answer.

[4]
B

Explain why charge should be an instance variable rather than a static variable in this design.

[2]
C

State two consequences of changing getCharge from public to private while leaving charge private.

[2]
D

Construct the public instance method isLow, with no parameters and a boolean return type. It returns true exactly when this bank's charge is strictly less than the current shared reserve.

[2]
E

Construct the public instance method discharge, which receives the integer parameter amount and returns the number of charge units actually supplied. If amount is not positive, or the charge is already at or below the reserve, it returns 0 without changing the charge. Otherwise it supplies the requested amount, or all charge above the reserve if that is smaller, and reduces this bank's charge accordingly.

[5]
F

Construct the public static method serve, which receives an array of BatteryBank objects named banks and an integer array named requests, and returns an integer. The arrays have equal lengths, all requests are positive, and every bank reference is valid. For each index, call that bank's discharge method exactly once with the corresponding request. Return the number of requests that were supplied in full. You may assume that discharge works as specified.

[5]

B3.2 Fundamentals of OOP for multiple classes