Name: 
 

ArrayList  Review



Multiple Choice
Identify the choice that best completes the statement or answers the question.
 

 1. 

What is output by the code segment below?

ArrayList<Integer> b = new ArrayList<Integer>();     
b.add(5);
b.add(3);
b.add(0, 7);
b.add(0, 8);
b.add(6);
System.out.println( b );
a.
[5, 3, 7, 8, 6]
b.
[8, 7, 5, 3, 6]
c.
[5, 3, 6, 7, 8]
d.
[3, 7, 5, 8, 6]
e.
[7, 8, 3, 5, 6]
 

 2. 

What is output by the code segment below?

ArrayList<Integer> b = new ArrayList<Integer>();     
b.add(5);
b.add(3);
b.add(0, 7);
b.add(0, 8);
b.add(6);
b.set(0, 2);
b.set(2, 4);
System.out.println( b );
a.
[7, 2, 3, 4, 6]
b.
[2, 7, 5, 3, 6]
c.
[2, 7, 5, 3, 8]
d.
[2, 7, 4, 3, 6]
e.
[7, 8, 3, 5, 6]
 

 3. 

What is output by the code segment below?

ArrayList<Integer> b = new ArrayList<Integer>();     
b.add(5);
b.add(3);
b.add(0, 7);
b.add(0, 8);
b.add(6);
b.set(0, 2);
b.set(2, 4);
b.remove( 2 );
b.remove( 1 );
System.out.println( b );
a.
[3, 4, 6]
b.
[5, 3, 6]
c.
[2, 3, 6]
d.
[4, 3, 6]
e.
[7, 3, 6]
 

 4. 

What is output by the code segment below?

ArrayList<Integer> b = new ArrayList<Integer>();     
b.add(5);
b.add(3);
b.add(0, 7);
b.remove( 2 );
b.add(0, 8);
b.add(6);
b.set(0, 2);
b.remove( 1 );
b.set(2, 4);
System.out.println( b );
a.
[2, 5, 6]
b.
[8, 5, 4]
c.
[2, 3, 4]
d.
[2, 5, 4]
e.
[2, 7, 6]
 

 5. 

What is output by the code segment below?

ArrayList<Integer> b = new ArrayList<Integer>();
b.add(5);
b.add(3);
b.add(0, 7);
b.add(0, 8);
b.remove( 2 );
b.add(6);
b.remove( 1 );
b.set(0, 2);
b.set(2, 4);
System.out.println( b );
a.
[2, 5, 6]
b.
[8, 5, 4]
c.
[2, 3, 4]
d.
[2, 5, 4]
e.
[2, 7, 6]
 

 6. 

Which of the following ArrayList methods will change the value of a location in an ArrayList?
a.
add()
b.
remove()
c.
set()
d.
get()
e.
clear()
 

 7. 

Which of the following ArrayList methods will insert a new value at a location in an ArrayList?
a.
add()
b.
remove()
c.
set()
d.
get()
e.
clear()
 

 8. 

Which of the following ArrayList instantiations would create an ArrayList that could store doubles?
a.
ArrayList<Double> list = new ArrayList<Double>();
b.
ArrayList<Integer> list= new ArrayList<Integer>();
c.
ArrayList<String> list= new ArrayList<String>();
d.
ArrayList<Boolean> list= new ArrayList<Boolean>();
e.
ArrayList<Byte> list= new ArrayList<Byte>();
 

 9. 

Which of the following ArrayList instantiations would create an ArrayList that could store Strings?
a.
ArrayList<Double> list = new ArrayList<Double>();
b.
ArrayList<Integer> list= new ArrayList<Integer>();
c.
ArrayList<String> list= new ArrayList<String>();
d.
ArrayList<Boolean> list= new ArrayList<Boolean>();
e.
ArrayList<Byte> list= new ArrayList<Byte>();
 

 10. 

Which of the following ArrayList instantiations would create an ArrayList that could store booleans?
a.
ArrayList<Double> list = new ArrayList<Double>();
b.
ArrayList<Integer> list= new ArrayList<Integer>();
c.
ArrayList<String> list= new ArrayList<String>();
d.
ArrayList<Boolean> list= new ArrayList<Boolean>();
e.
ArrayList<Byte> list= new ArrayList<Byte>();
 

 11. 

Which of the following ArrayList methods returns the number of elements currently stored in the ArrayList?
a.
count()
b.
length
c.
length()
d.
size()
e.
clear()
 

 12. 

Which of the following ArrayList methods returns the element at a specified index position?
a.
get()
b.
contains()
c.
indexOf()
d.
set()
e.
clear()
 

 13. 

What is output by the code below?

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(8);
out.println(list);
a.
[5, 8]
b.
[8, 5]
c.
5 8
d.
8 5
e.
13
 

 14. 

What is output by the code below?

ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
System.out.println(list);
a.
[one, two, three]
b.
[three, two, one]
c.
[one]
d.
[three]
e.
one,two, three
 

 15. 

What is output by the code below?

List<String> list = new ArrayList<String>();
list.add(0,"one");
list.add(0,"two");
list.add(0,"three");
System.out.println(list);
a.
[one, two, three]
b.
[three, two, one]
c.
[one]
d.
[three]
e.
three, two, one
 

 16. 

What is output by the code below?

ArrayList<Double> list = new ArrayList<Double>();
list.add(2.5);
list.add(5.3);
list.add(9.0);
System.out.println(list);
a.
[2.5]
b.
[9.0, 5.3, 2.5]
c.
[9.0]
d.
[2.5, 5.3, 9.0]
e.
9.0
 

 17. 

What is output by the code below?

List<String> list = new ArrayList<String>();
list.add("three");
list.add("two");
list.add("one");
System.out.println(list);
a.
[one, two, three]
b.
[three, two, one]
c.
[one]
d.
[three]
e.
two, one
 

 18. 

What is output by the code below?

List<String> list = new ArrayList<String>();
list.add("one");
list.add(0,"two");
list.set(0,"three");
list.add(1,"four");
list.set(2,"five");
System.out.println(list);
a.
[one, two, three]
b.
[three, four, five]
c.
[one, two, five]
d.
[one, two, four]
e.
[three, two, five]
 

 19. 

What is output by the code below?

List<String> list = new ArrayList<String>();
list.add("one");
list.set(4,"two");
list.add("three");
list.add(0,"four");
System.out.println(list);
a.
[four, one, three, two]
b.
[three, two, one]
c.
[one, two, three, four]
d.
[two, three, four]
e.
There is no output due to a runtime error.
 

 20. 

What is output by the code below?

ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
for(String s : list)
   out.print(s + " ");
out.println();
a.
one two three
b.
three two one
c.
three three three
d.
one one one
e.
There is no output due to a runtime error.
 

 21. 

What is output by the code below?

List list = new ArrayList();
list.add("one");
list.add("two");
list.add("three");
for(int i=list.size()-1; i>=0; i--)
   out.print(list.get(i) + " ");
out.println();
a.
one two three
b.
three two one
c.
one
d.
three
e.
There is no output due to a runtime error.
 

 22. 

What is output by the code below?

ArrayList objList = new ArrayList();
objList.add("one");
objList.add("two");
objList.add("three");
for(int i=objList.size(); i>=0; i--)
   out.print(objList.get(i) + " ");
out.println();
a.
one two three
b.
three two one
c.
one
d.
three
e.
There is no output due to a runtime error.
 

 23. 

What is output by the code below?

ArrayList funList = new ArrayList();
funList.add(0,"one");
funList.add("two");
funList.add(0,"three");
funList.add("four");
funList.add(0,"five");
System.out.println( funList.indexOf("four") );
a.
2
b.
1
c.
0
d.
4
e.
5
 

 24. 

What is output by the code below?

List<String> badList = new ArrayList<String>();
badList.add(0,"one");
badList.add("two");
badList.set(0,"three");
badList.add(1,"four");
badList.set(1,"five");
out.println( badList.get(0) );
a.
one
b.
two
c.
three
d.
four
e.
five
 

 25. 

Which of the following would fill blank <*1> to add a new   It   to itListOne?
public class It
{
   private int stuff;

   public It(int x){
      stuff=x;
   }

   public String toString(){
      return ""+stuff;
   }
}

//code in client class
ArrayList<It> itListOne;
itListOne = new ArrayList<It>();
<*1>
a.
itListOne.add(new It(56));
b.
itListOne.add(new It());
c.
itListOne.add(new 56);
d.
itListOne.add(56);
e.
A and B only
 

 26. 

Consider the following instance variable and incomplete method. 
The method sum should sum up all of the values in    list.

private List<Double> list;  //assume the list contains values

public double sum()
{
  double sum = 0;

 
/* code */

  return sum;
}

Which of the following code segments shown below could be used to replace /* code */ so that   sum  will work as intended?

I.     for ( int i = 0;  i  <  list.size();  i++)
          sum = sum + list.get(i);

II.   for ( int i = list.size()-1;  i  > -1;  i--)
          sum = sum + list.get(i);

III.  for ( int i = 0;  i  <  list.size();  i++)
          sum = sum + i;
a.
I only
b.
II only
c.
III only
d.
I and III only
e.
I and II only
 

 27. 

Consider the following instance variable and incomplete method. 
The method sum should sum up every other value in the    list.

private List<Double> list;    //assume the listcontains values

public double sumEveryOther()
{
  double sum = 0;

 
/* code */

  return sum;
}

Which of the following code segments shown below could be used to replace    /* code */   so  that   sumEveryOther   will sum up every other value as intended?

I.     for ( int i = 0;  i  <  list.size()/2;  i++)
          sum = sum + list.get(i);

II.   for ( int i = list.size()-1;  i  > -1;  i = i - 2)
          sum = sum + list.get(i);

III.  for ( int i = 0;  i  <  list.size();  i = i + 2)
          sum = sum + list.get(i);
a.
I only
b.
II and III only
c.
III only
d.
II and IV only
e.
I, II, and III only
 

 28. 

Which of the following could fill   /* code */  that a call of go(10)  would return [2, 4, 6, 8, 10]?

public static ArrayList<Integer> go(int big)
{
  
/* code */
}

I.
ArrayList<Integer> list;
list = new ArrayList<Integer>();
for(int i=2; i < big; i+=2)
   list.add( i );
return list;

II.
ArrayList<Integer> list;
list = new ArrayList<Integer>();
for(int i=2; i <= big; i+=2)
   list.add( i );
return list;

III.
ArrayList<Integer> list;
list = new ArrayList<Integer>();
for(int i=2; i <= big; i+=2)
   list.add( 0, i );
return list;
a.
I only
b.
II only
c.
III only
d.
I and II only
e.
I and III only
 

 29. 

Which of the following could fill   /* code */  that a call of go(25)  would return [25, 20, 15, 10, 5]?

public static ArrayList<Integer> go(int big)
{
  
/* code */
}

I.
ArrayList<Integer> list;
list = new ArrayList<Integer>();
for(int i=5; i <= big; i++)
   list.add( i );
return list;

II.
ArrayList<Integer> list;
list = new ArrayList<Integer>();
for(int i=5; i < big; i+=5)
   list.add( i );
return list;

III.
ArrayList<Integer> list;
list = new ArrayList<Integer>();
for(int i=5; i <= big; i+=5)
   list.add( 0, i );
return list;
a.
I only
b.
II only
c.
III only
d.
I and II only
e.
I and III only
 

 30. 

What is output by the code segment below?

ArrayList<Integer> b = new ArrayList<Integer>();
int x = 1;         
b.add(5);
b.add(3);
b.add(x);
b.set(1, 2);
b.add(new Integer(4));
System.out.println(b.size());
a.
3
b.
5
c.
4
d.
2
e.
6
 

 31. 

What is output by the code segment below?

ArrayList<Integer> a = new ArrayList<Integer>();
int x = 1;         
a.add(x);
a.add(3);
a.add(5);
System.out.println(a.remove(1));
a.
2
b.
6
c.
4
d.
3
e.
5
 

 32. 

What is output by the code segment below?

ArrayList<Integer> a = new ArrayList<Integer>();
int x = 1;         
a.add(x);
a.add(3);
a.add(5);
a.remove(1);
System.out.println(a.remove(x));
a.
2
b.
6
c.
4
d.
3
e.
5
 

 33. 

What is output by the code segment below?

ArrayList<Integer> b = new ArrayList<Integer>();
b.add(5);
b.add(3);
b.add(0, 7);
b.add(0, 8);
b.remove( 2 );
b.add(6);
b.remove( new Integer(1) );
b.set(0, 2);
b.set(2, 4);
System.out.println( b );
a.
[2, 7, 5, 6]
b.
[8, 5, 4]
c.
[2, 3, 4]
d.
[2, 5, 4, 6]
e.
[2, 7, 4, 6]
 



 
Check Your Work     Start Over