Skip to main content

AP Computer Science A Unit 3: Class Creation

Ready When You Are

Select a course and unit, then start practicing.

This page is set up for AP Computer Science A Class Creation. Click Practice, answer it, and read the explanation before moving on.

Designing classes, constructors, instance variables, methods, and encapsulation (object-oriented basics as required by the course). On the AP Computer Science A exam, items from Class Creation often ask you to apply course concepts to a scenario you have not memorized verbatim rather than recall isolated terms.

College Board topic statements for this unit include Defining classes with fields and methods; Constructors, overloading, and object state; Access control, instance vs class (static) members; and Designing methods to implement behavior and abstraction.

On the AP Computer Science A exam, Class Creation is weighted at roughly 10%–18% of the multiple-choice section. That is enough to matter on score day even if your class spent only a few weeks here—especially when questions combine this unit with later material.

If two answers feel half-right, Class Creation questions usually reward the option tied to scale, evidence, or mechanism—not the more dramatic storyline.

Try explaining this unit's core idea to someone else in under 60 seconds. If the explanation drifts, you found a gap worth fixing before test day.

Pair Class Creation MCQs with one sketch or outline per session. AP Computer Science A rewards multiple representations; translating a stem into a diagram or timeline catches gaps MCQs alone can hide.

Click Practice above for fresh MCQs tied to Class Creation. Answer, read the explanation, and log one sentence about why the correct choice works—that habit compounds faster than grinding endless worksheets.

Sample questions

A few exam-style multiple-choice questions from this unit, with answers and explanations.

  1. Question 1

    A class stores the highest score recorded so far. Assume every score passed to recordScore is nonnegative. The class is declared as follows:

    public class RecordBook
    {
        private int bestScore;
    
        public RecordBook(int initialScore)
        {
            bestScore = initialScore;
        }
    
        // Choose the method body for recordScore.
    }

    The method recordScore must return true exactly when its argument is greater than the score stored before the call. In that case, it must update bestScore to the argument. Otherwise, it must return false and leave bestScore unchanged. Which method body satisfies this contract?

    • A
      return score > bestScore;
    • B
      if (score >= bestScore)
      {
          bestScore = score;
          return true;
      }
      return false;
    • C
      bestScore = score;
      return score > bestScore;
    • D
      if (score > bestScore)
      {
          bestScore = score;
          return true;
      }
      return false;
      Correct answer

    Explanation

    D compares the new score with the stored score before changing anything. If the new score is greater, it updates the field and returns true; otherwise, it returns false without changing the field.

    A returns the correct comparison result but never updates bestScore when a higher score is recorded.

    B also returns true when the new score ties the current best, even though the contract requires the score to be strictly greater.

    C overwrites bestScore before comparing. The comparison is then never true, and the previous best is lost.

  2. Question 2

    Consider the following class and code segment.

    public class TokenMachine {
        private static int tokens = 0;
        private int id;
    
        public TokenMachine(int start) {
            id = start;
            tokens += start;
        }
    
        public void addToken() {
            tokens++;
        }
    
        public void setId(int newId) {
            id = newId;
        }
    
        public String toString() {
            return id + ":" + tokens;
        }
    }
    TokenMachine a = new TokenMachine(2);
    TokenMachine b = new TokenMachine(5);
    a.addToken();
    b.setId(3);
    System.out.print(a);
    System.out.print(" ");
    System.out.print(b);

    What is printed by the code segment?

    • A

      2:8 5:8

    • B

      3:8 5:8

    • C

      2:7 3:8

    • D

      2:8 3:8

      Correct answer

    Explanation

    The correct answer is D.

    After a is constructed, tokens becomes 22. After b is constructed, tokens becomes 77 because tokens is static and shared by both objects. Then a.addToken() increments the same shared tokens value to 88. The call b.setId(3) changes only b's instance variable id, not tokens.

    So a.toString() returns 2:8, and b.toString() returns 3:8.

    C is incorrect because it treats the shared tokens value as if a.addToken() did not affect both objects. A is incorrect because b.setId(3) changes b.id, so b does not keep 5. B is incorrect because a.id was never changed from 2, so a does not print 3.

  3. Question 3

    Consider the following class and client code:

    public class Beacon {
        private int signal;
    
        public Beacon(int initialSignal) {
            signal = initialSignal;
        }
    
        private int limit(int value) {
            if (value < 0) {
                return 0;
            }
            if (value > 5) {
                return 5;
            }
            return value;
        }
    
        public void boost(int amount) {
            signal = limit(signal + amount);
        }
    
        public int getSignal() {
            return signal;
        }
    }
    Beacon b = new Beacon(3);
    b.boost(4);
    b.boost(-8);
    System.out.println(b.getSignal());

    What is printed?

    • A

      3

    • B

      0

      Correct answer
    • C

      5

    • D

      -3

    Explanation

    The constructor sets signal to 3. The first call to boost adds 4, producing 7, and limit returns 5. The second call adds -8, producing -3, and limit returns 0. Thus, getSignal returns 0.

    B is correct: the private helper limits the updated signal after each call to boost.

    D is incorrect because it accounts for the subtraction but not the helper's lower limit of 0.

    C is incorrect because it reports the value after the first call and ignores the second call.

    A is incorrect because boost adds its argument to the current signal; it does not ignore the arguments or restore the initial value.

  4. Question 4

    Consider the class below.

    public class MovieTicket {
        private int basePrice;
    
        public MovieTicket(int price) {
            basePrice = price;
        }
    
        public int calculatePrice(int age) {
            int finalPrice = basePrice;
            if (age < 13) {
                finalPrice = finalPrice - 3;
            }
            if (age >= 65) {
                finalPrice = finalPrice - 2;
            }
            if (finalPrice < 0) {
                finalPrice = 0;
            }
            return finalPrice;
        }
    
        public int getBasePrice() {
            return basePrice;
        }
    }

    What is printed by the following code segment?

    MovieTicket t = new MovieTicket(12);
    System.out.print(t.calculatePrice(10) + " ");
    System.out.print(t.calculatePrice(70) + " ");
    System.out.print(t.getBasePrice());
    • A

      9 10 10

    • B

      12 12 12

    • C

      9 12 10

    • D

      9 10 12

      Correct answer

    Explanation

    The constructor sets the private instance variable basePrice to 12. The method calculatePrice uses a local variable finalPrice, so each call computes a result without changing the object’s state.

    • For t.calculatePrice(10), the age is less than 13, so the price is reduced by 3: 12 - 3 = 9.
    • For t.calculatePrice(70), the age is at least 65, so the price is reduced by 2: 12 - 2 = 10.
    • t.getBasePrice() still returns 12 because calculatePrice does not modify basePrice.

    D is correct.

    A is incorrect because it treats the second call as if it changed basePrice or reused the previous result.

    B is incorrect because both calls to calculatePrice do apply discounts based on age.

    C is incorrect because the first call is not 9 followed by 12; the second call does not return the original base price.

  5. Question 5

    A program uses a ReadingPlan class to represent a reading assignment. The class must keep its data private, provide a one-parameter constructor that creates a plan with 5 sessions by delegating to a two-parameter constructor, and provide a method that returns a readable description of the plan. Which implementation meets all of these requirements?

    • A
      public class ReadingPlan {
          private String title;
          private int sessions;
      
          public ReadingPlan(String title) {
              new ReadingPlan(title, 5);
          }
      
          public ReadingPlan(String title, int sessions) {
              this.title = title;
              this.sessions = sessions;
          }
      
          public String description() {
              return title + ": " + sessions + " sessions";
          }
      }
    • B
      public class ReadingPlan {
          public String title;
          private int sessions;
      
          public ReadingPlan(String title) {
              this.title = title;
              this.sessions = 5;
          }
      
          public ReadingPlan(String title, int sessions) {
              this.title = title;
              this.sessions = sessions;
          }
      
          public String description() {
              return title + ": " + sessions + " sessions";
          }
      }
    • C
      public class ReadingPlan {
          private String title;
          private int sessions;
      
          public ReadingPlan(String title) {
              this(title, 5);
          }
      
          public ReadingPlan(String title, int sessions) {
              this.title = title;
              this.sessions = sessions;
          }
      
          public String description() {
              return title + ": " + sessions + " sessions";
          }
      }
      Correct answer
    • D
      public class ReadingPlan {
          private String title;
          private int sessions;
      
          public ReadingPlan(String title) {
              this.title = title;
              this(title, 5);
          }
      
          public ReadingPlan(String title, int sessions) {
              this.title = title;
              this.sessions = sessions;
          }
      
          public String description() {
              return title + ": " + sessions + " sessions";
          }
      }

    Explanation

    C is correct: the one-parameter constructor delegates to the two-parameter constructor, and the this(...) call is its first statement. The two-parameter constructor initializes both private instance fields, while description() provides the requested behavior without exposing those fields.

    D is incorrect because a constructor call using this(...) must be the first statement in a constructor.

    A is incorrect because new ReadingPlan(title, 5) constructs a separate object; it does not initialize the object being constructed.

    B is incorrect because title is public rather than private, and the one-parameter constructor does not delegate to the two-parameter constructor.

Frequently asked questions