Java OCA OCP Practice Question 3157

Question

What will the following program print?

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class Main {
 public static void main(String[] args) {
     List<String> l = new ArrayList<String>();
     l.add("A"); 
     l.add("B"); 
     l.add("C"); 
     l.add("D"); 
     l.add("E");//from   w w w.  j av a 2s .c  o m
     ListIterator<String> i = l.listIterator();
     i.next(); i.next(); i.next(); i.next();
     i.remove();
     i.previous(); i.previous();
     i.remove();
     System.out.println(l);
   }
}

Select the one correct answer.

  • (a) It will print [A, B, C, D, E].
  • (b) It will print [A, C, E].
  • (c) It will print [B, D, E].
  • (d) It will print [A, B, D].
  • (e) It will print [B, C, E].
  • (f) It will throw a NoSuchElementException.


(b)

Note

The remove() method removes the last element returned by either next() or previous() method.

The four next() calls return A, B, C, and D.

D is subsequently removed.

The two previous() calls return C and B.

B is subsequently removed.




PreviousNext

Related