` Lecture 2

CS 151 - Lecture 10

Cover page image

Cay S. Horstmann

Lecture 10 Clicker Question 1

Go to Piazza and answer the question.

Consider this method:

public static String largest(String[] values, Comparator<String> comp)
{
   String result = null;
   for (int i = 0; i < values.length - 1; i++)
      if (i == 0 || comp.compare(result, values[i]) < 0)
         result = values[i];
   return result;
}

What is the result of this call?

String[] words = { "Mary", "had", "a", "little", "zebra" };
String largestWord = largest(words, (s, t) -> Math.max(s.length(), t.length()));
  1. "Mary"
  2. "little"
  3. "zebra"
  4. I have no idea.

Lecture 10 Clicker Question 2

With the same largest method, what is the result of

String result = largest(new String[] { "Mary" }, null);
  1. A compiler error
  2. A NullPointerException
  3. An ArrayIndexOutOfBoundsException
  4. "Mary"