Arraylists Quiz

By Mickey Arnold
Last updated 10 months ago
22 Questions

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("one"); list.set(0, "two"); list.add(0, "three"); 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);

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"));

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

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());

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

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

// 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" ) ) ;

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 );