Calling a Void Method

AP Computer Science A· difficulty 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

Explanation

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

Want 10 more like this — adaptive to your weak spots?

Related questions