AP Computer Science A · Topic 2.3

Calling a Void Method Practice

Part of Using Objects.

Practice questions

2

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

Sample questions

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

  1. Sample 1difficulty 1/5

    public class Printer {
        public void greet() {
            System.out.print("Hi");
        }
    }
    // ...
    Printer p = new Printer();
    int x = p.greet();

    What is the result?

    • A

      0

    • B

      null

    • C

      Hi

    • D

      Compile-time error

      check_circle

    Why

    A void method has no return value, so it cannot be assigned to an int variable.

  2. Sample 2difficulty 3/5

    Given <code>Dog d = new Dog("Rex");</code>, you call its bark method by

    • A

      bark.d()

    • B

      d.bark()

      check_circle
    • C

      Dog.bark()

    • D

      d->bark()

    Why

    Instance method via dot notation; static would use <code>Dog.method()</code>.