/
Java ArrayList Concepts
Save to my account
Sign up
Java ArrayList Concepts
Java ArrayList Concepts
Study
1
Question
What will cause an error in the statement `Object ob = list.get(6)` given that list is an ArrayList containing 2, 5, 7, 6, 0, 1?
Answer
This statement will cause an error because the index 6 is out of bounds for the list, which has indices 0 to 5.
2
Question
What does the statement `Integer intOb = list.get(2)` do and will it cause an error?
Answer
This statement retrieves the element at index 2 of the list, which is 7. It will not cause an error.
3
Question
What will happen if you execute `list.add(6, new Integer(9))` and will it cause an error?
Answer
This statement will cause an error because the index 6 is out of bounds for the list, which only has 6 elements.
4
Question
Will the statement `Object x = list.remove(6)` cause an error?
Answer
Yes, this statement will cause an error since index 6 is out of bounds for the list.
5
Question
What is the result of executing the statement `Object y = list.set(6, new Integer(8))`?
Answer
This will also cause an error because index 6 is out of bounds.
6
Question
Given the initial state of the list as [6, 1, 8], what will be the updated list after executing the following code segment: `int len = list.size(); for (int i = 0; i < len; i++) { list.add(i, new Integer(i + 1)); Object x = list.set(i, new Integer(i + 2)); }`?
Answer
The final list will be [2, 3, 4, 6, 1, 8]. The first three positions are filled with 2, 3, 4, while the last three remain unchanged.
7
Question
Which declaration correctly creates an ArrayList with an initial capacity of 20?
Answer
The correct declaration is `ArrayList<Object> list = new ArrayList<Object>(20);`.
8
Question
How can we assign the elements of the colors array to colorList correctly while maintaining the order?
Answer
The correct way to do this is by using option II: `for (int i = 0; i < colors.length; i++) colorList.add(colors[i]);`.
9
Question
What are the advantages of using an ArrayList of Strings instead of an array of Strings for reading lines of text from a file?
Answer
The advantages include automatic resizing of ArrayList for unexpectedly long text files, easy access to methods like size(), and more convenient get/set methods.
10
Question
In the ArrayList `list` defined as `ArrayList<String> list = new ArrayList<String>(); list.add("Andy"); list.add("Bart"); list.add("Carl"); list.add(0, "Eve");` what element is at index 2 after these operations?
Answer
The element at index 2 will be 'Bart'.
11
Question
If the ArrayList `list` contains "Andy", "Bart", "Carl", "Doug", "Elmo", how can we change it to look like "Andy", "Bart", "Carl", "Doug", "Oscar", "Elmo"?
Answer
You can use the statement `list.add(3, "Oscar");` or `list.set(3, "Oscar");` to replace Doug with Oscar.
12
Question
Which declaration is appropriate for a list expected to contain integers?
Answer
The appropriate declaration is `ArrayList<Integer> list = new ArrayList<Integer>();`.
13
Question
What is the initial content of the ArrayList 'list' after executing the following code? ArrayList<String> list = new ArrayList<String>(); list.add("Andy"); list.add("Bart"); list.add("Carl"); list.add("Doug"); list.add("Elmo");
Answer
The initial content of the ArrayList 'list' is: ["Andy", "Bart", "Carl", "Doug", "Elmo"]
14
Question
What will the ArrayList 'list' look like after executing list.remove(3)?
Answer
The list will look like: ["Andy", "Bart", "Carl", "Elmo"] because 'Doug' at index 3 is removed.
15
Question
What does the 'list.remove(2)' method do to the ArrayList 'list'?
Answer
The method removes the element at index 2, which is 'Carl'. After execution, the list will look like: ["Andy", "Bart", "Doug", "Elmo"].
16
Question
What will happen to the list if you execute list.add(2, null)?
Answer
This will insert 'null' at index 2. The updated list will look like: ["Andy", "Bart", null, "Carl", "Doug", "Elmo"].
17
Question
What command is used to replace the element 'Carl' with 'Zoltan' in the list?
Answer
To replace 'Carl' with 'Zoltan', the command is: list.set(list.indexOf("Carl"), "Zoltan"). This finds the index of 'Carl' and sets it to 'Zoltan'.
18
Question
After executing the following code, what is the capacity and size of the ArrayList 'list'? ArrayList<String> list = new ArrayList<String>(10); list.add("Ann"); list.add("Bob"); list.add("Eve");
Answer
The capacity of the list is 10 and the size is 3, because although only 3 elements are added, the initial capacity is set to 10.
19
Question
What is the advantage of using an array of primitive types (int or double) over an ArrayList of Integer or Double objects?
Answer
An array of primitive number types is more efficient to manipulate than an ArrayList of wrapper objects because primitive types do not have the overhead of object wrapping, which can lead to better performance in terms of speed and memory.
20
Question
What will the code do if the ArrayList 'list' contains "red", "red", "green" and the following code is executed? String element = "red"; for (int i = 0; i < list.size(); i++) { if (list.get(i).equals(element)) { list.remove(element); } }
Answer
The resulting list will be: ["green"]. The loop removes all occurrences of 'red', which are both found and removed from the list.
21
Question
In the context of ArrayLists, explain the difference between 'capacity' and 'size'.
Answer
'Size' refers to the number of elements currently stored in the ArrayList, while 'capacity' refers to the total amount of space allocated to the ArrayList, which can exceed its current size.
22
Question
What behavior does the method 'list.clear()' exhibit on an ArrayList?
Answer
The method 'list.clear()' removes all components from the list, leaving it empty, and thus the size of the list becomes 0.