ArrayLists Practice Questions (AP CSA)
Java MCQ with Answers
| <-- Back to Two-dimensional Arrays | Next to Text Files--> |
ArrayLists in Java
What is an ArrayList?
An ArrayList is a resizable array from the java.util package that stores objects (not primitives). It is declared with a type parameter in angle brackets, e.g., ArrayList<Integer> list = new ArrayList<Integer>();. Unlike arrays, its size changes automatically as elements are added or removed. Because it stores objects, primitive types such as int and double must use their wrapper classes Integer and Double. Java automatically converts between primitives and their wrappers through autoboxing and unboxing.
Core ArrayList Methods
Traversal and Modification During Loops
Tips for AP-CSA Exam
In the AP Computer Science A (AP CSA) exam, questions on ArrayLists test the ability to trace the effect of add(), remove(), and set() on the list's contents and indices, predict the output of Collections.sort() and Collections.reverse(), and identify off-by-one errors when inserting or removing elements inside loops. Always track both the index and the list size after every operation when tracing.
Q1. The method doSomething() is invoked as follows:
ArrayList intList = new ArrayList();
intList.add(16);
intList.add(79);
intList.add(11);
intList.add(89);
doSomething(intList, 32);
What will be the values in the intList after the execution of the doSomething() method?
public static void doSomething(ArrayList intList, Integer n)
{
int i=0;
for (i=0; i < intList.size() && n > intList.get(i); i++);
intList.add(i, n);
}
(A) [16, 79, 11, 32, 89]
(B) [11, 16, 32, 79, 89]
(C) [16, 32, 79, 11, 89]
(D) [16, 79, 11, 89, 32]
(E) [32, 16, 79, 11, 89]
Answer:
View Output(C) [16, 32, 79, 11, 89]
Explanation:
The method doSomething() uses a for-loop with two conditions; the first condition helps to control the index variable from moving outside the length of the arraylist intList. The second condition compares the value of the variable 'n' with every value in the arraylist in order to find a suitable position for insertion. As soon as it finds that for a particular value in the arraylist, the value of 'n' becomes large, it stops the execution of the loop and adds the value 'n' at the current index 'i'. Before the execution of the doSomething() method, the intList contains the following values: [16, 79, 11, 89]. For these values in the arraylist, since 16 is less than 32, it goes in the loop again; then 79 is greater than 32; here the loop execution stops.
| i | i < intList.size() && n > intList.get(i) | i++; | |
|---|---|---|---|
| For-loop: First execution | 0 | (0 < 4) && (32 > 16) -> TRUE | 1 |
| For-loop: Second execution | 1 | (1 < 4) && (32 > 79) -> FALSE | |
| Loop terminated | |||
| intList.add(i, n); -> intList.add(1, n); -> [16, 32, 79, 11, 89] | |||
Q2. The arraylist intList initially consists of the following values:
[1101, 233, 850, 683]
What would be output upon execution of the doSomething() method?
public static void doSomething(ArrayList intList)
{
int i=0;
for (i=0; i < intList.size(); i++)
{
if (intList.get(i) %2 == 0)
{
System.out.println(intList.set(i, intList.get(i)+1));
}
}
System.out.println(intList);
}
(A) 1101
233
[1101, 233, 851, 683]
(B) 850
[1101, 233, 851, 683]
(C) 851
[1101, 233, 851, 683]
(D) 1101
[1101, 233, 851, 683]
(E) 1101
233
850
[1101, 233, 851, 683]
Answer:
View Output(B) 850
[1101, 233, 851, 683]
Explanation:
The method doSomething() is designed to search for any even values (%2 ==0) in the arrayList and print them and then replace them with a new value which is equal to the original value incremented by 1.
| i | i < intList.size(); | i++; | |
|---|---|---|---|
| For-loop: First execution | 0 | (0 < 4) -> TRUE | |
|
if (intList.get(i) %2 == 0) -> if (intList.get(0) %2 == 0)-> if (1101 %2 == 0)-> FALSE -> Body of If statement is NOT executed |
|||
| 1 | |||
| For-loop: Second execution | 1 | (1 < 4) -> TRUE | |
|
if (intList.get(i) %2 == 0) -> if (intList.get(1) %2 == 0)-> if (233 %2 == 0)-> FALSE -> Body of If statement is NOT executed |
|||
| 2 | |||
| For-loop: Third execution | 2 | (2 < 4) -> TRUE | |
|
if (intList.get(i) %2 == 0) -> if (intList.get(1) %2 == 0)-> if (850 %2 == 0)-> TRUE -> Body of If statement gets executed |
|||
|
System.out.println(intList.set(i, intList.get(i)+1)); -> System.out.println(intList.set(2, intList.get(2)+1)); -> System.out.println(intList.set(2, 850+1)); -> System.out.println(850); -> 850 The set() method of ArrayList returns the element that was previously at the index i. The values in the intList are now: [1101, 233, 851, 683] |
|||
| 3 | |||
| For-loop: Fourth execution | 3 | (3 < 4) -> TRUE | |
|
if (intList.get(i) %2 == 0) -> if (intList.get(1) %2 == 0)-> if (683 %2 == 0)-> FALSE -> Body of If statement is NOT executed |
|||
| 4 | |||
| For-loop: Final execution | 4 | (4 < 4) -> FALSE | |
| Loop terminated | |||
Q3. What will be the output of the program after execution of the method doSomething()?
public static void doSomething()
{
ArrayList intList = new ArrayList();
intList.add(4);
intList.add(67);
intList.add(2,5);
intList.set(0,32);
Collections.sort(intList);
intList.add(2,34);
System.out.println(intList);
}
(A) [4, 32, 34, 67]
(B) [5, 34, 32, 67]
(C) [4, 34, 32, 67]
(D) [5, 32, 34, 67]
(E) [4, 32, 32, 67]
Answer:
View Output(D) [5, 32, 34, 67]
Explanation:
| Statement | Contents of intList |
|---|---|
| intList.add(4); | [4] |
| intList.add(67); | [4, 67] |
| intList.add(2,5); | [4, 67, 5] |
| intList.set(0,32); | [32, 67, 5] |
| Collections.sort(intList); | [5, 32, 67] |
| intList.add(2,34) | [5, 32, 34, 67] |
Q4. What will be the contents of the arraylist intList before and after the execution of the method removeFromList()?
ArrayList intList = new ArrayList();
intList.add(893);
intList.add(31);
intList.add(1,601);
intList.set(2,591);
System.out.println(intList);
removeFromList(intList);
System.out.println(intList);
public static void removeFromList(ArrayList intList)
{
for (int i=0; i< intList.size(); i++)
{
if (intList.get(i)%2 != 0) intList.remove(i);
}
}
(A) [893, 601, 591] and [601]
(B) [893, 601, 31] and [601]
(C) [601, 591] and [591]
(D) [893, 591] and [893, 591]
(E) [893, 31] and [893, 31]
Answer:
View Output(A) [893, 601, 591] and [601]
Explanation:
Before the execution of the method removeFromList(intList);
| Statement | Contents of intList |
|---|---|
| intList.add(893); | [893] |
| intList.add(31); | [893, 31] |
| intList.add(1,601); | [893, 601, 31] |
| intList.set(2,591); | [893, 601, 591] |
During and after the execution of the method removeFromList(intList);
| i | i < intList.size() | i++; | |
|---|---|---|---|
| For-loop: First execution | 0 | (0 < 3) -> TRUE | |
|
if (intList.get(i)%2 != 0) -> if (intList.get(0)%2 != 0) -> if (893%2 != 0)-> TRUE -> intList.remove(i); -> intList.remove(0); -> [601, 591] // removes the value 893 from the list |
|||
| 1 | |||
| For-loop: Second execution | 1 | (1 < 2) -> TRUE | |
|
if (intList.get(i)%2 != 0) -> if (intList.get(1)%2 != 0) -> if (591 %2 != 0)-> TRUE -> intList.remove(i); -> intList.remove(1); -> [601] // removes the value 591 from the list |
|||
| 2 | |||
| For-loop: Final execution | 2 | (2 < 1) -> FALSE | |
| Loop terminates | |||
Note: Despite of the fact that the value 601 is an odd number, it does not get deleted from the arrayList because the index counter moves ahead (after deleting the value 893) and the value 601 is never tested for being odd or even.
Q5. What will be the contents of the arraylist intList1 and intList2 before the execution of the method compareLists() and what will the method compareLists() return?
public static void main(String args[])
{
ArrayList intList1 = new ArrayList();
intList1.add(893);
intList1.add(31);
intList1.add(1,601);
intList1.add(3,591);
System.out.println(intList1);
ArrayList intList2 = new ArrayList();
intList2.add(893);
intList2.add(31);
intList2.add(601);
intList2.add(2,591);
System.out.println(intList2);
System.out.println(compareLists(intList1, intList2));
}
public static boolean compareLists(ArrayList intList1, ArrayList intList2)
{
if (intList1.size() != intList2.size()) return false;
else return true;
}
(A) [893, 31, 601, 591] and [893, 31, 591, 601] and true
(B) [893, 601, 31, 591] and [893, 31, 601, 591] and true
(C) [893, 601, 591] and [893, 31, 591, 601] and false
(D) [893, 601, 31, 591] and [893, 31, 591, 601] and true
(E) [893, 601, 31, 591] and [893, 591, 601] and false
Answer:
View Output(D) [893, 601, 31, 591] and [893, 31, 591, 601] and true
Explanation:
Contents of the intList1 before the execution of the method compareLists():
| Statement | Contents of intList1 |
|---|---|
| intList1.add(893); | [893] |
| intList1.add(31); | [893, 31] |
| intList1.add(1,601); | [893, 601, 31] |
| intList1.add(3,591); | [893, 601, 31, 591] |
Contents of the intList2 before the execution of the method compareLists():
| Statement | Contents of intList2 |
|---|---|
| intList2.add(893); | [893] |
| intList2.add(31); | [893, 31] |
| intList2.add(601); | [893, 31, 601] |
| intList2.add(2,591); | [893, 31, 591, 601] |
The compareLists() method simply compares the lengths of the two arraylists and returns true if they are same else returns false. Since, in this case, both the arraylists have the same number of elements, the method returns true.
Q6. What will be the contents of the arraylist intList after the execution of the method makeList()?
public static void makeList(ArrayList intList)
{
for (int i=0, num=1; i< 3; i++, num++)
{
intList.add(0,i);
intList.add(intList.size()-1, num++);
}
}
(A) [2, 1, 2, 4, 6, 0]
(B) [1, 2, 2, 4, 6, 0]
(C) [2, 1, 4, 2, 6, 0]
(D) [1, 1, 2, 3, 5, 0]
(E) [2, 1, 1, 3, 5, 0]
Answer:
View Output(E) [2, 1, 1, 3, 5, 0]
Explanation:
| i | num | i < 3 | i++ | num++ | intList | |
|---|---|---|---|---|---|---|
| For-loop: First execution | 0 | 1 | (0 < 3) -> TRUE | |||
|
intList.add(0,i); -> intList.add(0,0); -> [0] |
[0] | |||||
|
intList.add(intList.size()-1, num++);-> intList.add(1-1, num++);-> intList.add(0, 1) -> [1, 0] // num++ results in using the value of num in the expression and then it is incremented |
2 | [1, 0] | ||||
| 1 | 3 | |||||
| For-loop: Second execution | 1 | 3 | (1 < 3) -> TRUE | |||
|
intList.add(0,i); -> intList.add(0,1); -> [1, 1, 0] |
[1, 1, 0] | |||||
|
intList.add(intList.size()-1, num++);-> intList.add(3-1, num++);-> intList.add(2, 3) -> [1, 1, 3, 0] |
4 | [1, 1, 3, 0] | ||||
| 2 | 5 | |||||
| For-loop: Third execution | 2 | 5 | (2 < 3) -> TRUE | |||
|
intList.add(0,i); -> intList.add(0,2); -> [2, 1, 1, 3, 0] |
[2, 1, 1, 3, 0] | |||||
|
intList.add(intList.size()-1, num++);-> intList.add(5-1, num++);-> intList.add(4, 5) -> [2, 1, 1, 3, 5, 0] |
6 | [2, 1, 1, 3, 5, 0] | ||||
| 3 | 7 | |||||
| For-loop: Final execution | 3 | 7 | (3 < 3) -> FALSE | |||
| Loop terminates | ||||||
Q7. What will be the contents of the arraylist strList before and after the execution of the Collections.sort() method?
public static void main(String args[])
{
ArrayList strList = new ArrayList();
strList.add("Look");
strList.add("Care");
strList.add(1,"Laugh");
strList.add(2,"Speak");
System.out.println(strList);
Collections.sort(strList);
System.out.println(strList);
}
(A) [Laugh, Look, Speak, Care] and [Care, Laugh, Look, Speak]
(B) [Look, Laugh, Speak, Care] and [Care, Laugh, Look, Speak]
(C) [Look, Laugh, Care, Speak] and [Care, Look, Laugh, Speak]
(D) [Laugh, Care, Look, Speak] and [Care, Laugh, Look, Speak]
(E) [Laugh, Look, Care, Speak] and [Care, Look, Laugh, Speak]
Answer:
View Output(B) [Look, Laugh, Speak, Care] and [Care, Laugh, Look, Speak]
Explanation:
Contents of the strList before the execution of the method Collections.sort():
| Statement | Contents of strList |
|---|---|
| strList.add("Look"); | [Look] |
| strList.add("Care"); | [Look, Care] |
| strList.add(1,"Laugh"); | [Look, Laugh, Care] |
| strList.add(2,"Speak"); | [Look, Laugh, Speak, Care] |
Contents of the strList after the execution of the method Collections.sort():
Since this method sorts the strings based on the lexicographical order, the final contents in the strList will be: [Care, Laugh, Look, Speak].
Q8. What will be the contents of the arraylist strList before and after the execution of the Collections.reverse() method?
public static void main(String args[])
{
ArrayList strList = new ArrayList();
strList.add("stack");
strList.add("stuck");
strList.add(0,"stook");
strList.add(2,"stalk");
System.out.println(strList);
Collections.reverse(strList);
System.out.println(strList);
}
(A) [stook, stalk, stack, stuck] and [stuck, stack, stalk, stook]
(B) [stook, stack, stalk, stuck] and [stack, stalk, stook, stuck]
(C) [stook, stack, stalk, stuck] and [stuck, stook, stalk, stack]
(D) [stook, stack, stalk, stuck] and [stuck, stalk, stack, stook]
(E) [stook, stack, stuck, stalk] and [stalk, stuck, stack, stook]
Answer:
View Output(D) [stook, stack, stalk, stuck] and [stuck, stalk, stack, stook]
Explanation:
Contents of the strList before the execution of the method Collections.reverse():
| Statement | Contents of strList |
|---|---|
| strList.add("stack"); | [stack] |
| strList.add("stuck"); | [stack, stuck] |
| strList.add(0,"stook"); | [stook, stack, stuck] |
| strList.add(2,"stalk"); | [stook, stack, stalk, stuck] |
Contents of the strList after the execution of the method Collections.reverse():
Since this method simply reverses the contents of the list, the final contents in the strList will be: [stuck, stalk, stack, stook].
Q9. What will be the contents of the arraylist strList before and after the execution of the Collections.sort() method?
public static void main(String args[])
{
ArrayList strList = new ArrayList();
strList.add("566");
strList.add("goal");
strList.add(1,"win");
strList.add(2,"9All");
strList.add(2,"lost");
System.out.println(strList);
Collections.sort(strList);
System.out.println(strList);
}
(A) [566, win, lost, 9All, goal] and [566, 9All, goal, lost, win]
(B) [win, 566, lost, 9All, goal] and [566, 9All, goal, lost, win]
(C) [566, win, 9All, lost, goal] and [goal, lost, win, 566, 9All]
(D) [win, 566, 9All, lost, goal] and [win, lost, goal, 566, 9All]
(E) [566, win, lost, 9All, goal] and [win, lost, goal, 566, 9All]
Answer:
View Output(A) [566, win, lost, 9All, goal] and [566, 9All, goal, lost, win]
Explanation:
Contents of the strList before the execution of the method Collections.sort():
| Statement | Contents of strList |
|---|---|
| strList.add("566"); | [566] |
| strList.add("goal"); | [566, goal] |
| strList.add(1,"win"); | [566, win, goal] |
| strList.add(2,"9All"); | [566, win, 9All, goal] |
| strList.add(2,"lost"); | [566, win, lost, 9All, goal] |
Contents of the strList after the execution of the method Collections.sort():
Since this method sorts the strings based on the lexicographical order, the final contents in the strList will be: [566, 9All, goal, lost, win].
Rules of lexicographical ordering of Strings:
- Unicode Values: If the characters are different, their Unicode values are compared to determine the order.
- Case Sensitivity: Uppercase letters come before lowercase letters in the Unicode standard.
- Prefixes: If one string is a prefix of another, the shorter string comes first.
- Numbers come before uppercase and then comes lower case characters.
| <-- Back to Two-dimensional Arrays | Next to Text Files--> |