ArrayList in AP Computer Science A
Which of the following loops is the correct way to iterate through an ArrayList<String>
called names
and print each name?
for (int i = 0; i < names.length; i++) { System.out.println(names[i]); }
for (String name : names) { System.out.println(name); }
for (int i = 0; i < names.size(); i++) { System.out.println(i); }
for (String name = 0; name < names.size(); name++) { System.out.println(name); }
What happens when you attempt to modify the size of an ArrayList
(e.g., adding or removing elements) while traversing it using a for-each loop?
The loop will automatically adjust and continue without issues.
A ConcurrentModificationException
is thrown.
The changes will not be reflected until the next iteration.
The loop will terminate prematurely without any error message.
What is the purpose of the size()
method in the ArrayList
class?
To check if the ArrayList is empty.
To return the maximum number of elements the ArrayList can hold.
To return the number of elements currently in the ArrayList.
To set the size of the ArrayList.
When is it NOT appropriate to use a for-each loop to traverse an ArrayList
?
When you need to access elements in reverse order.
When you need to modify the elements of the ArrayList.
When you need to know the index of the current element.
When you need to modify the size of the ArrayList during traversal.
Given an ArrayList<String>
called names
, what will be the output of names.size()
after the following operations?
names.add("Alice");
names.add("Bob");
names.remove(0);
0
1
2
3
Which of the following is NOT a recommended step for programmers to safeguard users' personal privacy?
Using encryption when handling personal data.
Collecting as much data as possible to improve application functionality.
Implementing security measures like firewalls.
Being transparent with users about data collection and usage.
Which of the following is the correct way to instantiate an ArrayList
that holds Integer
objects?
ArrayList
ArrayList list = new ArrayList();
ArrayList
ArrayList

How are we doing?
Give us your feedback and let us know how we can improve
Consider the following code snippet:
java
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.set(1, 40);
numbers.remove(0);
System.out.println(numbers.get(1));
What will be prin...
10
20
30
40
Which of the following code snippets correctly converts an ArrayList<Integer>
called list
to an Integer[]
array?
Integer[] array = list.toArray();
Integer[] array = (Integer[]) list.toArray();
Integer[] array = new Integer[list.size()]; for (int i = 0; i < list.size(); i++) { array[i] = list.get(i); }
Integer[] array = new Integer[list.size()]; array = list.toArray();
What is the primary ethical concern related to the collection of user data using ArrayLists or other data structures?
Ensuring data structures are efficiently implemented.
Protecting user privacy and preventing misuse of personal information.
Optimizing data retrieval speed.
Complying with copyright laws for data storage.