AP CSA Unit-Level MCQ

Classes and Constructors

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

What classes and constructors covers

A class defines instance variables that hold each object's state and constructors that initialize them. A constructor has the same name as the class and no return type, and it typically assigns its parameters into the instance variables. When a parameter shares a name with an instance variable, the parameter shadows it, and this.field = field; is what distinguishes the two. Instance variables not assigned in a constructor keep their type defaults.

Where students lose points

Declaring local variables inside the constructor with the same names as the instance variables means the object is never actually initialized, and every getter then returns a default. Students also give constructors a void return type, which quietly turns them into ordinary methods, and they forget this when shadowing occurs.

How it shows up on the AP exam

The heart of Unit 3. Class-writing FRQs award points specifically for correct instance variable declarations and a constructor that stores its parameters, so this topic is directly worth exam points.

For practice use only.

Classes and Constructors MCQ Practice

AP CSA class and constructor practice: instance variables, constructor parameters, the this keyword, and what default values objects start with.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Code Tracing 01 - Classes and Constructors: track the final printed value.

public class Main {
  public static void main(String[] args) {
    Tracker t = new Tracker(18);
    t.add(3);
    t.add(5);
    System.out.print(t.getValue());
  }
}
class Tracker {
  private int value;
  public Tracker(int start) { value = start; }
  public void add(int amount) { value += amount; }
  public int getValue() { return value; }
}

All 15 classes and constructors questions

Work through the interactive quiz above first. This is the full classes and constructors question set with worked solutions, so students can review any question after attempting it.

  1. 1.Code Tracing 01 - Classes and Constructors: track the final printed value.

    public class Main {
      public static void main(String[] args) {
        Tracker t = new Tracker(18);
        t.add(3);
        t.add(5);
        System.out.print(t.getValue());
      }
    }
    class Tracker {
      private int value;
      public Tracker(int start) { value = start; }
      public void add(int amount) { value += amount; }
      public int getValue() { return value; }
    }
    • 18
    • 8
    • 27
    • 26
    Show worked solution

    Correct answer: 26

    The constructor initializes the instance variable. Each method call updates the same object state.

  2. 2.Code Tracing 12 - Classes and Constructors: follow the variable updates.

    public class Main {
      public static void main(String[] args) {
        Badge b = new Badge("PROG", 26);
        b.add(5);
        System.out.print(b.label());
      }
    }
    class Badge {
      private String name;
      private int points;
      public Badge(String n, int p) { name = n; points = p; }
      public void add(int extra) { points += extra; }
      public String label() { return name + ":" + points; }
    }
    • PROG:31
    • PROG:26
    • PROG31
    • 31:PROG
    Show worked solution

    Correct answer: PROG:31

    The object keeps both fields. The add method changes points, and label concatenates the current state.

  3. 3.Code Tracing 28 - Classes and Constructors: trace the branch and loop path.

    public class Main {
      public static void main(String[] args) {
        Counter c = new Counter(38);
        c.change(-3);
        c.change(10);
        System.out.print(c.read());
      }
    }
    class Counter {
      private int count;
      public Counter(int start) { count = start; }
      public void change(int amount) { count += amount; }
      public int read() { return count; }
    }
    • 45
    • 38
    • 51
    • 7
    Show worked solution

    Correct answer: 45

    Both method calls mutate the same private instance variable.

  4. 4.Code Tracing 39 - Classes and Constructors: watch the index changes.

    public class Main {
      public static void main(String[] args) {
        Score s = new Score(46, 5);
        s.bump(5);
        System.out.print(s.total());
      }
    }
    class Score {
      private int base;
      private int weight;
      public Score(int b, int w) { base = b; weight = w; }
      public void bump(int x) { base += x; }
      public int total() { return base * weight; }
    }
    • The code does not compile.
    • 255
    • 235
    • 230
    Show worked solution

    Correct answer: 255

    The bump method changes base before total multiplies by weight.

  5. 5.Code Tracing 50 - Classes and Constructors: evaluate the state change step by step.

    public class Main {
      public static void main(String[] args) {
        Card card = new Card("CODI", 54);
        card.next();
        card.next();
        System.out.print(card);
      }
    }
    class Card {
      private String name;
      private int level;
      public Card(String n, int l) { name = n; level = l; }
      public void next() { level++; }
      public String toString() { return name + "-" + level; }
    }
    • CODI-55
    • CODI56
    • CODI-56
    • CODI-54
    Show worked solution

    Correct answer: CODI-56

    Each call to next increments level, and printing the object uses its toString method.

  6. 6.What is printed by the following code segment?

    static class Box
    {
        private int n;
        public Box(int n) { this.n = n; }
        public Box() { this(7); }
        public int get() { return n; }
    }
    public static void main(String[] args)
    {
        System.out.print(new Box().get() + new Box(3).get());
    }
    • 10
    • 14
    • 6
    • 7
    Show worked solution

    Correct answer: 10

    One constructor delegates to the other, so the no-argument form supplies the default 7.

  7. 7.What is printed by the following code segment?

    static class C
    {
        private int a = 1;
        private int b = a + 1;
        public C() { a = 10; }
        public int sum() { return a + b; }
    }
    public static void main(String[] args)
    {
        System.out.print(new C().sum());
    }
    • 20
    • 12
    • 11
    • 3
    Show worked solution

    Correct answer: 12

    Field initialisers run before the constructor body, so b is computed from the original value of a.

  8. 8.What is printed by the following code segment?

    static class P
    {
        private int x;
        public P(int x) { x = x; }
        public int get() { return x; }
    }
    public static void main(String[] args)
    {
        System.out.print(new P(5).get());
    }
    • 5
    • The code does not compile.
    • 0
    • -1
    Show worked solution

    Correct answer: 0

    The parameter shadows the field, so the assignment copies the parameter onto itself.

  9. 9.What is printed by the following code segment?

    static class Q
    {
        private int[] d;
        public Q(int n) { d = new int[n]; }
        public int len() { return d.length; }
    }
    public static void main(String[] args)
    {
        System.out.print(new Q(3).len() + new Q(0).len());
    }
    • 0
    • 6
    • An exception is thrown.
    • 3
    Show worked solution

    Correct answer: 3

    A zero-length array is legal, so both constructions succeed.

  10. 10.What is printed by the following code segment?

    static class R
    {
        private static int made = 0;
        private int id;
        public R() { made++; id = made; }
        public int getId() { return id; }
        public static int total() { return made; }
    }
    public static void main(String[] args)
    {
        new R();
        R b = new R();
        System.out.print(b.getId() + " " + R.total());
    }
    • 2 2
    • 1 2
    • 2 1
    • 1 1
    Show worked solution

    Correct answer: 2 2

    A static field is shared by every object, so it counts constructions while each object keeps its own id.

  11. 11.What is printed by the following code segment?

    static class S
    {
        private String s;
        public S(String s) { this.s = s.trim(); }
        public int len() { return s.length(); }
    }
    public static void main(String[] args)
    {
        System.out.print(new S("  ab  ").len());
    }
    • 6
    • 2
    • 4
    • 0
    Show worked solution

    Correct answer: 2

    A constructor may normalise its argument before storing it.

  12. 12.What is printed by the following code segment?

    static class T
    {
        private int v;
        public T(int v) { this.v = v; }
        public T copy() { return new T(v); }
        public void set(int n) { v = n; }
        public int get() { return v; }
    }
    public static void main(String[] args)
    {
        T a = new T(1);
        T b = a.copy();
        a.set(9);
        System.out.print(a.get() + " " + b.get());
    }
    • 9 9
    • 1 1
    • 9 1
    • 1 9
    Show worked solution

    Correct answer: 9 1

    The copy holds its own field, so changing the original does not affect it.

  13. 13.Which client statement fails to compile?

    static class U
    {
        private int n;
        public U(int n) { this.n = n; }
    }
    • U u = new U(0);
    • U u = new U(-1);
    • U u; u = new U(2);
    • U u = new U();
    Show worked solution

    Correct answer: U u = new U();

    Declaring any constructor removes the default no-argument one.

  14. 14.What is printed by the following code segment?

    static class V
    {
        private int n = 5;
        public V(int n) { }
        public int get() { return n; }
    }
    public static void main(String[] args)
    {
        System.out.print(new V(9).get());
    }
    • 5
    • 9
    • 0
    • The code does not compile.
    Show worked solution

    Correct answer: 5

    The constructor ignores its parameter entirely, so the field keeps the value from its initialiser.

  15. 15.What is printed by the following code segment?

    static class W
    {
        private int a;
        private int b;
        public W(int a) { this.a = a; }
        public W(int a, int b) { this(a); this.b = b; }
        public int sum() { return a + b; }
    }
    public static void main(String[] args)
    {
        System.out.print(new W(2, 3).sum() + new W(4).sum());
    }
    • 5
    • 9
    • 12
    • 4
    Show worked solution

    Correct answer: 9

    The two-argument constructor delegates for the first field and sets the second itself.