Return to site

JAVA CERTIFICATION QUESTION: Overriding methods in Java

· java

This quiz’s code is deliberately designed to hint that the question is about one thing.

However, this question actually investigates something else.

Given these two classes:

public class Product {
  int id;
  String name;

  public Product(int i, String n) {
    id = i; name = n;
  }

  @Override
  public boolean equals(Product p) {
    return (p != null) 
      && (p.id==id) 
      && (p.name.equals(name));
  }
}

public class SetDemo {
  public static void main(String[] args) {
    Set<Product> set = Set.of(
      new Product(0, "P1"), 
      new Product(0, "P1"));
    System.out.print(set.stream().count());
  }
}

What is the result if you try to compile and run the code? Choose one.

A. 0

B. 1

C. 2

D. Compilation of the Product class fails.

E. The main method throws an exception during runtime.

 

 

 

·ꓷ uoᴉʇdo sᴉ ɹǝʍsuɐ ʇɔǝɹɹoɔ ǝɥꓕ ·uoᴉsnʅɔuoꓛ