created: Jan. 06, 2000; revised: 10/05/03, 06/05/06, 07/05/07, 11/02/2012


worm on ArrayLists and Iterators

Instructions: For each question, choose the single best answer. Make your choice by clicking on its button. You can change your answers at any time. When the quiz is graded, the correct answers will appear in the box after each question.


1. Declare and construct an ArrayList with an initial capacity of 20 references to Object.

A.    Object list(20) = new ArrayList() ;

B.    ArrayList list[20] = new ArrayList() ;

C.    ArrayList[Object] list = new ArrayList(20) ;

D.    ArrayList<Object> list = new ArrayList<Object>(20) ;


2. Examine the following code:

ArrayList<String> list = new ArrayList<String>(10) ;
list.add( "Ann" );
list.add( "Bob" );
list.add( "Eve" );

After the code has executed, what is the capacity of list? What is its size?

A.    3, 3

B.    3, 10

C.    10, 3

D.    10, 10


3. Examine the following code:

ArrayList<String> list = new ArrayList<String>(10) ;

list.add( "Andy" );
list.add( "Bart" );
list.add( "Carl" );
list.add( 0, "Eve" );

What element will be at index 2 of the list?

A.    Eve

B.    Andy

C.    Bart

D.    carl


4. Examine the following code:

ArrayList<String> list = new ArrayList<String>() ;

list.add( "Andy" );
list.add( "Bart" );
list.add( "Carl" );
list.add( "Doug" );
list.add( "Elmo" );

Which of the following will replace the element "Carl" with "Zoltan" ?

A.    list[2] = "Zoltan" ;

B.    list.set( "Zoltan", "Carl" );

C.    list.add( "Zoltan", list.indexOf("Carl") );

D.    list.set( list.indexOf("Carl"), "Zoltan" );


5. Examine the following code:

ArrayList<String> list = new ArrayList<String>() ;

list.add( "Andy" );
list.add( "Bart" );
list.add( "Carl" );
list.add( "Doug" );
list.add( "Elmo" );

Which of the following will change the list so that it looks like:

Andy
Bart
Carl
Doug
Oscar
Elmo

A.    list.add( 3, "Oscar" ) ;

B.    list.add( 4, "Oscar" ) ;

C.    list.set( 3, "Oscar" ) ;

D.    list.set( 4, "Oscar" ) ;


6. Examine the following code:

ArrayList<String> list = new ArrayList<String>() ;

list.add( "Andy" );
list.add( "Bart" );
list.add( "Carl" );
list.add( "Doug" );
list.add( "Elmo" );

Which of the following will change the list so that it looks like:

Andy
Bart
Doug
Elmo

A.    list.remove( 3 );

B.    list.remove( 2 );

C.    list.add( 2, null );

D.    list.set( list.indexOf( "Carl" ), "Doug" );


7. Examine the following code:

ArrayList<String> list = new ArrayList<String>() ;

list.add( "Andy" );
list.add( "Bart" );
list.add( "Carl" );
list.add( "Doug" );
list.add( "Elmo" );

Which of the following will change the list so that it looks like:

Andy
Bart
Carl
Doug

A.    list.remove( list.size() );

B.    list.remove( list.size()-1 );

C.    list.remove( 5 );

D.    list.clear( "Elmo" );


8. Examine the following code:

ArrayList<String> list = new ArrayList<String>() ;

list.add( "Andy" );
list.add( "Bart" );
list.add( "Carl" );
list.add( "Doug" );
list.add( "Elmo" );

Iterator iter = list.___________________;

while( iter.___________ )
  System.out.println( iter.___________ );

Fill in the blanks so that the list is printed.

A.    iterator()       hasNext()     next()

B.    iterator()       next()        nextElement()

C.    elements()       empty()       next()

D.    iterator()       more()        get()


9. Examine the following code:

ArrayList<String> list = new ArrayList<String>() ;

list.add( "Andy" );
list.add( "Bart" );
list.add( "Carl" );
list.add( "Doug" );
list.add( "Elmo" );

while( ________ name : ___________ )
  System.out.println( ___________ );

Fill in the blanks so that the list is printed.

A.    String       list        name

B.    String       iterator()    next()

C.    int          String         name

D.    iterator()    String        list


10. which of the following declarations would be appropriate for a list that is expected to contain integers?

A.    ArrayList<String> list = new ArrayList<String>() ;

B.    ArrayList<int> list = new ArrayList<int>() ;

C.    ArrayList<Integer> list = new ArrayList<Integer>() ;

D.    ArrayList list = new ArrayList() ;


11. What is the interface implemented by a class (such as ArrayList) that implements the iterator() method?

A.    Iterator<E>

B.    Iterable


12. What is the interface implemented by an iterator object itself?

A.    Iterator<E>

B.    Iterable


13. What is true of classes that can be used with the enhanced for loop?

A.    All classes can use the enhanced for loop.

B.    Only ArrayList can use the enhanced for loop.

C.    Only ArrayList and arrays can use the enhanced for loop.

D.    Any class that implements Iterable can use the enhanced for loop.


14. What equals() method must you override when defining a class of objects you expect to hold in a ArrayList ?

A.    public boolean equals( Object )

B.    public boolean equals( ArrayList )

C.    boolean equals( Object )

D.    private int equals( Object )


The number you got right:       Percent Correct:       Letter Grade:   


Click here If you have returned here from another page, or have re-loaded this page, you will need to click again on each of your choices for the grading program to work correctly. You may want to press the SHIFT KEY while clicking to clear the old answers.