Chapter 5
Working with Arrays and Collections

  1. What is the output of the following?
    List<String> museums = new ArrayList<>(1);
    museums.add("Natural History");
    museums.add("Science");
    museums.add("Art");
    museums.remove(2);
    System.out.println(museums);
    
    1. [Natural History, Science]
    2. [Natural History, Art, Science]
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  2. How many of the following are legal declarations?
    []String lions = new String[];
    String[] tigers = new String[1] {"tiger"};
    String bears[] = new String[] {};
    String ohMy [] = new String[0] {};
    
    1. None
    2. One
    3. Two
    4. Three
    5. Four
  3. Which of the following can fill in the blank to make the code compile?
    public class News<__________> {}
     
    1. ? only
    2. N only
    3. ? and N
    4. News, and Object
    5. N, News, and Object
    6. None of the above
  4. Which of the following are true about this code? (Choose two.)
    26: List<String> strings = new ArrayList<?>();
    27: var ints = new HashSet<Integer>();
    28: Double dbl = 5.0;
    29: ints.add(2);
    30: ints.add(null);
    
    1. The code compiles as is.
    2. One line needs to be removed for the code to compile.
    3. Two lines need to be removed for the code to compile.
    4. One line of code uses autoboxing.
    5. Two lines of code use autoboxing
    6. Three lines of code use autoboxing
  5. Which of the following creates an empty two-dimensional array with dimensions 2×2?
    1. int[][] blue = new int[2, 2];
    2. int[][] blue = new int[2], [2];
    3. int[][] blue = new int[2][2];
    4. int[][] blue = new int[2 x 2];
    5. None of the above
  6. What is the output of the following?
    var q = new ArrayDeque<String>();
    q.offer("snowball");
    q.offer("minnie");
    q.offer("sugar");
                  
    System.out.print(q.peek() + " " + q.peek() + " " + q.size());
    
    1. sugar sugar 3
    2. sugar minnie 1
    3. snowball minnie 1
    4. snowball snowball 3
    5. The code does not compile.
    6. None of the above.
  7. You are running a library. Patrons select books by name. They get at the back of the checkout line. When they get to the front, they scan the book's ISBN, a unique identification number. The checkout system finds the book based on this number and marks the book as checked out. Of these choices, which data structures best represent the line to check out the book and the book lookup to mark it as checked out, respectively?
    1. ArrayList, HashSet
    2. ArrayList, TreeMap
    3. ArrayList, TreeSet
    4. LinkedList, HashSet
    5. LinkedList, TreeMap
    6. LinkedList, TreeSet
  8. What is the result of running the following program?
    1:  package fun;
    2:  public class Sudoku {
    3:     static int[][] game;
    4:
    5:     public static void main(String[] args) {
    6:        game[3][3] = 6;
    7:        Object[] obj = game;
    8:        game[3][3] = "X";
    9:        System.out.println(game[3][3]);
    10:    } }
    
    1. X
    2. The code does not compile.
    3. The code compiles but throws a NullPointerException at runtime.
    4. The code compiles but throws a different exception at runtime.
    5. None of the above.
  9. Suppose you want to implement a Comparator<String> so that it sorts the longest strings first. You may assume there are no null values. Which method could implement such a comparator?

    1. public int compare(String s1, String s2) {
         return s1.length() - s2.length();
      }
       

    2. public int compare(String s1, String s2) {
         return s2.length() - s1.length();
      }
       

    3. public int compare(Object obj1, Object obj2) {
         String s1 = (String) obj1;
         String s2 = (String) obj2;
         return s1.length() - s2.length();
      }
       

    4. public int compare(Object obj1, Object obj2) {
         String s1 = (String) obj1;
         String s2 = (String) obj2;
         return s2.length() - s1.length();
      }
       
    5. None of the above
  10. How many lines does the following code output?
    var days = new String[] { "Sunday", "Monday", "Tuesday",
       "Wednesday", "Thursday", "Friday", "Saturday" };
     
    for (int i = 1; i < days.length; i++)
          System.out.println(days[i]);
    
    1. Zero
    2. Six
    3. Seven
    4. The code does not compile.
    5. The code compiles but throws an exception at runtime.
  11. Which cannot fill in the blank for this code to compile and print true at runtime?
    var c = new _________________ <String>();
    c.add("pen");
    c.remove("pen");
    System.out.println(c.isEmpty());
     
    1. ArrayDeque
    2. ArrayList
    3. TreeMap
    4. TreeSet
    5. All of these can fill in the blank.
  12. What is true of the following code? (Choose two.)
    import java.util.*;
    public class Garden {
       private static void sortAndSearch(String x, String y) {
          var coll = Arrays.asList(x,y);
          Collections.sort(coll);
          _________ result = Collections.binarySearch(coll, x);
          System.out.println(result);
       }
       public static void main(String[] args) {
          sortAndSearch("seed", "flower");
       } }
     
    1. If the blank contains int, then the code outputs 0.
    2. If the blank contains int, then the code outputs 1.
    3. If the blank contains int, then the code does not compile.
    4. If the blank contains String, then the code outputs flower.
    5. If the blank contains String, then the code outputs seed.
    6. If the blank contains String, then the code does not compile.
  13. How many of the following are legal declarations?
    public void greek() {
       [][]String alpha;
       []String beta;
       String[][] gamma;
       String[] delta[];
       String epsilon[][];
       var[][] zeta;
    }
    
    1. One
    2. Two
    3. Three
    4. Four
    5. Five
    6. Six
  14. What is the result of the following?
    var list = new ArrayList<Integer>();
    list.add(56);
    list.add(56);
    list.add(3);
     
    var set = new TreeSet<Integer>(list);
    System.out.print(set.size());
    System.out.print(" ");
    System.out.print(set.iterator().next());
    
    1. 2 3
    2. 2 56
    3. 3 3
    4. 3 56
    5. None of the above
  15. What is true of this code when run as java Copier.java with no arguments? (Choose two.)
    1:  import java.util.Arrays;
    2:
    3:  public class Copier {
    4:     public static void main(String… original) {
    5:        String… copy = original;
    6:        Arrays.linearSort(original);
    7:        Arrays.search(original, "");
    8:        System.out.println(original.size() 
    9:           + " " + original[0]);
    10:    } }
     
    
    1. One line contains a compiler error.
    2. Two lines contain a compiler error.
    3. Three lines contain a compiler error.
    4. Four lines contain a compiler error.
    5. If the compiler errors were fixed, the code would throw an exception.
    6. If the compiler errors were fixed, the code would run successfully.
  16. What is the output of the following? (Choose three.)
     20: var chars = new ______________________<Character>();
     21: chars.add('a');
     22: chars.add(Character.valueOf('b'));
     23: chars.set(1, 'c');
     24: chars.remove(0);
     25: System.out.print(chars.size() + " " + chars.contains('b'));
     
    1. When inserting ArrayList into the blank, the code prints 1 false.
    2. When inserting ArrayList into the blank, the code does not compile.
    3. When inserting HashMap into the blank, the code prints 1 false.
    4. When inserting HashMap into the blank, the code does not compile.
    5. When inserting HashSet into the blank, the code prints 1 false.
    6. When inserting HashSet into the blank, the code does not compile.
  17. What is the output of the following?
    import java.util.*;
    record Magazine(String name) {
       public int compareTo(Magazine m) {
          return name.compareTo(m.name);
       }
    }
    public class Newsstand {
       public static void main(String[] args) {
          var set = new TreeSet<Magazine>();
          set.add(new Magazine("highlights"));
          set.add(new Magazine("Newsweek"));
          set.add(new Magazine("highlights"));
          System.out.println(set.iterator().next());
       } }
    
    1. Magazine[name=highlights]
    2. Magazine[name=Newsweek]
    3. null
    4. The code does not compile.
    5. The code compiles but throws an exception at runtime.
  18. Which is the first line to prevent this code from compiling or running without error?
    11: char[][] ticTacToe = new char[3][3];
    12: ticTacToe[1][3] = 'X';
    13: ticTacToe[2][2] = 'X';
    14: ticTacToe[3][1] = 'X';
    15: System.out.println(ticTacToe.length + " in a row!");
    
    1. Line 11
    2. Line 12
    3. Line 13
    4. Line 14
    5. Line 15
    6. None of the above; the code compiles and runs without issue.
  19. What is the first line with a compiler error?
    class Mammal {}
    class Bat extends Mammal {}
    class Cat extends Mammal {}
    class Sat {}
     
    class Fur<T extends Mammal> {    // line R
       void clean() {
          var bat = new Fur<Bat>();  // line S
          var cat = new Fur<Cat>();  // line T
          var sat = new Fur<Sat>();  // line U
       }
    }
    
    1. Line R
    2. Line S
    3. Line T
    4. Line U
    5. None of the above
  20. What is a possible result of this code?
    17: var nums = new HashSet<Long>();
    18: nums.add((long) Math.min(5, 3));
    19: nums.add(Math.round(3.14));
    20: nums.add((long) Math.pow(4,2));
    21: System.out.println(nums);
    
    1. [3]
    2. [16]
    3. [16, 3]
    4. [16, 3, 3]
    5. None of the above
  21. What is the output of the following?
    5: var x = new ArrayDeque<Integer>();
    6: x.offer(18);
    7: x.offer(5);
    8: x.push(13);
    9: System.out.println(x.poll() + " " + x.poll());
    
    1. 13 5
    2. 13 18
    3. 18 5
    4. 18 13
    5. The code does not compile.
    6. The code compiles but prints something else.
  22. Suppose we want to store JellyBean objects. Which of the following require JellyBean to implement the Comparable interface or create a Comparator to add them to the collection? (Choose two.)
    1. ArrayList
    2. HashMap
    3. HashSet
    4. SortedArray
    5. TreeMap
    6. TreeSet
  23. Which of the following references the first and last elements in a nonempty array?
    1. trains[0] and trains[trains.length]
    2. trains[0] and trains[trains.length - 1]
    3. trains[1] and trains[trains.length]
    4. trains[1] and trains[trains.length - 1]
    5. None of the above
  24. Which of the following fills in the blank so this code compiles?
    public static void throwOne(Collection<____________> coll) {
       var iter = coll.iterator();
       if (iter.hasNext())
          throw iter.next();
    }
     
    1. ?
    2. ? extends RuntimeException
    3. ? super RuntimeException
    4. T
    5. T extends RuntimeException
    6. T super RuntimeException
    7. None of the above
  25. Which of these four array declarations produces a different array than the others?
    1. int[][] nums = new int[2][1];
    2. int[] nums[] = new int[2][1];
    3. int[] nums[] = new int[][] { { 0 }, { 0 } };
    4. int[] nums[] = new int[][] { { 0, 0 } };
  26. What does the following output?
    var laptops = new String[] { "Linux", "Mac", "Windows" };
    var desktops = new String[] { "Mac", "Linux", "Windows" };
     
    var search = Arrays.binarySearch(laptops, "Linux");
    var mismatch1 = Arrays.mismatch(laptops, desktops);
    var mismatch2 = Arrays.mismatch(desktops, desktops);
     
    System.out.println(search + " " + mismatch1 + " " + mismatch2);
    
    1. -1 0 -1 is guaranteed
    2. -1 -1 0 is guaranteed
    3. 0 -1 0 is guaranteed
    4. 0 0 -1 is guaranteed
    5. The output is not defined.
    6. The code does not compile.
  27. Which line in the main() method doesn't compile or points to a class that doesn't compile?
    1:  interface Comic<S> {
    2:     void draw(S s);
    3:  }
    4:  class ComicClass<S> implements Comic<S> {
    5:     public void draw(S s) {
    6:        System.out.println(s);
    7:     }
    8:  }
    9:  class SnoopyClass implements Comic<Snoopy> {
    10:    public void draw(Snoopy s) {
    11:       System.out.println(s);
    12:    }
    13: }
    14: class SnoopyComic implements Comic<Snoopy> {
    15:    public void draw(S s) {
    16:       System.out.println(s);
    17:    }
    18: }
    19: public class Snoopy {
    20:    public static void main(String[] args) {
    21:       Comic<Snoopy> s1 = s -> System.out.println(s);
    22:       Comic<Snoopy> s2 = new ComicClass<>();
    23:       Comic<Snoopy> s3 = new SnoopyClass();
    24:       Comic<Snoopy> s4 = new SnoopyComic();
    25:    } }
    
    1. Line 21
    2. Line 22
    3. Line 23
    4. Line 24
    5. None of the above. All of the code compiles.
  28. Fill in the blank to make this code compile:
    public class Truck implements Comparable<Truck> {
       private int id;
       public Truck(int id) {
          this.id = id;
       }
       @Override
       public int _________________ {
          return id - t.id;
       } }
     
    1. compare(Truck t)
    2. compare(Truck t1, Truck t2)
    3. compareTo(Truck t)
    4. compareTo(Truck t1, Truck t2)
    5. None of the above
  29. How many lines does the following code output?
    var days = new String[] { "Sunday", "Monday", "Tuesday",
       "Wednesday", "Thursday", "Friday", "Saturday" };
     
    for (int i = 0; i < days.length; i++)
       System.out.println(days[i]);
    
    1. Six
    2. Seven
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  30. Which of the following fill in the blank to print out true? (Choose two.)
    String[] array = {"Natural History", "Science"};
    var museums = _________________;
    museums.set(0, "Art");
    System.out.println(museums.contains("Art"));
     
    1. Arrays.asList(array)
    2. Arrays.asList("Natural History, Science")
    3. List.of(array)
    4. List.of("Natural History", "Science")
    5. new ArrayList<String>("Natural History", "Science")
    6. new List<String>("Natural History", "Science")
  31. Which option cannot fill in the blank to print Clean socks?
    class Wash<T> {
       T item;
       public void clean(T item) {
          System.out.println("Clean " + item);
       } }
    public class LaundryTime {
       public static void main(String[] args) {
          _______________________
          wash.clean("socks");
       } }
     
    1. var wash = new Wash<String>();
    2. var wash = new Wash<>();
    3. Wash wash = new Wash();
    4. Wash wash = new Wash<String>();
    5. Wash<String> wash = new Wash<>();
    6. All of these can fill in the blank.
  32. Which of the options in the graphic best represent the blocks variable?
    var blocks = new char[][] {
        { 'a', 'b', 'c' }, { 'd' }, { 'e', 'f' } };
    

    An illustration of four options for a code: Option A, Option B, Option C, and Option D.
    1. Option A
    2. Option B
    3. Option C
    4. Option D
  33. Fill in the blank so the code prints gamma. (Choose two.)
    var list = Arrays.asList("alpha", "beta", "gamma");
    Collections.sort(list, _________________);
    System.out.println(list.get(0));
     
    1. (s, t) -> s.compareTo(t)
    2. (s, t) -> t.compareTo(s)
    3. Comparator.comparing((String s) -> s.charAt(0))
    4. Comparator.comparing((String s) -> s.charAt(0)).reverse()
    5. Comparator.comparing((String s) -> s.charAt(0)).reversed()
  34. How many of the following are legal declarations?
    float[] lion = new float[];
    float[] tiger = new float[1];
    float[] bear = new[] float;
    float[] ohMy = new[1] float;
    
    1. None
    2. One
    3. Two
    4. Three
    5. Four
  35. Which is the first line of code that causes an ArrayIndexOutOfBoundsException?
    var matrix = new String[1][2];
    matrix[0][0] = "Don't think you are, know you are."; // m1
    matrix[0][1] = "I'm trying to free your mind Neo"; // m2
    matrix[1][0] = "Is all around you "; // m3
    matrix[1][1] = "Why oh why didn't I take the BLUE pill?"; // m4
    
    1. m1
    2. m2
    3. m3
    4. m4
    5. The code does not compile.
    6. None of the above.
  36. Suppose we have list of type List<Integer>. Which method allows you to pass that List and return an immutable Set containing the same elements?
    1. List.copyOf(list)
    2. List.of(list)
    3. Set.copyOf(list)
    4. Set.of(list)
    5. None of the above
  37. What does the following output? (Choose two.)
    var os = new String[] { "Mac", "Linux", "Windows" };
    Arrays.sort(os);
     
    System.out.println(Arrays.binarySearch(os, "RedHat"));
    System.out.println(Arrays.binarySearch(os, "Mac"));
    
    1. -1
    2. -2
    3. -3
    4. 0
    5. 1
    6. 2
  38. What does the following output?
    var names = new HashMap<String, String>();
    names.put("peter", "pan");
    names.put("wendy", "darling");
    var first = names.entrySet();        // line x1
    System.out.println(first.getKey());  // line x2
    
    1. peter
    2. wendy
    3. It does not compile due to line x1.
    4. It does not compile due to line x2.
    5. It does not compile due to another reason.
    6. It throws an exception at runtime.
  39. Which of these elements are in the output of the following? (Choose three.)
    var q = new ArrayDeque<String>();
    q.offerFirst("snowball");
    q.offer("sugar");
    q.offerLast("minnie");
                  
    System.out.println(q.poll());
    System.out.println(q.removeFirst());
    System.out.println(q.size());
    
    1. sugar
    2. minnie
    3. snowball
    4. 1
    5. 2
    6. 3
  40. Which of these pairs of declarations can point to an array that is different from the others?
    1. int[][][][] nums1a, nums1b;
    2. int[][][] nums2a[], nums2b;
    3. int[][] nums3a[][], nums3b[][];
    4. int[] nums4a[][][], numbs4b[][][];
  41. Which of the following does not behave the same way as the others?
    1. var set = new HashSet<>();
    2. var set = new HashSet<Object>();
    3. HashSet<> set = new HashSet<Object>();
    4. HashSet<Object> set = new HashSet<>();
    5. HashSet<Object> set = new HashSet<Object>();
  42. What is true about the output of the following code?
    var ints = new int[] {3,1,4};
    var others = new int[] {2,7,1,8};
    System.out.println(Arrays.compare(ints, others));
    
    1. It is negative because ints has fewer elements.
    2. It is 0 because the arrays can't be compared.
    3. It is positive because the first element is larger.
    4. It is undefined.
    5. The code does not compile.
  43. Fill in the blank so that the code prints beta.
    var list = List.of("alpha", "beta", "gamma");
    Collections.sort(list, _________________);
    System.out.println(list.get(0));
     
    1. (s, t) -> s.compareTo(t)
    2. (s, t) -> t.compareTo(s)
    3. Comparator.comparing(String::length)
    4. Comparator.comparing(String::length).reversed()
    5. None of the above
  44. What is the output of the following?
    12: var queue = new ArrayDeque<>();
    13: queue.offer("exelsior");
    14: queue.peekFirst();
    15: queue.addFirst("edwin");
    16: queue.removeLast();
    17: System.out.println(queue);
    
    1. [edwin]
    2. [excelsior]
    3. [edwin, excelsior]
    4. [excelsior, edwin]
    5. The code does not compile.
    6. The code throws an exception at runtime.
  45. How many dimensions does the array reference moreBools allow?
    boolean[][] bools[], moreBools;
    
    1. One dimension
    2. Two dimensions
    3. Three dimensions
    4. None of the above
  46. What is the result of the following?
    Comparator<Integer> c = (x, y) -> y - x;
    var ints = Arrays.asList(3, 1, 4);
    Collections.sort(ints, c);
    System.out.println(Collections.binarySearch(ints, 1));
    
    1. -1 is guaranteed.
    2. 0 is guaranteed.
    3. 1 is guaranteed.
    4. The code does not compile.
    5. The result is not defined.
  47. Which statement most accurately represents the relationship between searching and sorting with respect to the Arrays class?
    1. If the array is not sorted, calling Arrays.binarySearch() will be accurate, but slower than if it were sorted.
    2. The array does not need to be sorted before calling Arrays.binarySearch() to get an accurate result.
    3. The array must be sorted before calling Arrays.binarySearch() to get an accurate result.
    4. None of the above.
  48. Which statements are true about the following figure while ensuring the code continues to compile? (Choose two.)
    An illustration of two codings.
    1. <> can be inserted at positions P and R without making any other changes.
    2. <> can be inserted at positions Q and S without making any other changes.
    3. <> can be inserted at all four positions.
    4. Both variables point to an ArrayList<String>.
    5. Only one variable points to an ArrayList<String>.
    6. Neither variable points to an ArrayList<String>.
  49. What is the result of the following when called without any command-line arguments? (Choose two.)
    1: import java.util.*;
    2: public class Binary {
    3: 
    4:    public static void main(String… args) {
    5:       Arrays.sort(args);
    6:       System.out.println(Arrays.toString(args));
    7:       System.out.println(args[0]);
    8:    } }
    
    1. null
    2. []
    3. Binary
    4. The code throws an ArrayIndexOutOfBoundsException.
    5. The code throws a NullPointerException.
    6. The code does not compile.
  50. What is the first line with a compiler error?
    class Mammal {}
    class Bat extends Mammal {}
    class Cat extends Mammal {}
    class Sat {}
     
    class Fur<? extends Mammal> {    // line R
       void clean() {
          var bat = new Fur<Bat>();  // line S
          var cat = new Fur<Cat>();  // line T
          var sat = new Fur<Sat>();  // line U
       } }
    
    1. Line R
    2. Line S
    3. Line T
    4. Line U
    5. None of the above
  51. What is the result of running the following program?
    1:  package fun;
    2:  public class Sudoku {
    3:     static int[][] game = new int[6][6];
    4:
    5:     public static void main(String[] args) {
    6:        game[3][3] = 6;
    7:        Object[] obj = game;
    8:        obj[3] = "X";
    9:        System.out.println(game[3][3]);
    10:    } }
    
    1. 6
    2. X
    3. The code does not compile.
    4. The code compiles but throws a NullPointerException at runtime.
    5. The code compiles but throws a different exception at runtime.
  52. How many of these allow inserting null values: ArrayList, LinkedList, HashSet, and TreeSet?
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
  53. What is the output of the following?
    var threes = Arrays.asList("3", "three", "THREE");
    Collections.sort(threes);
    System.out.println(threes);
    
    1. [3, three, THREE]
    2. [3, THREE, three]
    3. [three, THREE, 3]
    4. [THREE, three, 3]
    5. None of the above
  54. How many dimensions does the array reference moreBools allow?
    boolean[][][] bools, moreBools;
    
    1. One dimension
    2. Two dimensions
    3. Three dimensions
    4. None of the above
  55. What is the output of the following?
    20: List<Character> chars = new ArrayList<>();
    21: chars.add('a');
    22: chars.add('b');
    23: chars.clear();
    24: chars.remove(0);
    25: System.out.print(chars.isEmpty() + " " + chars.length());
    
    1. false 1
    2. true 0
    3. 2
    4. The code does not compile.
    5. The code throws an exception at runtime.
  56. Which fills in the blank in the method signature to allow this code to compile and print [duck, duck, goose] at runtime?
    import java.util.*;
    public class ExtendingGenerics {
       private static <_________________ , U> U add(T list, U element) {
          list.add(element);
          return element;
       }
       public static void main(String[] args) {
          var values = new ArrayList<String>();
          add(values, "duck");
          add(values, "duck");
          add(values, "goose");
          System.out.println(values);
       } }
     
    1. ? extends Collection<U>
    2. ? implements Collection<U>
    3. T extends Collection<U>
    4. T implements Collection<U>
    5. None of the above
  57. What does the following output?
    String[] os = new String[] { "Mac", "Linux", "Windows" };
    System.out.println(Arrays.binarySearch(os, "Linux"));
    
    1. 0 is guaranteed.
    2. 1 is guaranteed.
    3. 2 is guaranteed
    4. The output is not defined.
    5. The code does not compile.
  58. Which is the first line to prevent this code from compiling and running without error?
    11: char[][] ticTacToe = new char[3,3];
    12: ticTacToe[1][3] = 'X';
    13: ticTacToe[2][2] = 'X';
    14: ticTacToe[3][1] = 'X';
    15: System.out.println(ticTacToe.length + " in a row!");
    
    1. Line 11
    2. Line 12
    3. Line 13
    4. Line 14
    5. Line 15
    6. None of the above; the code compiles and runs without issue.
  59. What is the result of the following?
    var list = new ArrayList<String>();
    list.add("Austin");
    list.add("Boston");
    list.add("San Francisco");
     
    list.removeIf(a -> a.length()> 10);
    System.out.println(list.size());
    
    1. 0
    2. 1
    3. 2
    4. 3
    5. The code does not compile.
  60. What happens when calling the following method with a non-null and non-empty array?
    public static void addStationName(String[] names) {
       names[names.length] = "Times Square";
    }
    
    1. It adds an element to the array whose value is Times Square.
    2. It replaces the last element in the array with the value Times Square.
    3. It throws an exception.
    4. It does not compile regardless of what is passed in.
    5. None of the above.
  61. Which is not a true statement about an array?
    1. An array expands automatically when it is full.
    2. An array is allowed to contain duplicate values.
    3. An array understands the concept of ordered elements.
    4. An array uses a zero index to reference the first element.
  62. Which of the following cannot fill in the blank to make the code compile?
    private void output(___________________<?> x) {
       x.forEach(System.out::println);
    }
    
    1. Collection
    2. LinkedList
    3. TreeMap
    4. None of these can fill in the blank.
    5. All of these can fill in the blank.
  63. Which of the following fills in the blank so this code compiles?
    public static void getExceptions(Collection<____________________> coll) {
       coll.add(new RuntimeException());
       coll.add(new Exception());
    }
     
    1. ?
    2. ? extends RuntimeException
    3. ? super RuntimeException
    4. T
    5. T extends RuntimeException
    6. T super RuntimeException
    7. None of the above
  64. What is the output of the following? (Choose two.)
    35: var mags = new HashMap<String, Integer>();
    36: mags.put("People", 1974);
    37: mags.put("Readers Digest", 1922);
    38: mags.put("The Economist", 1843);
    39:
    40: Collection<Integer> years = mags.values();
    41:
    42: List<Integer> sorted = new ArrayList<>(years);
    43: Collections.sort(sorted);
    44:
    45: int first = sorted.get(0);
    46: System.out.println(first);
    47:
    48: Integer last = sorted.get(sorted.size());
    49: System.out.println(last);
    
    1. 1843
    2. 1922
    3. 1974
    4. The code compiles but throws an exception at runtime.
    5. The code does not compile.
  65. How do you access the array element with the value of "z"?
    An illustration of accessing the array element with the value.
    1. dimensions["three"][2]
    2. dimensions["three"][3]
    3. dimensions[2][2]
    4. dimensions[3][3]
  66. What is the output of the following?
    import java.util.*;
    record Magazine(String name) implements Comparable<Magazine> {
       public int compareTo(Magazine m) {
          return name.compareTo(m.name);
       }
    }
    public class Newsstand {
       public static void main(String[] args) {
          var set = new TreeSet<Magazine>();
          set.add(new Magazine("highlights"));
          set.add(new Magazine("Newsweek"));
          set.add(new Magazine("highlights"));
          System.out.println(set.iterator().next());
       } }
    
    1. Magazine[name=highlights]
    2. Magazine[name=Newsweek]
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  67. Which can fill in the blanks to print Cleaned 2 items?
    import java.util.*;
    class Wash<T _________________ Collection> {
       public void clean(T items) {
          System.out.println("Cleaned " + items.size() + " items");
       } }
    public class LaundryTime {
       public static void main(String[] args) {
          Wash<List> wash = new Wash<_________________>();
          wash.clean(List.of("sock", "tie"));
       } }
     
    1. extends, ArrayList
    2. extends, List
    3. super, ArrayList
    4. super, List
    5. None of the above
  68. How many lines does the following code output?
    var days = new String[] { "Sunday", "Monday", "Tuesday",
       "Wednesday", "Thursday", "Friday", "Saturday" };
    for (int i = 1; i <= days.length; i++)
       System.out.println(days[i]);
    
    1. Six
    2. Seven
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  69. What is the output of the following?
    var listing = new String[][] {
        { "Book" }, { "Game", "29.99" } };
    System.out.println(listing.length + " " + listing[0].length);
    
    1. 1 1
    2. 1 2
    3. 2 1
    4. 2 2
    5. The code does not compile.
    6. The code compiles but throws an exception at runtime.
  70. What is the output of the following?
    Queue<String> q = new ArrayDeque<>();
    q.add("snowball");
    q.addLast("sugar");
    q.addFirst("minnie");
                               
    System.out.println(q.peek() + " " + q.peek() + " " + q.size());
    
    1. sugar sugar 3
    2. sugar minnie 1
    3. minnie minnie 3
    4. minnie snowball 1
    5. The code does not compile.
    6. None of the above.
  71. What is the result of the following?
    13: var numbers = Arrays.asList(3, 1, 4);
    14: numbers.set(1, null);
    15: int first = numbers.get(0);
    16: int middle = numbers.get(1);
    17: int last = numbers.get(3);
    18: System.out.println(first + " " + middle + " " + last);
    
    1. The code does not compile.
    2. Line 14 throws an exception.
    3. Line 15 throws an exception.
    4. Line 16 throws an exception.
    5. Line 17 throws an exception.
    6. 3null4
  72. Fill in the blank so the code prints gamma.
    var list = Arrays.asList("alpha", "beta", "gamma");
    Collections.sort(list, ________________________________);
    System.out.println(list.get(0));
     

    1. Comparator.comparing(String::length)
         .andCompare(s -> s.charAt(0))
       

    2. Comparator.comparing(String::length)
         .thenCompare(s -> s.charAt(0))
       

    3. Comparator.comparing(String::length)
         .thenComparing(s -> s.charAt(0))
       

    4. Comparator.comparing(String::length)
         .andCompare(s -> s.charAt(0))
         .reversed()
       

    5. Comparator.comparing(String::length)
         .thenCompare(s -> s.charAt(0))
         .reversed()
       

    6. Comparator.comparing(String::length)
         .thenComparing(s -> s.charAt(0))
         .reversed()
       
  73. What is the output of the following when run as java FirstName.java Wolfie? (Choose two.)
    public class FirstName {
       public static void main(String… names) {
          System.out.println(names[0]);
          System.out.println(names[1]);
       } }
    
    1. FirstName
    2. Wolfie
    3. The code throws an ArrayIndexOutOfBoundsException.
    4. The code throws a NullPointerException.
    5. The code throws a different exception.
  74. What does the following output?
    11: var pennies = new ArrayList<>();
    12: pennies.add(1);
    13: pennies.add(2);
    14: pennies.add(Integer.valueOf(3));
    15: pennies.add(Integer.valueOf(4));
    16: pennies.remove(2);
    17: pennies.remove(Integer.valueOf(1));
    18: System.out.println(pennies);
    
    1. [1, 2]
    2. [1, 4]
    3. [2, 4]
    4. [2, 3]
    5. [3, 4]
    6. The code does not compile.
  75. What is true of the following code? (Choose two.)
    private static void sortAndSearch(String… args) {
       var one = args[1];
       Comparator<String> comp = (x, y) -> _________________;
       Arrays.sort(args, comp);
       var result = Arrays.binarySearch(args, one, comp);
       System.out.println(result);
    }
    public static void main(String[] args) {
       sortAndSearch("seed", "flower");
    }
     
    1. If the blank contains -x.compareTo(y), then the code outputs 0.
    2. If the blank contains -x.compareTo(y), then the code outputs -1.
    3. If the blank contains x.compareTo(y), then the code outputs 0.
    4. If the blank contains -y.compareTo(x), then the code outputs 0.
    5. If the blank contains -y.compareTo(x), then the code outputs -1.
    6. If the blank contains y.compareTo(x), then the code outputs 0.
    7. If the blank contains y.compareTo(x), then the code outputs -1.
  76. What does this code output?
    String[] nums = new String[] { "1", "9", "10" };
    Arrays.sort(nums);
    System.out.println(Arrays.toString(nums));
    
    1. [1, 9, 10]
    2. [1, 10, 9]
    3. [9, 1, 10]
    4. [9, 10, 1]
    5. [10, 1, 9]
    6. [10, 9, 1]
  77. Which is the first line to prevent this code from compiling and running without error?
    11: char[][] ticTacToe = new char[3][3];
    12: ticTacToe[0][0] = 'X';
    13: ticTacToe[1][1] = 'X';
    14: ticTacToe[2][2] = 'X';
    15: System.out.println(ticTacToe.length + " in a row!");
    
    1. Line 11
    2. Line 12
    3. Line 13
    4. Line 14
    5. Line 15
    6. None of the above; the code compiles and runs without issue.
  78. What is true of the following code? (Choose three.)
    36: var names = new HashMap<String, String>();
    37: names.put("peter", "pan");
    38: names.put("wendy", "darling");
    39:
    40: String w = names.getOrDefault("peter");
    41: String x = names.getOrDefault("peter", "x");
    42: String y = names.getOrDefault("john", "x");
    
    1. Exactly one line does not compile.
    2. Exactly two lines do not compile.
    3. If any lines that do not compile are removed, the String on line 40 is set to null.
    4. If any lines that do not compile are removed, the String on line 41 is set to "pan".
    5. If any lines that do not compile are removed, the String on line 41 is set to "x".
    6. If any lines that do not compile are removed, the String on line 42 is set to "x".
  79. What does the following output?
    18: List<String> list = List.of(
    19:    "Mary", "had", "a", "little", "lamb");
    20: Set<String> set = new HashSet<>(list);
    21: set.addAll(list);
    22: for(String sheep : set)
    23:    if (sheep.length()> 1)
    24:       set.remove(sheep);
    25: System.out.println(set);
    
    1. [a, lamb, had, Mary, little]
    2. [a]
    3. [a, a]
    4. The code does not compile.
    5. The code throws an exception at runtime.
  80. Which of the following fills in the blank so this code compiles?
    public static void getExceptions(Collection<_________________> coll) {
       coll.add(new RuntimeException());
       coll.add(new Exception());
    }
     
    1. ?
    2. ? extends Exception
    3. ? super Exception
    4. T
    5. T extends Exception
    6. T super Exception
    7. None of the above
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.37.129