Preview

17 - ArrayList Methods

 1. The following statement would:
obj.add("hello");

  add a string hello in the arraylist at last position.

  add a string hello in the arraylist in the last position - 1 (second last)

  add a string hello in the arraylist in the first position

  add an object hello in the arraylist in the first position

 2. Which of the following would you add to line 5 in order to add a Honda to the list?
import java.util.ArrayList;
public class MyClass { 
  public static void main(String[] args) { 
    ArrayList cars = new ArrayList();
    ?????
    System.out.println(cars);
  } 
}

  insert.cars("Honda");

  cars.add("Honda");

  cars.insert("Honda");

  add.cars("Honda");

 3. Which of the following array list methods returns the number of elements in the list?

  E remove(int index)

  void add(int index, E obj)

  int size()

  boolean add(E obj)

 4. Two methods (including the get method) are being used in the code below. The output of the code in this case is: Honda
import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList cars = new ArrayList();
    cars.add("Honda");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    System.out.println(cars.get(0));
  } 
}

  FALSE

  TRUE

 5. Which of the following array list methods removes the item at the index and shifts remaining items to the left (to a lower index)?

  E remove(int index)

  int size()

  boolean add(E obj)

  void add(int index, E obj)

 6. void add(int index, E obj) moves any current objects at index or beyond to the ______________ and inserts obj at the index

  right (to a higher index)

  first index (replacing the index with the obj)

  left (to a lower index)

  next index (replacing the index with 0)

 7. E get(int index) returns the item in the list at the index

  FALSE

  TRUE

 8. E set(int index, E obj) replaces the item at index with the index itself

  TRUE

  FALSE

 9. To remove all the elements in the ArrayList, use the __________method:

  clear() method, e.g. cars.clear();

  clearall() method, e.g. cars.deleteall();

  delete() method,e.g. cars.delete();

  remove() method, e.g. cars.remove();

 10. To find out how many elements an ArrayList have, use the size method e.g:

  cars.size();

  cars(size);

  size.cars();

  size.cars(index);

 11. What will print when the following code executes?
ArrayList<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(2, 4);
list1.add(5);
System.out.println(list1);

   [1, 2, 4, 5]

   [1, 2, 3, 4, 5]

   [1, 2, 4, 3, 5]

  [1, 4, 2, 3, 5]

 12. Which of the following is an explanation for the above?

  The add(2,4) will put the 4 at index 2 and also move the 3 to index 1

  None of the options listed here explain it correctly

  The add(2,4)will put the 2 at index 4.

  The add(2, 4) will put the 4 at index 2, but first move the 3 to index 3.

 13. The following will print: ["Adam", "Luke", "Sharon", "Solomon"]
ArrayList list1 = new ArrayList();
list1.add("Adam");
list1.add("Luke");
list1.add("Sharon");
list1.add(1, "Solomon");
System.out.println(list1);

  TRUE

  FALSE

 14. In the following code, what will the set method do?
List list1 = new ArrayList();
list1.add(1);
list1.add(2);
list1.add(3);
list1.set(2, 4);
list1.add(2, 5);
list1.add(6);
System.out.println(list1);

  The set will replace the item at index 4

  The set will swap the items at index 2 and 4, if they are present

  None of the options listed here explain it correctly

  The set will replace the item at index 2

 15. The output of the above code is: [1, 2, 5, 4, 6]

  FALSE

  TRUE