AP Computer Science A · Topic 5.2

Constructors Practice

Part of Writing Classes.

Practice questions

19

Want a predicted score for the whole AP CSA exam? Take the 20-question diagnostic and Lumi will plan the rest.

Sample questions

5 of 19 — sign in to practice the rest with adaptive difficulty and mastery tracking.

  1. Sample 1difficulty 2/5

    public class Counter {
        private int count;
        public Counter() { count = 0; }
        public void inc() { count++; }
        public int getCount() { return count; }
    }
    // ...
    Counter c = new Counter();
    c.inc(); c.inc(); c.inc();
    System.out.println(c.getCount());

    What is printed?

    • A

      3

      check_circle
    • B

      0

    • C

      1

    • D

      Compile-time error

    Why

    The default constructor sets count to 0; inc() is called three times, so count becomes 3.

  2. Sample 2difficulty 2/5

    public class Foo {
        public void Foo() {
            System.out.println("ctor?");
        }
    }
    // ...
    Foo f = new Foo();

    What happens?

    • A

      Nothing is printed; Java's default constructor was used

      check_circle
    • B

      Run-time error

    • C

      Compile-time error

    • D

      ctor? is printed

    Why

    Foo here is a regular method (it has return type void), not a constructor; the implicit default constructor runs and produces no output.

  3. Sample 3difficulty 2/5

    public class Greeter {
        private String greeting;
        public Greeter() { greeting = "Hello"; }
        public Greeter(String g) { greeting = g; }
        public String say() { return greeting; }
    }
    // ...
    Greeter g = new Greeter("Hola");
    System.out.println(g.say());

    What is printed?

    • A

      Hola

      check_circle
    • B

      Hello

    • C

      null

    • D

      Compile-time error

    Why

    The String-parameter constructor sets greeting to "Hola".

  4. Sample 4difficulty 2/5

    public class Point {
        private int x, y;
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public int getX() { return x; }
        public int getY() { return y; }
    }
    // ...
    Point p = new Point(4, 7);
    System.out.println(p.getX() + "," + p.getY());

    What is printed?

    • A

      Compile-time error

    • B

      7,4

    • C

      0,0

    • D

      4,7

      check_circle

    Why

    The constructor uses this.x = x and this.y = y to assign instance variables, so getX returns 4 and getY returns 7.

  5. Sample 5difficulty 2/5

    public class Item {
        private String name;
        private double price;
        public Item() { name = "?"; price = 0.0; }
        public Item(String n, double p) { name = n; price = p; }
        public String getName() { return name; }
    }
    // ...
    Item i = new Item();
    System.out.println(i.getName());

    What is printed?

    • A

      0.0

    • B

      null

    • C

      ?

      check_circle
    • D

      Compile-time error

    Why

    The no-argument constructor sets name to "?" and price to 0.0.

AP Computer Science A · 5.2 Constructors — Practice Questions | Acemy