OCJP Practice Papers Creating and Using Arrays include following topics

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

See the complete syllabus for OCJP here

Part 1 - OCJP Practice Papers Creating and Using Arrays

1.What symbol is used for a varargs method parameter?
A. ..
B. …
C. –
D. —

2. Fill in the blank in the following code to get the first element from the varargsparameter. public void toss (Frisbee... f) { Frisbee first = ____________; }
A. f
B. f[0]
C. f[1]
D. None of the above

3. Which of the following are primitives? int[] lowercase = new int[0]; Integer[] uppercase = new Integer[0];
A. Only lowercase
B. Only uppercase
C. Bother lowercase and uppercase
D. Neither lowercase nor uppercase

4. How many of the following are legal declarations? []double lion; double[] tiger; double bear[];
A. None
B. One
C. Two
D. 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)); }
A. printStormName("Arlene");
B. printStormName(new String[] { "Bret" });
C. printStormNames("Cindy");
D. printStormNames(new String[] { "Don" });

6. How do you determine the number of elements in an array?
A. buses.length
B. buses.length()
C. buses.size
D. buses.size()

7. Which of the following create an empty two-dimensional array with dimensions 22?
A. int[][] blue = new int[2, 2];
B. int[][] blue = new int[2], [2];
C. int[][] blue = new int[2][2];
D. 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]);
A. Six
B. Seven
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

9. What are the names of the methods to do searching and sorting respectively onarrays?
A. Arrays.binarySearch() and Arrays.linearSort()
B. Arrays.binarySearch() and Arrays.sort()
C. Arrays.search() and Arrays.linearSort()
D. 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));
A. [1, 9, 10]
B. [1, 10, 9]
C. [10, 1, 9]
D. None of the above

11. Which of the following references the first and last element in a non-empty array?
A. trains[0] and trains[trains.length]
B. trains[0] and trains[trains.length - 1]
C. trains[1] and trains[trains.length]
D. 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] {};
A. None
B. One
C. Two
D. 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;
A. None
B. One
C. Two
D. Three

14. Which statement most accurately represents the relationship between searching andsorting with respect to the Arrays class?
A. If the array is not sorted, calling Arrays.binarySearch() will be accurate, but slower than if it were sorted.
B. The array does not need to be sorted before calling Arrays.binarySearch() to get an accurate result.
C. The array must be sorted before calling Arrays.binarySearch() to get an accurate result.
D. None of the above

15. Which is not a true statement about an array?
A. An array expands automatically when it is full.
B. An array is allowed to contain duplicate values.
C. An array understands the concept of ordered elements.
D. 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
A. m1
B. m2
C. m3
D. m4

17. What does the following output? String[] os = new String[] { "Mac", "Linux", "Windows" }; Arrays.sort(os); System.out.println(Arrays.binarySearch(os, "Mac"));
A. 0
B. 1
C. 2
D. 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
A. Line r1
B. Line r2
C. Line r3
D. 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);
A. Two
B. Three
C. Four
D. Five

20. How many of the following are legal declarations? [][] String alpha; [] String beta; String[][] gamma; String[] delta[]; String epsilon[][];
A. Two
B. Three
C. Four
D. Five

21. Which of the options in the graphic best represent the blocks variable? char[][] blocks = new char[][] { { 'a', 'b', 'c' }, { 'd' }, { 'e', 'f' } };
A. Option A
B. Option B
C. Option C
D. Option D

22. What happens when calling the following method with a non-null and non-emptyarray? public static void addStationName(String[] names) { names[names.length] = "Times Square"; }
A. It adds an element to the array the value of which is Times Square.
B. It replaces the last element in the array with the value Times Square.
C. It does not compile.
D. 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]);
A. Six
B. Seven
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

24. How many dimensions does the array reference moreBools allow? boolean[][][] bools, moreBools;
A. One dimension
B. Two dimensions
C. Three dimensions
D. None of the above

25. What is a possible output of the following code? String[] strings = new String[2]; System.out.println(strings);
A. [null, null]
B. [,]
C. [Ljava.lang.String;@74a14482
D. 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
A. Line r1
B. Line r2
C. Line r3
D. 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]); } }
A. 0
B. 0 followed by an exception
C. 1 followed by an exception
D. 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: }
A. X
B. The code does not compile.
C. The code compiles but throws a NullPointerException at runtime.
D. 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"));
A. -1
B. -2
C. -3
D. 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]); } }
A. FirstName
B. Wolfie
C. The code throws an ArrayIndexOutOfBoundsException.
D. 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); } }
A. 0
B. 1
C. 2
D. 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); } }
A. 0
B. 1
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

33. Which of these four array declarations produces a different array than the others?
A. int[][] nums = new int[2][1];
B. int[] nums[] = new int[2][1];
C. int[] nums[] = new int[][] { { 0 }, { 0 } };
D. int[] nums[] = new int[][] { { 0, 0 } };

34. How do you access the array element with the value of "z"?
A. dimensions["three"][2]
B. dimensions["three"][3]
C. dimensions[2][2]
D. 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]);
A. Six
B. Seven
C. The code does not compile.
D. 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]); } }
A. FirstName
B. Wolfie
C. The code throws an ArrayIndexOutOfBoundsException.
D. 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
A. Line r1
B. Line r2
C. Line r3
D. 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()); } }
A. 0
B. 1
C. 2
D. The code does not compile.

39. How many dimensions does the array reference moreBools allow? boolean[][] bools[], moreBools;
A. One dimension
B. Two dimensions
C. Three dimensions
D. 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)); } }
A. null
B. []
C. The code does not compile.
D. 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"));
A. 0
B. 1
C. 2
D. 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: }
A. X
B. The code does not compile.
C. The code compiles but throws a NullPointerException at runtime.
D. 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);
A. 2 1
B. 2 2
C. The code does not compile.
D. 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]); } }
A. FirstName
B. The code does not compile.
C. The code throws an ArrayIndexOutOfBoundsException.
D. 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]);
A. Six
B. Seven
C. The code does not compile.
D. 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); } }
A. 0
B. 1
C. 2
D. 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"));
A. 0
B. 1
C. 2
D. The output is not defined.

48. Which of the following statements are true? I. You can always change a method signature from call(String[] arg) to call(String... arg) without causing a compiler error in the calling code. II. You can always change a method signature from call(String... arg) to call(String[] arg) without causing a compiler error in the existing code.
A. I
B. II
C. Both I and II
D. Neither I nor II

49. Which of these four array references can point to an array that is different from the others?
A. int[][][][] nums1a, nums1b;
B. int[][][] nums2a[], nums2b;
C. int[][] nums3a[][], nums3b[][];
D. 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); } }
A. 0
B. 1
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

OCJP Practice Test, Certification Path, Tips/Tricks

  1. Oracle (OCJP) Java Certification Exam
  2. Java Certification Tips And Tricks
  3. OCJP Practice Papers - Java Basics
  4. OCJP Practice Papers - Operators And Decision Constructs
  5. OCJP Practice Papers-A Advanced Java Class Design
  6. OCJP Practice Papers Creating And Using Arrays
  7. OCJP Practice Papers Using Loop Constructs
  8. OCJP Practice Papers - Generics And Collections
  9. OCJP Practice Papers - Lambda Built-In Functional Interfaces
  10. OCJP Practice Papers Java Class Design
  11. OCJP Practice Papers - Java Stream API
  12. OCJP Practice Papers - Exceptions And Assertions
  13. OCJP Practice Papers - Date/Time API
  14. OCJP Practice Papers - Java I/O Fundamentals
  15. OCJP Practice Papers - Working With Methods And Encapsulation
  16. OCJP Practice Papers - Working With Inheritance
  17. OCJP Practice Papers - Handling Exceptions
  18. OCJP Practice Papers - Selected Classes From Java API
  19. OCJP Practice Papers - Java File I/O Nio.2
  20. OCJP Practice Papers - Java Concurrency