JAVA HOMEWORK HELP Consider the following pseudocode for the next six questions.
ID: 3694696 • Letter: J
Question
JAVA HOMEWORK HELP
Consider the following pseudocode for the next six questions. aList is type LinkedList<Integer>
aList.add(4);
aList.add(7);
aList.add(12);
System.out.println(aList.remove( ) ); // r1
aList.add(5);
aList.add(8);
aList.add(18);
System.out.println(aList.get(0)); // r2
System.out.println(aList.remove( )); // r3
aList.add(1, 3);
System.out.println(aList.remove( ));
System.out.println(aList.remove( )); // r4
System.out.println(aList.remove( )); // r5
Q1 What value is printed in the line of code marked r1?
Q2 What value is printed in the line of code marked r2?
Q3 What value is printed in the line of code marked r3?
Q4 What value is printed in the line of code marked r4?
Q5 What value is printed in the line of code marked r5?
Q6 After the code executes, how many elements would remain in a?
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class DemoLinkedList
{
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<Integer> aList = new LinkedList<Integer>();
aList.add(4);
aList.add(7);
aList.add(12);
System.out.println("r1 = "+aList.remove( ) ); // r1
aList.add(5);
aList.add(8);
aList.add(18);
System.out.println("r2 = "+aList.get(0)); // r2
System.out.println("r3 = "+aList.remove( )); // r3
aList.add(1, 3);
System.out.println(aList.remove( ));
System.out.println("r4 = "+aList.remove( )); // r4
System.out.println("r5 = "+aList.remove( )); // r5
System.out.println("Remaining Element in a:");
Iterator<Integer> itr=aList.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.