Constructors

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

Explanation

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

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

Related questions