Lists - Computer Science

Card 0 of 6

Question

Consider the code below:

String\[\] db = {"Harvey","Plutarch","Frege","Radulphus"};

ArrayList names = new ArrayList();

for(int i = 0; i < 12; i++) {

names.add(db\[i % db.length\]);

}

for(int i = 0; i < 12; i++) {

if(names.get(i).equals("Frege")) {

names.remove(i);

}

}

What is the bug in the code above?

Answer

In the second loop, when you remove one of the items, you thus make the ArrayList one element shorter. This means that if you attempt to go through to index 11, you will receive an error at some point, for the list will have "shrunk". This is like an array out of bounds error, though it will be an IndexOutOfBoundsException exception.

Compare your answer with the correct one above

Question

Consider the following code:

public static class Circle {

private double radius;

public Circle(double r) {

radius = r;

}

public double getArea() {

return radius * radius * Math.PI;

}

}

public static void main(String\[\] args) {

ArrayList circles = new ArrayList();

for(int i = 0; i < 10; i++) {

circles.add(new Circle(i + 4 * 2));

}

}

Which of the following represents code for iterating through the list circles in order to output the areas of the circles contained therein?

Answer

In the answers, there are only two issues to consider. On the one hand, look at the loop control statement. It is some variant on:

circle.size()

and

circles.length

For the List types, you need to use the method size(), not length (which you use for arrays). Likewise, one way to extract elements from the ArrayList type is to use the .get method. You must use this and not the brackets \[\] that you use for arrays.

Compare your answer with the correct one above

Question

Consider the code below:

ArrayList myPhilosophers = new ArrayList();

myPhilosophers.add("Frege");

myPhilosophers.add("Husserl");

myPhilosophers.add("Hegel");

myPhilosophers.add("Bill");

myPhilosophers.add("Frederick");

for(int i = 0; i < myPhilosophers.size(); i++) {

String s = myPhilosophers.get(i);

if(s.charAt(0) >= 'H' || s.charAt(3) < 'd') {

myPhilosophers.set(i, s.toUpperCase());

}

}

System.out.println(myPhilosophers);

What is the output for the code above?

Answer

Consider the logic that is in the loop. The if statement will be reached either if you:

  1. Have a first character that is H or later in the alphabet (in capital letters).
  2. Have a fourth character that is less than d.

The first case applies to "Husserl" and "Hegel." However, the second does not apply to any—not even to "Frederick", for d is equal to d, not less than it!

Compare your answer with the correct one above

Question

Which of the following is NOT a valid declaration for an ArrayList?

Answer

ArrayLists are lists of objects, and thus cannot store primitive types. If you wish to store primitive types in an ArrayList, container objects such as Double or Integer must instead be used. The object reference type List is a valid reference type for an ArrayList object.

Compare your answer with the correct one above

Question

Which of the following is NOT a difference between the Array class and the ArrayList class in Java?

Answer

In Java, Array is a fixed length data structure, while ArrayList is a variable length Collection Class (other Collection class members include HashMap and HashSet). This means that an Array cannot change its size once it's made; a whole new Array must be made. ArrayLists can change size at will.

Arrays can reference both primitives (like int) and types (like Integer). ArrayLists can only reference types, not primitives. Through a method called "Autoboxing", it can appear that an ArrayList stores a primitive, but what it really is doing is automatically masking the primitive with the type it's like so that it works with the ArrayList.

During the creation of an Array, the .length value is assigned, so whenever you append .length to the end of an array, you get the length (which will never change for the given Array). For ArrayLists, you use the .size() function, which returns the length of the ArrayList. Because the ArrayList size is variable, it is a function (which is why the parenthesis are there at the end).

Java provides the add() function for ArrayLists, which, as the name implies, adds the argument to the ArrayList. Arrays, on the other hand, do not have functions to add elements. To add something to an Array, you'd call something similar to this:

int[] my_array = new int[10];

my_array[0] = 5;

Compare your answer with the correct one above

Question

Write a program that iterates through this data structure and prints the data (choose the best answer):

List<List<String>> listOflistOfStrings = new ArrayList<ArrayList<String>>();

Answer

The correct answer uses a ForEach loop. A ForEach loop is recommended for iterating through Lists because Lists contain iterators. ForEach loops use the iterator to iterate through the List. One of the answers used a regular For loop, while the answer was correct, it was not the best choice.

Compare your answer with the correct one above

Tap the card to reveal the answer