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 321–330 of 402
  1. 321
  2. 322
  3. 323
  4. 324
  5. 325
  6. 326
  7. 327
  8. 328
  9. 329
  10. 330

Question 321

Free response Writing Classes

A fitness app records daily exercise sessions for one user. You will implement a class FitnessTracker that stores a rolling log of the user's most recent workouts and reports summary statistics. The class will be used like this.

FitnessTracker ft = new FitnessTracker(7);    // track the last 7 sessions
ft.logSession(30, 4.2);                       // 30 minutes, 4.2 kilometers
ft.logSession(45, 6.0);
ft.logSession(20, 2.5);
double avgPace = ft.averagePace();            // minutes per kilometer
int active     = ft.activeDays(25);           // sessions of at least 25 minutes

A FitnessTracker must meet the following specifications.

Method / Constructor HeaderDescription
public FitnessTracker(int capacity) Creates a tracker that retains at most capacity of the most recently logged sessions. The tracker begins with no logged sessions. You may assume capacity > 0.
public void logSession(int minutes, double kilometers) Records a single workout session. If the tracker already holds capacity sessions, the oldest logged session is discarded before the new session is added. minutes is always > 0 and kilometers is always > 0.0.
public int sessionCount() Returns the number of sessions currently stored (always between 0 and capacity).
public double averagePace() Returns the average pace (minutes per kilometer) across all currently stored sessions, computed as (total minutes) / (total kilometers) using double arithmetic. If no sessions are stored, returns 0.0.
public int activeDays(int minMinutes) Returns the number of currently stored sessions whose duration is greater than or equal to minMinutes.

Write the complete FitnessTracker class. Your implementation must meet all specifications and conform to the example. You may define additional private fields, constructors, and helper methods as needed. You may use any ArrayList- or array-based approach.

The answer and the full solution open in a window.

Question 322

Multiple choice Writing Classes
Consider the following class.
public class Point
{
    private int x;
    private int y;

    public Point()
    {
        this(0, 0);
    }

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int sum()
    {
        return x + y;
    }
}
Consider the following code segment.
Point p = new Point();
Point q = new Point(3, 5);
System.out.println(p.sum() + q.sum());
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 323

Multiple choice Writing Classes
Consider the following class.
public class Counter
{
    private static int count = 0;
    private int id;

    public Counter()
    {
        count++;
        id = count;
    }

    public int getId()
    {
        return id;
    }

    public static int getCount()
    {
        return count;
    }
}
Consider the following code segment.
Counter a = new Counter();
Counter b = new Counter();
Counter c = new Counter();
System.out.println(a.getId() + b.getId() + c.getId() + Counter.getCount());
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 324

Multiple choice Writing Classes
Which of the following is true about a field declared private in a Java class, based on the rules of the AP Java subset?
Pick an answer, then check it. The solution opens in a window.

Question 325

Multiple choice Writing Classes

Consider the following class.

public class Box
{
    public int value;

    public Box(int v)
    {
        value = v;
    }
}

Consider the following code segment.

Box a = new Box(10);
Box b = a;
Box c = new Box(10);
b.value = 20;
System.out.println(a.value + " " + b.value + " " + c.value);

What is printed when the code segment executes?

Pick an answer, then check it. The solution opens in a window.

Question 326

Multiple choice Writing Classes
Consider the following class.
public class Calc
{
    public int op(int a, int b)
    {
        return a + b;
    }

    public int op(int a)
    {
        return a * 2;
    }

    public double op(double a, double b)
    {
        return a - b;
    }
}
Consider the following code segment.
Calc c = new Calc();
double r = c.op(3, 5) + c.op(4) + c.op(3.0, 1.0);
What value is stored in r after the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 327

Multiple choice Writing Classes
Consider the following class.
public class Widget
{
    private String name;
    private int count;

    public Widget(String n, int c)
    {
        name = n;
        count = c;
    }

    public String toString()
    {
        return name + ":" + count;
    }
}
Consider the following code segment.
Widget w = new Widget("gear", 7);
System.out.println(w + " has " + w.toString().length() + " chars");
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 328

Multiple choice Writing Classes
Consider the following class.
public class Data
{
    private int n;
    private double d;
    private String s;
    private boolean b;

    public String snapshot()
    {
        return n + "," + d + "," + s + "," + b;
    }
}
Consider the following code segment.
Data data = new Data();
System.out.println(data.snapshot());
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 329

Multiple choice Writing Classes
Consider the following class.
public class Util
{
    private boolean isEven(int n)
    {
        return n % 2 == 0;
    }

    public int countEven(int[] arr)
    {
        int c = 0;
        for (int i = 0; i < arr.length; i++)
        {
            if (isEven(arr[i]))
            {
                c++;
            }
        }
        return c;
    }
}
Which of the following is true of the method isEven in the class above?
Pick an answer, then check it. The solution opens in a window.

Question 330

Multiple choice Writing Classes
Consider the following class.
public class Pair
{
    private int a;
    private int b;

    public Pair(int a, int b)
    {
        this.a = a;
        this.b = b;
    }

    public Pair swapped()
    {
        return new Pair(b, a);
    }

    public String show()
    {
        return a + "," + b;
    }
}
Consider the following code segment.
Pair p = new Pair(2, 5);
Pair q = p.swapped();
System.out.println(p.show() + " | " + q.show());
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.
Question 321 of 402
Page 33 of 41