ArrayList Methods - AP Computer Science A

  1. Identify the method that removes an element at a specified index in an ArrayList.
    remove(int index). Removes by index and returns the removed element.
  2. What exception does the set method throw if the index is out of range?
    IndexOutOfBoundsException. Thrown when index < 0 or >= size().
  3. Identify the method that removes the first occurrence of a specified element in an ArrayList.
    remove(Object o). Searches and removes by value, not index.
  4. What does the ArrayList get method return?
    Returns the element at the specified index. Accesses elements without modifying the ArrayList.
  5. What is the return type of the ArrayList remove method?
    Returns the element that was removed. Returns the element that was at the index.
  6. What does the ArrayList hashCode method return?
    Returns hash code value for the list. Computed from elements for hashing collections.
  7. Identify the method to check if an ArrayList contains a specific element.
    contains(Object o). Returns true if element exists in the list.
  8. Identify the method that inserts all elements of a collection into an ArrayList at a specified position.
    addAll(int index, Collection<? extends E> c). Shifts existing elements to accommodate new ones.
  9. What is the consequence of modifying an ArrayList while iterating over it?
    Throws ConcurrentModificationException. Prevents corruption from simultaneous modifications.
  10. Find the method to clear all elements from an ArrayList.
    clear(). Removes all elements, making the list empty.
  11. Which method in ArrayList returns an iterator over the elements?
    iterator(). Enables traversal through enhanced for loops.
  12. Which method returns the index of the last occurrence of an element in an ArrayList?
    lastIndexOf(Object o). Searches backwards from the end of the list.
  13. Which method replaces an element at a specified position in an ArrayList?
    set(int index, E element). Replaces existing element and returns old value.
  14. What is the purpose of the toArray method in ArrayList?
    Converts the ArrayList to an array. Creates Object array containing list elements.
  15. What does the ArrayList indexOf method return?
    Returns index of first occurrence of an element. Returns -1 if element is not found.
  16. What does the toString method return for an ArrayList?
    String representation of the list. Shows elements in bracket-separated format.
  17. What does the subList method in ArrayList return?
    Returns a view of a portion of the list. Creates sublist that reflects changes to original.
  18. What is the purpose of the ArrayList add method?
    Adds an element to the ArrayList. Inserts elements at the end or specified position.
  19. Which method returns a shallow copy of an ArrayList?
    clone(). Creates independent copy with same elements.
  20. What is the boolean return type of the remove method used with an object in ArrayList?
    True if the element is successfully removed. Returns false if element was not found.
  21. What does the ArrayList isEmpty method check?
    Checks if the ArrayList is empty. Returns true if size equals zero.
  22. Find the method to add an element at a specific position in an ArrayList.
    add(int index, E element). Inserts element and shifts existing elements right.
  23. Identify the method to remove all elements from an ArrayList that are in a specified collection.
    removeAll(Collection<?> c). Removes elements that exist in both collections.
  24. Which method returns the number of elements in an ArrayList?
    size(). Returns the current number of stored elements.
  25. Which method sorts the elements of an ArrayList based on a specified comparator?
    sort(Comparator<? super E> c). Orders elements according to comparison logic.
  26. Find the method that checks for equality between an ArrayList and another object.
    equals(Object o). Compares element-by-element for structural equality.
  27. Which method retains only the elements in an ArrayList that are in a specified collection?
    retainAll(Collection<?> c). Keeps only common elements between collections.
  28. In which scenario does the ArrayList get method throw an exception?
    Throws IndexOutOfBoundsException if index is out of range. When index is negative or >= size().
  29. Which method in ArrayList appends all elements in a specified collection?
    addAll(Collection<? extends E> c). Adds all elements to the end sequentially.
  30. Find the method that checks if two ArrayLists are equal.
    equals(Object o). Compares element-by-element for structural equality.

Study these flashcards