Constructors

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

Explanation

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

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

Related questions

AP Computer Science A · Constructors Practice Question | Acemy