Skip to main content

Section 5.1 Check Your Understanding

Exercises Exercises

1. True/False.

    ArrayLists can store objects and primitives such as int, double, boolean.
  • True.

  • ArrayLists cannot store the following: int, double, boolean, char, float, long, byte, short. A workaround is to use Wrapper classes.
  • False.

  • ArrayLists cannot store the following: int, double, boolean, char, float, long, byte, short. A workaround is to use Wrapper classes.

2. Accessing Elements in an ArrayList.

    How would we access the first element in the ArrayList list?
  • list.item(0)
  • Incorrect: ArrayList does not have a .item() method.
  • list.get(0)
  • Correct: In ArrayLists, we use the .get(i) method to access the element at index i.
  • list[0]
  • Incorrect: Square brackets are used for accessing elements in an array, not an ArrayList.
  • list.set(0, element)
  • Incorrect: The .set(index, element) method is used to replace the element at the specified index with the specified element. It is not used for accessing elements.

3. Select all that apply.

    Which of the following are true about ArrayLists? (Select all that apply)
  • ArrayLists have a dynamic size.
  • Correct: Unlike partially-filled arrays, ArrayLists automatically resize themselves when an element is added or removed.
  • ArrayLists have a fixed size.
  • Incorrect: ArrayLists resize automatically when an element is added or removed while partially-filled arrays require manual resizing if you have reached capacity and are trying to add an element.
  • ArrayLists use the .length() method to get the number of elements in the list.
  • Incorrect: ArrayLists use .size() while arrays use .length().
  • ArrayLists cannot be instantiated without importing from the Java Library.
  • Correct: To use ArrayLists, you must import the java.util.ArrayList package first.
You have attempted of activities on this page.