AP® Computer Science A Class Creation Practice Questions
Work through AP Computer Science A practice questions on Class Creation, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Question 342
Question 343
Question 344
A music teacher wants to track how students log their practice time. You will design a class PracticeLog that records each practice session's duration (in minutes) and answers summary queries.
A PracticeLog object supports the following behavior:
- When constructed, a new log has zero minutes logged, zero sessions, and a longest-session value of 0.
- The method
logSession(int minutes)records a session of the given duration. Ifminutesis less than or equal to 0, the call is ignored (nothing is updated). Otherwise, the total minutes and session count increase, and the longest-session value is updated if this session is longer than any previously recorded session. - The method
averageSession()returns the average number of minutes per recorded session, as adouble. If no sessions have been logged, the method returns0.0. - The method
getTotal()returns the total minutes logged. - The method
getCount()returns the number of logged sessions. - The method
getLongest()returns the longest single session ever logged, or 0 if none.
The following table contains a sample code execution sequence and the corresponding results.
PracticeLog log = new PracticeLog();
log.logSession(30);
log.logSession(0);
log.logSession(45);
log.logSession(20);
log.getTotal();
log.getCount();
log.getLongest();
log.averageSession();
| Statements and Expressions | Value Returned (blank if no value) | Comment |
|---|---|---|
PracticeLog log = new PracticeLog(); |
Creates a new log with totalMinutes=0, sessionCount=0, longestSession=0. | |
log.logSession(30); |
Records a 30-minute session. total=30, count=1, longest=30. | |
log.logSession(0); |
Ignored (minutes \(\leq 0\)). No fields change. total=30, count=1, longest=30. | |
log.logSession(45); |
Records a 45-minute session. total=75, count=2, longest is updated to 45. | |
log.logSession(20); |
Records a 20-minute session. total=95, count=3. Longest stays at 45 (\(20 \lt 45\)). | |
log.getTotal(); |
95 |
Returns current totalMinutes. |
log.getCount(); |
3 |
Returns current sessionCount (the ignored 0-minute call did not count). |
log.getLongest(); |
45 |
Returns the maximum single-session duration recorded so far. |
log.averageSession(); |
31.666666666666668 |
Returns 95 / 3 as a double. Multiplying by 1.0 in the computation avoids integer division. |
Write the complete PracticeLog class, including all instance variables, the constructor, and all methods described above. Your implementation must conform to all specifications and must behave correctly for all valid inputs.
Main Solution
Sample Solution
public class PracticeLog
{
private int totalMinutes;
private int sessionCount;
private int longestSession;
public PracticeLog()
{
totalMinutes = 0;
sessionCount = 0;
longestSession = 0;
}
public void logSession(int minutes)
{
if (minutes > 0)
{
totalMinutes = totalMinutes + minutes;
sessionCount = sessionCount + 1;
if (minutes > longestSession)
{
longestSession = minutes;
}
}
}
public double averageSession()
{
if (sessionCount == 0)
{
return 0.0;
}
return totalMinutes * 1.0 / sessionCount;
}
public int getTotal() { return totalMinutes; }
public int getCount() { return sessionCount; }
public int getLongest() { return longestSession; }
}
Scoring (9 pts): +1 correct private instance variables of appropriate types; +1 constructor initializes all three fields to 0; +1 logSession guards against minutes <= 0; +1 logSession updates totalMinutes and sessionCount correctly; +1 logSession updates longestSession only when the new value exceeds the current maximum; +1 averageSession handles the zero-session case (returns 0.0); +1 averageSession avoids integer division without casting; +1 getTotal, getCount, getLongest return the correct fields; +1 class compiles using only allowed constructs.
Answer images
Question 345
Question 346
Question 347
Question 348
public class Stamp
{
private String code;
public Stamp(String c) { code = c; }
public String toString() { return "[" + code + "]"; }
}
Consider the following code segment.
Stamp s = new Stamp("A17");
System.out.println("mail " + s);
What is printed when the code segment executes?Main Solution
Answer
Answer: B. When an object is used in a String concatenation, its toString() method is invoked automatically. The overridden toString returns "[A17]", so the full output is "mail [A17]".
Answer: Correct: B
A
mail A17
B
mail [A17]
C
mail [Stamp]
D
The code will not compile.
Answer images
Question 349
Question 350
public class Basket
{
private int[] items;
public Basket(int[] arr) { items = arr; }
public void zeroFirst() { items[0] = 0; }
public int firstItem() { return items[0]; }
}
Consider the following code segment.
int[] data = {7, 8, 9};
Basket b = new Basket(data);
b.zeroFirst();
System.out.println(data[0] + " " + b.firstItem());
What is printed when the code segment executes?Main Solution
Answer
Answer: D. The constructor stores the reference that was passed in; items and data refer to the same array. Modifying items[0] from inside the Basket is visible through data as well. Both reads return 0.
Answer: Correct: D
A
7 7
B
7 0
C
0 7
D
0 0
Answer images
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

