Java OCA OCP Practice Question 931

Question

What is the result of the following?

import java.util.ArrayList;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      List<String> one = new ArrayList<String>();
      one.add("abc");
      List<String> two = new ArrayList<>();
      two.add("abc");
      if (one == two)
         System.out.println("A");
      else if (one.equals(two))
         System.out.println("B");
      else// ww  w  .  jav  a2 s  .  com
         System.out.println("C");
   }
}
  • A. A
  • B. B
  • C. C
  • D. An exception is thrown.
  • E. The code does not compile.


B.

Note

The first if statement is false because the variables do not point to the same object.

The second if statement is true because ArrayList implements equality to mean the same elements in the same order.




PreviousNext

Related