7.13.1. Easier Multiple Choice QuestionsΒΆ
These problems are easier than most of those that you will usually see on the AP CSA exam.
- nums.length
- You can't use length on lists and the last index is one less than the size.
- nums.length - 1
- You can't use length on lists, use size instead.
- nums.size()
- Since the first element in a list is at index 0 the last element is at the size minus 1.
- nums.size() - 1
- The last element is at the size of the list minus 1.
7-11-1-1: Which index is the last element in a list called nums
at?
- An array has faster access to its elements than a list does.
- Since an ArrayList is implemented by an array, it has the same access time.
- An array knows it length, but a list doesn't know its length.
- Lists do know their length, but they don't make it public.
- An ArrayList can allocate more space than it needs.
- Every time an ArrayList fills up a new array is created that is twice as big. This can lead to extra space that is wasted.
7-11-1-2: Which of the following is a reason to use an array instead of an ArrayList?
- An ArrayList can grow or shrink as needed, while an array is always the same size.
- This is the main advantage to an ArrayList.
- You can use a for-each loop on an ArrayList, but not in an array.
- You can use a for-each loop on either an ArrayList or array.
- You can store objects in an ArrayList, but not in an array.
- Arrays can also store objects of the same type.
7-11-1-3: Which of the following is a reason to use an ArrayList instead of an array?
- nums[0]
- This is how you get the first value in an array, but not in a list.
- nums[1]
- This is how you get the second value in an array. Remember that this is a list and that the first item in an array is at index 0.
- nums.first()
- The
List
doesn't have afirst
method. - nums.get(0)
- Use the
get
method to get a value from a list and the first element in a list is at index 0. - nums.get(1)
- This would return the second element in a list. Remember that the first element in a list or array is at index 0.
7-11-1-4: Which of the following is the correct way to get the first value in a list called nums
?
- nums[1] = 5;
- This is how you set the second value in an array, but not in a list.
- nums[2] = 5;
- This is how you set the third value in an array, but not in a list.
- nums.set(5, 1);
- This would the value at index 5 to 1.
- nums.set(1, 5);
- This sets the second value in the list to 5.
- nums.set(2, 5);
- This would set the third value in the list to 5. Remember that the first value is at index 0.
7-11-1-5: Which of the following is the correct way to set the second value in a list called nums
to 5?
- nums.remove(3);
- This would remove the value at index 3 which is 1.
- nums.remove(0);
- This would remove the value at index 0 which is 5.
- nums.remove(1);
- This would remove the value at index 1 which is 3.
- nums.remove(2);
- This would remove the value at index 2 which is 2.
7-11-1-6: Which of the following is the correct way to remove the value 3 from the list nums = [5, 3, 2, 1]
?
- nums.add(2, 0);
- This would add 0 at index 2. Remember that the method is
add(index, obj)
. - nums.add(2, 1);
- This would add 1 at index 2. Remember that the method is
add(index, obj)
- nums.add(0, 2);
- This would add 2 at index 0 which would result in
[2, 1, 3, 4]
- nums.add(1, 2);
- This would add 2 at index 1 which would result in
[1, 2, 3, 4]
- nums.add(2, 2);
- This would add 2 at index 2 which would result in
[1, 3, 2, 4]
7-11-1-7: Which of the following is the correct way to add 2 between the 1 and 3 in the following list nums = [1, 3, 4]
?
- [2, 3]
- This would be true if it was
remove(0)
- [1, 2, 3]
- The
remove
will remove a value from the list, so this can't be correct. - [1, 2]
- This would be true if it was
remove(2)
- [1, 3]
- This removes the value at index 1 which is 2.
7-11-1-8: What will print when the following code executes?
List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.remove(1);
System.out.println(list1);
You can step through the code above by clicking on the following Ex-8-11-9.
- ["Sarah", "Destini", "Layla", "Sharrie"]
- The list is first ["Anaya", "Layla", "Sharrie"] and then ["Destini, "Layla", "Sharrie"] and finally ["Sarah", "Destini, "Layla", "Sharrie"]
- ["Sarah", "Destini", "Anaya", "Layla", "Sharrie"]
- The set replaces the value at index 0.
- ["Sarah", "Layla", "Sharrie"]
- This would be true if the second add was a set.
- ["Destini", "Layla", "Sharrie", "Sarah"]
- This would be true if the last add didn't have an index of 0.
7-11-1-9: What will print when the following code executes?
List<String> list1 = new ArrayList<String>();
list1.add("Anaya");
list1.add("Layla");
list1.add("Sharrie");
list1.set(0, "Destini");
list1.add(0, "Sarah");
System.out.println(list1);
You can step through the code above by clicking on the following Ex-8-11-10.