Atlas登入
Black FridayHalf off Premium, through Sunday, Nov 29.See plans
AP Computer Science A · Unit test

Class creation practice test

Up to 15 AP Computer Science A questions on class creation, timed at the exam's pace and scored, new every time. Try 3 below first.

Take the class creation unit test →Try 3 questions

Class creation is 10%–18% of the AP Computer Science A exam's multiple choice. Learn it first in the class creation chapter.

3 free class creation practice questions

Real questions from the Atlas AP Computer Science A exam pool, with the answer and the working.

  1. Consider the following class.

    public class Cell
    {
      private int val;
    
      public Cell(int v)
      {
        val = v;
      }
    
      public void absorb(Cell other)
      {
        val += other.val;
        other.val = 0;
      }
    
      public int getVal()
      {
        return val;
      }
    }

    Consider the following code segment.

    Cell a = new Cell(4);
    Cell b = new Cell(7);
    Cell c = b;
    a.absorb(b);
    c.absorb(a);
    System.out.println(a.getVal() + " " + b.getVal() + " " + c.getVal());

    What is printed as a result of executing the code segment?

    1. A0 0 18
    2. B0 11 11
    3. C11 18 18
    4. D0 0 0
    Show the answer

    B.

    c refers to the same object as b. a.absorb(b) makes a 11 and b 0. c.absorb(a) is b absorbing a: b becomes 11 and a 0. b and c are one object, so both print 11.

    0 0 18: treats c = b as making a copy: it makes c refer to the same Cell object. 11 18 18: forgets other.val = 0: a method of Cell can change another Cell's private variable. 0 0 0: zeroes other.val before adding it.

  2. Consider the following class.

    public class Scorebook
    {
      private int total;
      private int count;
    
      public void add(int score)
      {
        total += score;
        count++;
      }
    
      /** Returns the mean of the scores added so far,
       *  or 0.0 if none have been added
       */
      public double average()
      {
        /* missing code */
      }
    }

    Which of the following can replace /* missing code */ so that average works as intended?

    1. Areturn total / count;
    2. Bif (count == 0) { return 0.0; } return total / count;
    3. Creturn (double) (total / count);
    4. Dif (count == 0) { return 0.0; } return (double) total / count;
    Show the answer

    D.

    The check for count == 0 avoids dividing by zero, and casting total to double before dividing keeps the fraction: scores 7 and 8 give 7.5.

    return total / count; does int division (7.0 instead of 7.5) and throws an ArithmeticException when count is 0. if (count == 0) { return 0.0; } return total / count; handles 0 but still truncates. (double) (total / count) casts after the int division has dropped the fraction, and also fails when count is 0.

  3. Consider the following class.

    public class Counter
    {
      private int count;
      private static int made = 0;
    
      public Counter()
      {
        count = 0;
        made++;
      }
    
      public static int getMade()
      {
        return made + count;
      }
    }

    Which of the following best describes the class?

    1. AIt does not compile, because the static method getMade uses the instance variable count.
    2. BIt compiles, and getMade() returns the number of Counter objects made, since count is always 0.
    3. CIt does not compile, because the private variable made cannot be changed in the constructor.
    4. DIt compiles, but made is reset to 0 each time a Counter is constructed.
    Show the answer

    A.

    A static method belongs to the class, not to any object, so it has no count to use: referring to an instance variable in a static method is a compile-time error.

    The value count happens to hold does not matter, because the code never compiles. Constructors can change private variables, static ones included. A static variable is initialized once, when the class is loaded, not each time an object is made.

Take the full unit test →AP Computer Science A practice exam

Questions people ask

What is in the AP Computer Science A Class creation practice test?
Up to 15 multiple-choice questions from this unit, plus a free-response question when the unit has one, timed at the real exam's pace and scored as a percent. The questions are new every time, drawn from a pool of checked questions.
How much of the AP Computer Science A exam is Class creation?
10% to 18% of the multiple-choice questions, by the College Board’s course and exam description.
Is it free?
The 3 questions on this page need no account. With a free Atlas account your first unit test is free; Atlas Premium has no limit.

Other AP Computer Science A units