1.
The E get(int index) method of the List interface should throw an exception if:
Correct Answer
C. The index passed to it is negative or greater or equal to the size of the list
Explanation
The correct answer is "the index passed to it is negative or greater or equal to the size of the list". This is because the get() method should throw an exception if the index is either negative or greater than or equal to the size of the list. In both cases, the index is out of bounds and accessing an element at that index would result in an error.
2.
The difference between an array and an ArrayList is:
Correct Answer
C. C. arrays have a fixed size and cannot grow to accommodate more elements
Explanation
The correct answer is c. arrays have a fixed size and cannot grow to accommodate more elements. This statement accurately describes the difference between an array and an ArrayList. Arrays have a fixed size that is determined when they are created, and they cannot grow or shrink dynamically. In contrast, an ArrayList is a resizable collection that can grow or shrink as needed to accommodate more or fewer elements. Therefore, the statement accurately highlights the key difference between these two data structures.
3.
The position of an item within a list is called its:
Correct Answer
A. A. index
Explanation
The position of an item within a list is commonly referred to as its "index". The index represents the specific location or order of an item in the list, allowing for easy reference and retrieval of the item when needed. It is a fundamental concept in programming and data structures, as it enables efficient manipulation and access to elements within a list.
4.
A constructor for an array-based list takes an integer parameter, to be used as the capacity of the internal array of the list. Which exception should the constructor throw if its parameter is zero or negative?
Correct Answer
A. IllegalArgumentException
Explanation
The constructor for an array-based list should throw an IllegalArgumentException if its parameter is zero or negative. This is because the capacity of the internal array should be a positive value, and passing a zero or negative value would be considered an invalid argument. The IllegalArgumentException is the appropriate exception to indicate that the input parameter is not valid.
5.
The size of an array-based list such as ArrayList.
Correct Answer
B. Is the number of elements that are currently stored in the list
Explanation
The size of an array-based list such as ArrayList refers to the number of elements that are currently stored in the list. This is because the size of the list is determined by the number of elements that have been added to it, rather than the amount of memory it can hold or the length of its internal array.
6.
The capacity of an array-based list such as ArrayList.
Correct Answer
C. is the size of its internal array
Explanation
The capacity of an array-based list such as ArrayList refers to the size of its internal array. This means that it determines the maximum number of elements that the list can hold. It is not related to the number of bytes of memory or the number of elements currently stored in the list.
7.
An ArrayList is so-called because:
Correct Answer
B. it is implemented as a class that uses an internal array to hold the elements of the list
Explanation
The correct answer is that an ArrayList is implemented as a class that uses an internal array to hold the elements of the list. This means that the ArrayList class internally manages an array to store and manipulate the elements in the list. Other options mentioned, such as using array subscript notation or passing it as a parameter to any method that expects an array, are not specific to ArrayList and can also be applicable to other types of arrays.
8.
A new element is added to an ArrayList object at index k. Assuming the list has size s and does not have to be resized.
Correct Answer
B. the elements at current positions k..s-1 must be moved toward the end of the array
Explanation
When a new element is added to an ArrayList at index k, all the elements at positions k to s-1 (where s is the size of the list) must be moved toward the end of the array to make space for the new element. This is because ArrayLists are dynamically resizable arrays, and when a new element is added, the array needs to accommodate it by shifting the existing elements to make room. The elements at positions 0 to k-1 do not need to be moved as they are not affected by the insertion. The element at position k is not overwritten, but rather shifted along with the other elements.
9.
The boolean contains(E element) method searches an ArrayList for a given element. Correct and efficient implementation of this method.
Correct Answer
C. Uses sequential search to locate the element
Explanation
The correct answer is that the method uses sequential search to locate the element. Sequential search is a simple and straightforward approach where the method iterates through the ArrayList one element at a time until it finds a match for the given element or reaches the end of the list. This method is efficient for small lists or when the element being searched for is likely to be near the beginning of the list. However, for large lists or when the element is likely to be towards the end of the list, binary search would be a more efficient approach.
10.
If a new element is added to an ArrayList whose internal array if already full:
Correct Answer
B. A new, bigger internal array is created, the elements are moved to the new array, the old internal array is deleted, and then the new element is added
Explanation
When a new element is added to an ArrayList whose internal array is already full, the ArrayList creates a new, bigger internal array. Then, it moves all the elements from the old array to the new array. After that, the old internal array is deleted. Finally, the new element is added to the ArrayList.