Preview

18 - Traversing ArrayLists

 1. What is the output of the following code?
import java.util.ArrayList;

public class Main { 
  public static void main(String[] args) { 
    ArrayList cars = new ArrayList();
    cars.add("Honda");
    cars.add("Mercedes");
    cars.add("Ford");
    cars.add("Mazda");
    for (int i = 1; i < cars.size(); i++) {
      System.out.println(cars.get(i));
    }
  } 
}

  Mercedes,Ford

  Mercedes,Ford,Mazda

  Honda,Mercedes

  Honda,Mercedes,Ford,Mazda

 2. A for each loop could also be used to itereate. What is the correct syntax for the line with the question marks below?
import java.util.ArrayList;
public class MyClass { 
  public static void main(String[] args) { 
    ArrayList cars = new ArrayList();
    cars.add("Honda");
    cars.add("Mercedes");
    cars.add("Ford");
    cars.add("Mazda");
    ??????????????????????
      System.out.println(i);
    }
  } 
}

   for each (int i = 1; i < cars.size(); i++) {

   for each (String i = 1; i i++) {

  for each (String i : cars) {

  for (String i : cars) {

 3. The following code seeks to sort the numbers in ascending order. What is wrong with it? (refer to line 14)
import java.util.ArrayList;
import java.util.Collections;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<Integer> myNumbers = new ArrayList<Integer>();
    myNumbers.add(33);
    myNumbers.add(15);
    myNumbers.add(20);
    myNumbers.add(34);
    myNumbers.add(8);
    myNumbers.add(12);

    sort.Collections(myNumbers);

    for (int i : myNumbers) {
      System.out.println(i);
    }
  } 
}

  line 14 should be: Collections.sort(myNumbers);

  line 14 should be: sort.Collections(myNumbers);

  line 14 should be: Collections(sort);

  line 14 should be: Collections.get(sort);

 4. What should go on line 11 in order for the program to output Adam,Eve,Cain,Abel,Seth?
import java.util.*;  
public class HelloWorld{
  public static void main(String args[]){  
     ArrayList alist=new ArrayList();  
     alist.add("Adam");  
     alist.add("Eve");  
     alist.add("Cain");  
     alist.add("Abel"); 
     alist.add("Seth");
     //iterating ArrayList
     ?????????????????????
        System.out.println(str);  
     }  
}

  for each(int:alist)

  for(int:alist(index)

  for(String str:alist(i)

  for(String str:alist)

 5. The output of the following code is 0.
import java.util.*;  // import all classes in this package.
public class Test
{
    public static void main(String[] args)
    {
        ArrayList myList = new ArrayList();
        myList.add(50);
        myList.add(30);
        myList.add(20);
        int total = 0;
        for (Integer value: myList)
        {
            total = total + value;
        }
        System.out.println(total);
    }
}

  TRUE

  FALSE

 6. The output of the above code is 100.

  TRUE

  FALSE

 7. The output of the following code is:
import java.util.*;
public class TestForLoop
{
    public static void main(String[] args)
    {
        ArrayList<Integer> myList = new ArrayList<Integer>();
        myList.add(50);
        myList.add(30);
        myList.add(20);
        int total = 0;
        for (int i=0; i <= myList.size(); i++)
        {
            total = total + myList.get(i);
        }
        System.out.println(total);
    }
}

  50

  100

  Error

  200

 8. What will nums contain as a result of executing numQuest?
//Assume that nums has been created as an 
//ArrayList object and it initially contains 
//the following Integer values [0, 0, 4, 2, 5, 0, 3, 0].

ArrayList list1 = new ArrayList();
private ArrayList nums;

// precondition: nums.size() > 0;
// nums contains Integer objects
public void numQuest()
{
   int k = 0;
   Integer zero = new Integer(0);
   while (k < nums.size())
   {
      if (nums.get(k).equals(zero))
         nums.remove(k);
      k++;
   }
}

  [4, 2, 5, 3]

  [3, 5, 2, 4, 0, 0, 0, 0]

  [0, 4, 2, 5, 3]

  [0, 0, 0, 0, 4, 2, 5, 3]

 9. _____________________using an enhanced for loop can result in a ConcurrentModificationException being thrown.

  Adding or deleting an element from an ArrayList while static

  Changing the value of the index while

  Changing the size of an ArrayList while traversing it

  Changing the size of an ArrayList in the code

 10. When using an enhanced for loop to traverse an ArrayList, it is possible to easily add or remove elements.

  FALSE

  TRUE