Chapter 4
Creating and Using Arrays

THE OCA EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:

  • Creating and Using Arrays
    • Declare, instantiate, initialize and use a one-dimensional array
    • Declare, instantiate, initialize and use multi-dimensional arrays

  1. What symbol is used for a varargs method parameter?

    1. ..
    2. ...
    3. --
    4. ---
  2. Fill in the blank in the following code to get the first element from the varargs parameter.

    public void toss (Frisbee... f) {
       Frisbee first = ____________;
    }
    1. f
    2. f[0]
    3. f[1]
    4. None of the above
  3. Which of the following are primitives?

    int[] lowercase = new int[0];
    Integer[] uppercase = new Integer[0];
    1. Only lowercase
    2. Only uppercase
    3. Bother lowercase and uppercase
    4. Neither lowercase nor uppercase
  4. How many of the following are legal declarations?

    []double lion;
    double[] tiger;
    double bear[];
    1. None
    2. One
    3. Two
    4. Three
  5. Given the following two methods, which method call will not compile?

    public void printStormName(String... names) {
       System.out.println(Arrays.toString(names));
    }
    public void printStormNames(String[] names) {
       System.out.println(Arrays.toString(names));
    }
    1. printStormName("Arlene");
    2. printStormName(new String[] { "Bret" });
    3. printStormNames("Cindy");
    4. printStormNames(new String[] { "Don" });
  6. How do you determine the number of elements in an array?

    1. buses.length
    2. buses.length()
    3. buses.size
    4. buses.size()
  7. Which of the following create 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];
  8. How many lines does the following code output?

    String[] 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.
  9. What are the names of the methods to do searching and sorting respectively on arrays?

    1. Arrays.binarySearch() and Arrays.linearSort()
    2. Arrays.binarySearch() and Arrays.sort()
    3. Arrays.search() and Arrays.linearSort()
    4. Arrays.search() and Arrays.sort()
  10. 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. [10, 1, 9]
    4. None of the above
  11. Which of the following references the first and last element in a non-empty 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]
  12. How many of the following are legal declarations?

    String lion [] = new String[] {"lion"};
    String tiger [] = new String[1] {"tiger"};
    String bear [] = new String[] {};
    String ohMy [] = new String[0] {};
    1. None
    2. One
    3. Two
    4. Three
  13. 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
  14. 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
  15. 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.
  16. Which line of code causes an ArrayIndexOutOfBoundsException?

    String[][] 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
  17. What does the following output?

    String[] os = new String[] { "Mac", "Linux", "Windows" };
    Arrays.sort(os);
    System.out.println(Arrays.binarySearch(os, "Mac"));
    1. 0
    2. 1
    3. 2
    4. The output is not defined.
  18. Which is the first line to prevent this code from compiling and running without error?

    char[][] ticTacToe = new char[3,3];                  // r1
    ticTacToe[1][3] = 'X';                               // r2
    ticTacToe[2][2] = 'X';
    ticTacToe[3][1] = 'X';
    System.out.println(ticTacToe.length + " in a row!"); // r3
    1. Line r1
    2. Line r2
    3. Line r3
    4. None of the above
  19. How many objects are created when running the following code?

    Integer[] lotto = new Integer[4];
    lotto[0] = new Integer(1_000_000);
    lotto[1] = new Integer(999_999);
    1. Two
    2. Three
    3. Four
    4. Five
  20. How many of the following are legal declarations?

    [][] String alpha;
    [] String beta;
    String[][] gamma;
    String[] delta[];
    String epsilon[][];
    1. Two
    2. Three
    3. Four
    4. Five
  21. Which of the options in the graphic best represent the blocks variable?

    char[][] blocks = new char[][] { { 'a', 'b', 'c' }, { 'd' }, { 'e', 'f' } };
    Diagram shows four options where each option has blocks with different variables like a, b, c, d, e, f, and g in options A and C, while options B and D have empty blocks connected to variables.
    1. Option A
    2. Option B
    3. Option C
    4. Option D
  22. 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 the value of which is Times Square.
    2. It replaces the last element in the array with the value Times Square.
    3. It does not compile.
    4. It throws an exception.
  23. How many lines does the following code output?

    String[] days = new String[] { "Sunday", "Monday", "Tuesday",
          "Wednesday", "Thursday", "Friday", "Saturday" };
    for (int i = 0; i < days.size(); 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.
  24. 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
  25. What is a possible output of the following code?

    String[] strings = new String[2];
    System.out.println(strings);
    1. [null, null]
    2. [,]
    3. [Ljava.lang.String;@74a14482
    4. None of the above
  26. Which is the first line to prevent this code from compiling and running without error?

    char[][] ticTacToe = new char[3][3];                 // r1
    ticTacToe[1][3] = 'X';                               // r2
    ticTacToe[2][2] = 'X';
    ticTacToe[3][1] = 'X';
    System.out.println(ticTacToe.length + " in a row!"); // r3
    1. Line r1
    2. Line r2
    3. Line r3
    4. None of the above
  27. What is the result of running the following as java Copier?

    package duplicate;
    public class Copier {
      public static void main(String... original) {
          String... copy = original;
          System.out.println(copy.length + " " + copy[0]);
       }
    }
    1. 0
    2. 0 followed by an exception
    3. 1 followed by an exception
    4. The code does not compile.
  28. 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:     }
    11:  }
    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.
  29. What does the following output?

    String[] os = new String[] { "Mac", "Linux", "Windows" };
    Arrays.sort(os);
    System.out.println(Arrays.binarySearch(os, "RedHat"));
    1. -1
    2. -2
    3. -3
    4. The output is not defined.
  30. What is the output of the following when run as java FirstName Wolfie?

    public class FirstName {
       public static void main(String... names) {
          System.out.println(names[0]);
       }
    }
    1. FirstName
    2. Wolfie
    3. The code throws an ArrayIndexOutOfBoundsException.
    4. The code throws a NullPointerException.
  31. What is the output of the following when run as java Count 1 2?

    public class Count {
       public static void main(String target[]) {
          System.out.println(target.length);
       }
    }
    1. 0
    2. 1
    3. 2
    4. The code does not compile.
  32. What is the output of the following when run as java unix.EchoFirst seed flower?

    package unix;
    import java.util.*;
    public class EchoFirst {
     
       public static void main(String[] args) {
          String one = args[0];
          Arrays.sort(args);
          int result = Arrays.binarySearch(args, one);
          System.out.println(result);
      }
    }
    1. 0
    2. 1
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  33. 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 } };
  34. How do you access the array element with the value of "z"?

    Chart shows dimensions which is connected to variables like one, two, and three, where one is connected to p, two is connected to x and y, and three is connected to x, y, and z.
    1. dimensions["three"][2]
    2. dimensions["three"][3]
    3. dimensions[2][2]
    4. dimensions[3][3]
  35. How many lines does the following code output?

    String[] 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.
  36. What is the output of the following when run as java FirstName Wolfie?

    public class FirstName {
       public static void main(String... names) {
          System.out.println(names[1]);
       }
    }
    1. FirstName
    2. Wolfie
    3. The code throws an ArrayIndexOutOfBoundsException.
    4. The code throws a NullPointerException.
  37. Which is the first line to prevent this code from compiling and running without error?

    char[][] ticTacToe = new char[3][3];                 // r1
    ticTacToe[0][0] = 'X';                               // r2
    ticTacToe[1][1] = 'X';
    ticTacToe[2][2] = 'X';
    System.out.println(ticTacToe.length + " in a row!"); // r3
    1. Line r1
    2. Line r2
    3. Line r3
    4. None of the above
  38. What is the output of the following when run as java Count 1 2?

    public class Count {
       public static void main(String target[]) {
          System.out.println(target.length());
       }
    }
     
    1. 0
    2. 1
    3. 2
    4. The code does not compile.
  39. 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
  40. What is the result of the following when called as java counting.Binary?

    package counting;
    import java.util.*;
    public class Binary {
     
       public static void main(String... args) {
          Arrays.sort(args);
          System.out.println(Arrays.toString(args));
       }
    }
    1. null
    2. []
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  41. What does the following output?

    String[] os = new String[] { "Mac", "Linux", "Windows" };
    System.out.println(Arrays.binarySearch(os, "Linux"));
    1. 0
    2. 1
    3. 2
    4. The output is not defined.
  42. 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:     }
    11:  }
    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.
  43. What is the output of the following?

    String[][] listing = new String[][] { { "Book" }, { "Game", "29.99" } };
    System.out.println(listing.length + " " + listing[0].length);
    1. 2 1
    2. 2 2
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.
  44. What is the output of the following when run as java FirstName?

    public class FirstName {
       public static void main(String[] names) {
          System.out.println(names[0]);
       }
    }
    1. FirstName
    2. The code does not compile.
    3. The code throws an ArrayIndexOutOfBoundsException.
    4. The code throws a NullPointerException.
  45. How many lines does the following code output?

    String[] 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.
  46. What is the output of the following when run as java Count "1 2"?

    public class Count {
       public static void main(String target[]) {
          System.out.println(target.length);
       }
    }
    1. 0
    2. 1
    3. 2
    4. The code does not compile.
  47. What does the following output?

    String[] os = new String[] { "Linux", "Mac", "Windows" };
    System.out.println(Arrays.binarySearch(os, "Linux"));
    1. 0
    2. 1
    3. 2
    4. The output is not defined.
  48. Which of the following statements are true?

    • You can always change a method signature from call(String[] arg) to call(String... arg) without causing a compiler error in the calling code.
    • You can always change a method signature from call(String... arg) to call(String[] arg) without causing a compiler error in the existing code.
      1. I
      2. II
      3. Both I and II
      4. Neither I nor II
  49. Which of these four array references 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[][][];
  50. What is the output of the following when run as java unix.EchoFirst seed flower?

    package unix;
    import java.util.*;
    public class EchoFirst {
     
       public static void main(String[] args) {
          Arrays.sort(args);
          String result = Arrays.binarySearch(args, args[0]);
          System.out.println(result);
      }
    }
     
    1. 0
    2. 1
    3. The code does not compile.
    4. The code compiles but throws an exception at runtime.