9.18. Easy Multiple Choice Questions¶

These problems are easier than most of those that you will usually see on the AP CS A exam.

8-11-1: Which index is the last element in a list called `nums` at?
• 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.
8-11-2: Which of the following is a reason to use an array instead of an ArrayList?
• 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.
8-11-3: 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.
• 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.
8-11-4: Which of the following is the correct way to get the first value in a list called `nums`?
• nums
• This is how you get the first value in an array, but not in a list.
• nums
• 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` interface doesn't have a `first` 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.
8-11-5: Which of the following is the correct way to set the second value in a list called `nums` to 5?
• nums = 5;
• This is how you set the second value in an array, but not in a list.
• nums = 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.
8-11-6: Which of the following is the correct way to remove the value 3 from the list `nums = [5, 3, 2, 1]`?
• 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.
8-11-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]`?
• This would add 0 at index 2. Remember that the method is `add(index, obj)`.
• This would add 1 at index 2. Remember that the method is `add(index, obj)`
• This would add 2 at index 0 which would result in `[2, 1, 3, 4]`
• This would add 2 at index 1 which would result in `[1, 2, 3, 4]`
• This would add 2 at index 2 which would result in `[1, 3, 2, 4]`
8-11-8: Which of the following is false about an interface?
• It is a type of class.
• An interface is a special type of abstract class in Java.
• The methods in an interface will be public and abstract.
• The methods defined in an interface are public and abstract.
• It is like a contract in that the class that implements the interface must provide the methods defined in the interface.
• An interface is like a contract for the implementing classes.
• You can create an object of an interface type.
• You can not create an object of an interface type. This is why you create a ``List`` using the `ArrayList` class which implements the ``List`` interface.

8-11-9: What will print when the following code executes?

```List<Integer> list1 = new ArrayList<Integer>();
list1.remove(1);
System.out.println(list1);
```
• [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.

You can step through the code above by clicking on the following Ex-8-11-9.

8-11-10: What will print when the following code executes?

```List<String> list1 = new ArrayList<String>();
list1.set(0, "Destini");
System.out.println(list1);
```
• ["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.

You can step through the code above by clicking on the following Ex-8-11-10.

Next Section - 9.19. Medium Multiple Choice Questions