Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.ListIterator; import java.util.LinkedList; public class Test {

ID: 3684446 • Letter: I

Question

import java.util.ListIterator;
import java.util.LinkedList;

public class Test {

public static void main( String[] args ) {

LinkedList a, b;

a = new LinkedList();
b = new LinkedList();

a.add( "alpha" );
a.add( "bravo" );
a.add( "tango" );
a.add( "charlie" );

ListIterator i, j;

i = a.listIterator();
j = b.listIterator();

while ( i.hasNext() ) {
j.add( i.next() );
}

System.out.println( a );
System.out.println( b );

}

}

What is the result of the execution of the above Java program?

A. charlie, tango, bravo, alpha
   charlie, tango, bravo, alpha

B. alpha, bravo, tango, charlie
   alpha, bravo, tango, charlie

C. charlie, tango, bravo, alpha
   alpha, bravo, tango, charlie

D. alpha, bravo, tango, charlie
     charlie, tango, bravo, alpha

E. Runs into an infinite loop

Explanation / Answer

import java.util.ListIterator;
import java.util.LinkedList;
public class Test {
public static void main( String[] args ) {
LinkedList a, b;
a = new LinkedList();   //Creates a linked list a.
b = new LinkedList();   //Creates another linked list b.
a.add( "alpha" );           //Adds alpha to the end of the list in a.
a.add( "bravo" );           //Adds bravo to the end of the list in a.
a.add( "tango" );           //Adds tango to the end of the list in a.
a.add( "charlie" );       //Adds charlie to the end of the list in a.
ListIterator i, j;       //Creates 2 list iterators i and j.
i = a.listIterator();       //i iterates for each element of a.
j = b.listIterator();       //j iterated for each element of b.
while ( i.hasNext() ) {   //For each element of a, i.e., as long as there are elements in a.
j.add( i.next() );           //Add the same element to j, i.e, add it to the end of list in b.
}
System.out.println( a );   //Prints a. Therefore the output is: alpha, bravo, tango, charlie.
System.out.println( b );   //Prints b. Therefore the output is again: alpha, bravo, tango, charlie.
}
}

Therefore the answer is: B.