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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.