Verityprep
Question Bank · Unit 3

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.

Questions 341–350 of 402
  1. 341
  2. 342
  3. 343
  4. 344
  5. 345
  6. 346
  7. 347
  8. 348
  9. 349
  10. 350

Question 341

Multiple choice Writing Classes
Consider the following class. public class Bag { private int count; public Bag(int c) { count = c; } public int getCount() { return count; } public void…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 342

Multiple choice Writing Classes
Consider the following class. public class Player { private static int totalGames = 0; private int wins; public Player() { wins = 0; } public void…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 343

Multiple choice Writing Classes
Consider the following class. public class Tag { private String text; public Tag(String t) { text = t; } public String getText() { return text; } } Consider the…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 344

Free response Writing Classes

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. If minutes is 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 a double. If no sessions have been logged, the method returns 0.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.

The answer and the full solution open in a window.

Question 345

Multiple choice Writing Classes
Consider the following class. public class Counter { private int value; public Counter(int start) { value = start; } public Counter() { this(10); } public void…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 346

Multiple choice Writing Classes
Consider the following class. public class Ticket { private static int nextId = 100; private int id; public Ticket() { id = nextId; nextId = nextId + 10; }…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 347

Multiple choice Writing Classes
Consider the following class. public class Point { private int x; private int y; public Point(int a, int b) { x = a; y = b; } public void shift(int d) { x = x +…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 348

Multiple choice Writing Classes
Consider the following class.
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?
Pick an answer, then check it. The solution opens in a window.

Question 349

Multiple choice Writing Classes
Consider the following method. public static void modify(int n) { n = n + 100; } The following code segment appears in…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 350

Multiple choice Writing Classes
Consider the following class.
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?
Pick an answer, then check it. The solution opens in a window.
Question 341 of 402
Page 35 of 41