What is printed as a result of executing the following code?
ArrayList<Integer> alist = new ArrayList<Integer>();
alist.add(1);
alist.add(2);
alist.remove(1);
alist.add(1, 3);
alist.set(1, 4);
alist.add(5);
System.out.println(alist);
ArrayLists
Quiz
•
Computers
•
11th - 12th Grade
•
Medium
Used 16+ times
FREE Resource
10 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
What is printed as a result of executing the following code?
ArrayList<Integer> alist = new ArrayList<Integer>();
alist.add(1);
alist.add(2);
alist.remove(1);
alist.add(1, 3);
alist.set(1, 4);
alist.add(5);
System.out.println(alist);
[1, 4, 5]
[1, 4, 3, 5]
[2, 4, 5]
[2, 4, 3, 5]
2.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
Which statement below is the correct way to retrieve the first element in the nums ArrayList?
nums.get(0)
nums[0]
nums(0)
nums[1]
3.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
Given the ArrayList nums with the values [3, 7, 6, 0], what code below is the proper way to change the 7 to be a 5?
nums.set(1, 5)
nums.set(7, 5)
nums.set(5, 2)
nums[2] = 5
4.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
What is printed as a result of executing the following code segment?
List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.set(2, new Integer(4));
list1.add(2, new Integer(5));
list1.add(new Integer(6));
System.out.println(list1);
[1, 2, 3, 4, 5]
[1, 2, 4, 5, 6]
[1, 2, 5, 4 ,6]
[1, 5,2, 4 ,6]
5.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
Given the following code and assume that nums initially contains [0, 0, 4, 2, 5, 0, 3], what will nums contain as a result of executing numQuest?
private List<Integer> nums;
// precondition: nums.size() > 0;
// nums contains Integer objects
public void numQuest()
{
int k = 0;
Integer zero = new Integer(0);
while (k < nums.size())
{
if (nums.get(k).equals(zero))
nums.remove(k);
else
k++;
}
}
[0, 4, 2, 5, 3]
[3, 5, 2, 4, 0, 0, 0]
[0, 0, 0, 4, 2, 5, 3]
[4, 2, 5, 3]
6.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
Which of the following best describes the behavior of process1 and process2 (shown below)?
public static List<Integer> process1(int n)
{
List<Integer> someList = new ArrayList<Integer>();
for (int k = 0; k < n; k++)
someList.add(k);
return someList;
}
public static List<Integer> process2(int n)
{
List<Integer> someList = new ArrayList<Integer>();
for (int k = 0; k < n; k++)
someList.add(k, k);
return someList;
}
Both methods produce the same result, and process1 is faster than process2.
The two methods produce different results and take the same amount of time.
The two methods produce different results, and process1 is faster than process2.
The two methods produce different results, and process2 is faster than process1.
Both methods produce the same result and take the same amount of time.
7.
MULTIPLE CHOICE QUESTION
2 mins • 1 pt
What is printed as a result of executing the following code segment?
List<Integer> aList = new ArrayList<Integer>();
aList.add(new Integer(1));
aList.add(new Integer(2));
aList.add(1, new Integer(5));
aList.set(1, new Integer(4));
aList.add(new Integer(6));
aList.add(new Integer(3));
System.out.println(aList);
[1, 2, 5, 4, 6, 3]
[6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 6]
[1, 4, 2, 6, 3]
. [1, 2, 4, 6, 3]
14 questions
List Review Quiz
Quiz
•
9th - 12th Grade
11 questions
Javascript Lists Lecture Knowledge Check
Quiz
•
9th - 12th Grade
15 questions
Kuis Python 1
Quiz
•
11th Grade
15 questions
Python Quiz
Quiz
•
12th Grade
10 questions
12 th cs one mark 7 to 9
Quiz
•
12th Grade
10 questions
Списки Python + Pygame
Quiz
•
KG - University
15 questions
PCEP Section 3B: list methods: append(), insert(), index()
Quiz
•
12th Grade
10 questions
Python List Comprehension Review
Quiz
•
9th - 12th Grade
15 questions
Character Analysis
Quiz
•
4th Grade
17 questions
Chapter 12 - Doing the Right Thing
Quiz
•
9th - 12th Grade
10 questions
American Flag
Quiz
•
1st - 2nd Grade
20 questions
Reading Comprehension
Quiz
•
5th Grade
30 questions
Linear Inequalities
Quiz
•
9th - 12th Grade
20 questions
Types of Credit
Quiz
•
9th - 12th Grade
18 questions
Full S.T.E.A.M. Ahead Summer Academy Pre-Test 24-25
Quiz
•
5th Grade
14 questions
Misplaced and Dangling Modifiers
Quiz
•
6th - 8th Grade
17 questions
Chapter 12 - Doing the Right Thing
Quiz
•
9th - 12th Grade
30 questions
Linear Inequalities
Quiz
•
9th - 12th Grade
20 questions
Types of Credit
Quiz
•
9th - 12th Grade
20 questions
Taxes
Quiz
•
9th - 12th Grade
17 questions
Parts of Speech
Quiz
•
7th - 12th Grade
20 questions
Chapter 3 - Making a Good Impression
Quiz
•
9th - 12th Grade
20 questions
Inequalities Graphing
Quiz
•
9th - 12th Grade
10 questions
Identifying equations
Quiz
•
KG - University