WorksheetsArrays and ArrayLists in Java
Total questions: 25
Worksheet time: 6hrs 15mins
How do you add an element with the value "Pikachu" to the back of an ArrayList called "pokemon" in Java?
pokemon.add("Pikachu");
pokemon.append("Pikachu");
pokemon.push_back("Pikachu");
pokemon[last_index + 1] = "Pikachu";
If an ArrayList<String> list has been initialized with 5 strings, the statement
list.add(6, "cat");
will throw an IndexOutOfBoundsException.
TRUE
FALSE
You can add an item in a(n) ____ and the size expands automatically to accommodate the new item.
Array
ArrayList
ResizableArray
array
What is printed as a result of executing the code segment?
Consider the following method from the picture below. What is printed as a result of executing the code segment?
[a, b, c, d, e, f]
[a, c, e, d, f]
[c, b, e, d, f]
[c, b, e, f]
Which index is the last element in an ArrayList called nums at?
nums.length
nums.length - 1
nums.length()
nums.size()
nums.size() - 1
Which of the following is a reason to use an ArrayList instead of an array?
An ArrayList can grow or shrink as needed, while an array is always the same size.
You can use a for-each loop on an ArrayList, but not in an array.
You can store objects in an ArrayList, but not in an array.
Which of the following is the correct way to get the first value in an ArrayList called nums?
nums[0]
nums[1]
nums.first()
nums.get(1)
nums.get(0)
Which of the following is the correct way to set the third value in an ArrayList called nums to 5?
nums[2] = 5;
nums[3] = 5;
nums.set(5, 2);
nums.set(2, 5);
nums.set(3, 5);
Which of the following is the correct way to remove the value 1 from the ArrayList nums = [5, 3, 2, 1]?
nums.remove(3);
nums.remove(1);
nums.remove(2);
nums.remove(4);
nums.remove(nums.size());
Which of the following is the correct way to add 2 between the 1 and 3 in the following ArrayList nums = [1, 3, 4]?
nums.add(1, 2);
nums.add(2, 0);
nums.add(2, 1);
nums.add(0, 2);
nums.add(2, 2);
What will print when the following code executes?
[1, 3]
[1, 2]
[1, 2, 3]
[2, 3]
What will print when the following code executes?
["Sarah", "Destini", "Layla", "Sharrie"]
["Sarah", "Destini", "Anaya", "Layla", "Sharrie"]
["Sarah", "Layla", "Sharrie"]
["Destini", "Layla", "Sharrie", "Sarah"]
I want to create a new ArrayList to hold objects of type Basketball.
ArrayList <Basketeball> team = /* missing code*/
What is the correct missing code?
new Basketball ( )
new ArrayList <Basketball> ( )
new ArrayList (Basketball)
new ArrayList (<Basketball>)
new ArrayList < > (Thing)
ArrayList<Integer> nums = new ArrayList< > () ;
nums.add(5);
numds.add(4);
nums.add(3);
nums.add(2);
nums.add(0,6);
nums.set(3,4);
nums.remove(3);
nums.add(2,2);
[4, 6, 5, 4, 2]
[5, 4, 2, 3, 2]
[6, 4, 2, 4, 2]
[6, 5, 2, 4, 2]
[6, 5, 2, 5, 2]
ArrayList <String> states = new ArrayList <String> ();
states.add("LA");
states.add(0, "DE");
states.set(1, "TX");
states.add("IL");
states.add(2, states.get(0));
states.remove(1);
[DE, IL, DE]
[DE, DE, IL]
[LA, LA, IL]
[TX, TX, IL]
[TX, TX, IL]
ArrayList<Integer> nums = new ArrayList<Integer> ();
nums.add(nums.size(), nums.size());
nums.add(nums.size() - 1, nums.size() +1);
nums.add(nums.size() -2, nums.size() +2);
[[0, 1, 2]
[0, 2, 4]
[1, 2, 3]
[2, 1, 0]
[4, 2, 0]
ArrayList<Integer> nums = new ArrayList();
for (int i = 0; i < 5; i++) {
nums.add(i + 1);
}
for (int i = 0; i < 5; i++) {
if (nums.get(i) % 2 == 1) {
System.out.print(nums.get(i) + " ");
}
}
What is printed?
2 4
1 3
1 3 5
3 5
ArrayList<String> pets = new ArrayList<String> ( );
pets.add("cat");
pets.add("dog");
pets.add("pig");
pets.add("fish");
pets.add("rat");
pets.add("bunny");
int i = 0;
while(pets.get(i).length() < 4) {
pets.remove(i);
i++;
}
[ ]
[ fish , bunny]
[dog, fish, rat, bunny]
[dog, fish, bunny]
[dog, cow, fish]
public static int search(int[ ] a, int target) {
int result = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] == target)
result = i;
}
return result;
}
Returns the index of the first occurrence of the target in a, or -1 if not found
Returns the index of the last occurrence of the target in a, or -1 if not found
Returns the target value if it is found, -1 otherwise
Always returns -1
public static void sort(int[ ] a) {
for (int j = 1; j < a.length; j++) {
int temp = a[j];
int loc = j;
while (loc > 0 && temp < a[loc - 1]) {
a[loc] = a[loc - 1];
loc--; // line 7
}
a[loc] = temp;
}}
int [ ] arr = {7, 9, 1, 3, 12}
How many times does line 7 execute?
1
2
3
4
5
public static void sort(int[] a) {
for (int j = 0; j < a.length - 1; j++) {
int loc = j;
for (int k = j + 1; k < a.length; k++) {
if (a[k] < a[loc])
loc = k;
}
int temp = a[j];
a[j] = a[loc];
a[minIndex] = temp;
}}
int arr = { 30, 40, 10, 50, 20};
What are the values in the array after 2 iterations of the outer loop?
{10, 20, 30, 40, 50}
{10, 30, 40, 50, 20}
{10, 40, 30, 50, 20}
{40, 30, 10, 50, 20}
