You are on page 1of 6

ArrayLists

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 exectued, what is the capactiy 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(
list.add(
list.add(
list.add(

"Andy" );
"Bart" );
"Carl" );
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(
list.add(
list.add(
list.add(
list.add(

"Andy"
"Bart"
"Carl"
"Doug"
"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(
list.add(
list.add(
list.add(
list.add(

"Andy"
"Bart"
"Carl"
"Doug"
"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(
list.add(
list.add(
list.add(
list.add(

"Andy"
"Bart"
"Carl"
"Doug"
"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(
list.add(
list.add(
list.add(
list.add(

"Andy"
"Bart"
"Carl"
"Doug"
"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(
list.add(
list.add(
list.add(
list.add(

"Andy"
"Bart"
"Carl"
"Doug"
"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(
list.add(
list.add(
list.add(
list.add(

"Andy"
"Bart"
"Carl"
"Doug"
"Elmo"

);
);
);
);
);

while( ________ name : ___________ )


System.out.println( ___________ );

Fill in the blanks so that the list is printed.


A.

String

list

B.

String

iterator()

C.

int

String

D.

iterator()

String

name
next()
name
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 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 )

You might also like