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

What is the printout of the following code? List list = new ArrayList(); list.ad

ID: 3574591 • Letter: W

Question

What is the printout of the following code?

List list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
for (int i = 0; i < list.size(); i++)
System.out.print(list.remove(i));

a) AD

b) ABC

c) AB

d) ABCD

e) AC

Suppose list is a LinkedList that contains 1 million int values. Analyze the following code:

A:
for (int i = 0; i < list.size(); i++)
sum += list.get(i);

B:
for (int i: list)
sum += i;

a) Code fragment A runs as fast as code fragment B.

b) Code fragment A runs faster than code fragment B.

c)Code fragment B runs faster than code fragment A.

Explanation / Answer

Question 1:

Answer is : e) AC

for (int i = 0; i < list.size(); i++)
System.out.println(list.remove(i));

so on the first execution it remove A , then stack will be

index 0 : B

index 1 : C

index 2 : D

So the second it remove index of 1 . so it remove 'C', then stack will be

index 0 : C

index 1 : D

then loop teminated .

Question 2:

Answer is : a) Code fragment A runs as fast as code fragment B.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote