Java OCA OCP Practice Question 1371

Question

What sequence of digits will the following program print?

import java.util.* ; 
public class Main{ 
   public static void main (String args []){ 
      List s1 = new ArrayList ( ); 
      s1.add ("a"); 
      s1.add ("b"); 
      s1.add (1, "c"); 
      List s2 = new ArrayList (  s1.subList (1, 1) ); 
      s1.addAll (s2); /*w  w w.  j  a  v a2s . c  om*/
      System .out.println (s1); 
    } 
} 

Select 1 option

  • A. The sequence a, b, c is printed.
  • B. The sequence a, b, c, b is printed.
  • C. The sequence a, c, b, c is printed.
  • D. The sequence a, c, b is printed.
  • E. None of the above.


Correct Option is  : D

Note

First, "a" and "b" are appended to an empty list.

Next, "c" is added between "1" and "2".

A new list s2 is created using the sublist view allowing access to elements from index 1 to index 1(exclusive) (i.e. no elements ).

Now, s2 is added to s1.

So s1 remains:a, c, b




PreviousNext

Related