Writing Methods

AP Computer Science A· difficulty 2/5

public class Printer {
    public String show(int n) { return "int"; }
    public String show(double n) { return "double"; }
}
// ...
Printer p = new Printer();
System.out.println(p.show(3.0));

What is printed?

  • A

    double

    check_circle
  • B

    int

  • C

    Ambiguous overload

  • D

    Compile-time error

Explanation

The literal 3.0 is a double, so the show(double) overload is selected.

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

Related questions