OCA Java SE 8 Core Java APIs - OCA Mock Question Core Java APIs 3-3








Question

What is the result of the following statements?

public class Main{
   public static void main(String[] argv){
     ArrayList<Integer> values = new ArrayList<>(); 
     values.add(4); 
     values.add(5); 
     values.set(1, 6); 
     values.remove(0); 
     for (Integer v : values) System.out.print(v); 
   }
}
  1. 4
  2. 5
  3. 6
  4. 46
  5. 45
  6. An exception is thrown.
  7. The code does not compile.




Answer



C.

Note

The code creates an array list and add two values to it. Then the code set the second value to 6 and removed the first element.

import java.util.ArrayList;
/* w w  w . j  a v  a  2s  .  c  o  m*/
public class Main{
   public static void main(String[] argv){
     ArrayList<Integer> values = new ArrayList<>(); 
     values.add(4); 
     System.out.print(values); 
     values.add(5); 
     System.out.print(values); 
     values.set(1, 6); 
     System.out.print(values); 
     values.remove(0); 
     System.out.print(values); 
     for (Integer v : values) 
       System.out.print(v); 
   }
}

The code above generates the following result.