Arraylists Quiz
star
star
star
star
star
Last updated over 1 year ago
22 questions

1
What is output by the code below?
ArrayList<String> list = new ArrayList<String>();
list.add("three");
list.remove(0);
list.add("one");
System.out.println(list);
What is output by the code below?
ArrayList<String> list = new ArrayList<String>();
list.add("three");
list.remove(0);
list.add("one");
System.out.println(list);
1
What is output by the code below?
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.set(0, "two");
list.add(0, "three");
System.out.println(list);
What is output by the code below?
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.set(0, "two");
list.add(0, "three");
System.out.println(list);

1
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.remove(0);
list.set(1,"five");
System.out.println(list);
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.remove(0);
list.set(1,"five");
System.out.println(list);
1
What is output by the code below?
List<String> bigList = new ArrayList<String>();
bigList.add(0,"one");
bigList.add("two");
bigList.add(0,"three");
bigList.add("four");
bigList.remove(0);
bigList.add(0,"five");
bigList.set(2,"six");
bigList.set(3,"seven");
bigList.add("zero");
System.out.println(bigList.indexOf("six"));
What is output by the code below?
List<String> bigList = new ArrayList<String>();
bigList.add(0,"one");
bigList.add("two");
bigList.add(0,"three");
bigList.add("four");
bigList.remove(0);
bigList.add(0,"five");
bigList.set(2,"six");
bigList.set(3,"seven");
bigList.add("zero");
System.out.println(bigList.indexOf("six"));

1
Quiz: Which of the following would fill blank <*1> to add a new It to itListOne?
Quiz: Which of the following would fill blank <*1> to add a new It to itListOne?

1
What is the output by the code below?
java
public class It {
private int stuff;
private String word;
public It(int x, String w){
stuff = x;
word = w;
}
public int getStuff(){
return stuff;
}
public String toString(){
return "" + stuff + " " + word ;
}
}
//code in client class
ArrayList<It> x;
x = new ArrayList<It>();
x.add(new It(5, "dog"));
x.add(new It(3, "cat"));
System.out.println(x.get(0).getStuff());
What is the output by the code below?
java
public class It {
private int stuff;
private String word;
public It(int x, String w){
stuff = x;
word = w;
}
public int getStuff(){
return stuff;
}
public String toString(){
return "" + stuff + " " + word ;
}
}
//code in client class
ArrayList<It> x;
x = new ArrayList<It>();
x.add(new It(5, "dog"));
x.add(new It(3, "cat"));
System.out.println(x.get(0).getStuff());

1
Consider the following Dog class. Which of the following answer choices would create a List of Dog references?
Consider the following Dog class. Which of the following answer choices would create a List of Dog references?

1
Given the Dog and DogPound classes, which code could fill line 1 so that method printDogs would print all Dogs in the DogPound?
Given the Dog and DogPound classes, which code could fill line 1 so that method printDogs would print all Dogs in the DogPound?

1
// client code found in PigRunner.javaArrayList< Pig > list;list = new ArrayList< Pig >();< *1 >Which of the following could fill < *1 > to add a Pig to the list?I. list.add( 5 );II. list.add( 5, "piggy" );III. list.add( new Pig( 5, "piggy" ) ) ;
// client code found in PigRunner.java
ArrayList< Pig > list;
list = new ArrayList< Pig >();
< *1 >
Which of the following could fill < *1 > to add a Pig to the list?
I. list.add( 5 );
II. list.add( 5, "piggy" );
III. list.add( new Pig( 5, "piggy" ) ) ;

1
Consider the following Pig class and client code below.
class Pig
{
public Pig( int x, String y )
{
//code not shown
}
}
// client code found in PigRunner.java
ArrayList< Pig > list;
list = new ArrayList< Pig >();
// several Pigs have been added to the list
< *1 >
Which of the following could fill < *1 > to print out each Pig in the list on a separate line?
I.
for( Pig it : list )
System.out.println( it );
II.
for( Pig it : list )
System.out.println( list.get( it ) );
III.
for( Pig it : list )
System.out.println( it );
Consider the following Pig class and client code below.
class Pig
{
public Pig( int x, String y )
{
//code not shown
}
}
// client code found in PigRunner.java
ArrayList< Pig > list;
list = new ArrayList< Pig >();
// several Pigs have been added to the list
< *1 >
Which of the following could fill < *1 > to print out each Pig in the list on a separate line?
I.
for( Pig it : list )
System.out.println( it );
II.
for( Pig it : list )
System.out.println( list.get( it ) );
III.
for( Pig it : list )
System.out.println( it );

1
1
1
1
1
1
1

1
1
1
1
1