OCA CHAPTER 4 REVIEW QUESTIONS

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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.

A

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.

B

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.

B

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.

D

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

B. Since no elements are being provided when creating the arrays, a size is required. Therefore, lion and bear are incorrect. The braces containing the size are required to be after the type, making ohMy incorrect. The only one that is correct is tiger, making the correct answer Option B.

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.

C

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.

B - cannot put "X" to an int

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.

B - length will output the number of elements

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

B. Arrays begin with an index of 0. This array is a 3×3 array. Therefore. only indexes 0, 1. and 2 are valid. Line r2 throws an ArrayIndexOutOfBoundsException. Therefore. Option B is correct.

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

C - brackets on type will go to all variables defined

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

C. This code creates a two-dimensional array of size 1×2. Lines m1 and m2 assign values to both elements in the outer array. Line m3 attempts to reference the second element of the outer array. Since there is no such position, it throws an exception, and Option C is correct.

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

A

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

B. The first line creates one object; the array itself. While there are four references to null in that array, none of those are objects. The second line creates one object and points one of the array references to it. So far there are two objects: the array itself and one object it is referencing. The third line does the same, bringing up the object count to three. Therefore, Option B is correct.

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.

C

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.

C - cannot return an int to a string result

34. How do you access the array element with the value of "z"? "one" "two" "three" V V V "p" "x" "x" "y" "y" "z" A. dimensions["three"][2] B. dimensions["three"][3] C. dimensions[2][2] D. dimensions[3][3]

C - zero based *remember

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

C. Calling toString() on an array doesn't output the contents of the array, making Option C correct. If you wanted Option A to be the answer, you'd have to call Arrays.toString(strings).

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

C. When using an array initializer, you are not allowed to specify the size separately. The size is inferred from the number of elements listed. Therefore, tiger and ohMy are incorrect. When you're not using an array initializer, the size is required. An empty array initializer is allowed. Option C is correct because lion and bear are legal.

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.

D. Line 6 assigns an int to a cell in a 2D array. This is fine. Line 7 casts to a general Object[]. This is dangerous, but legal. Why is it dangerous, you ask? That brings us to line 8. The compiler can't protect us from assigning a String to the int[] because the reference is more generic. Therefore, line 8 throws an ArrayStoreException because the type is incorrect, and Option D is correct. You couldn't have assigned an int on line 8 either because obj[3] is really an int[] behind the scenes and not an int.

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

D. This code is correct. Line r1 correctly creates a 2D array. The next three lines correctly assign a value to an array element. Line r3 correctly outputs 3 in a row!

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.

D. Three dots in a row is a varargs parameter. While varargs is used like an array from within the method, it can only be used as a method parameter. This syntax is not allowed for a variable, making Option D the answer.

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

D. Trick question! While int is a primitive, all arrays are objects. One way to tell is that an array has a public instance variable called length. Another way is that you can assign it a variable of type Object. Therefore, Option D is correct.


Ensembles d'études connexes

Social Studies Cumulative Exam (86%)

View Set

Further Practice on 'Key' Word Transformation 121-143 (Ariella & Dasha)

View Set

Ch 45 Assessment and Management of Patients with Endocrine Disorders

View Set

Property-Casualty Insurance Test

View Set

Fundamentals 171 C. 12 Essentials

View Set

ExamFX Chapter 6 Arkansas Statutes, Rules, And Regulations for Life and Health

View Set

Conversion Disorder (Functional Neurological Symptom Disorder)

View Set