Chapter 4

Ace your homework & exams now with Quizwiz!

47:What does the following output? String[] os = new String[] { "Linux", "Mac", "Windows" }; System.out.print(Arrays.binarySearch(os, "Linux")); A: 0 B: 1 C: 2 D: The output is not defined.

A: 0 You do not have to call sort if the array is already sorted.

43: What is the output of the following ? String[][] listing = new String[][] { {"Book"}, {"Game", "29.99"} }; System.out.print(listing.length + " " + listing[0].length); A: 2 1 B: 2 2 C: The code does not compile. D: The code does compile, but throws an exception at runtime.

A: 2 1

15: Which is not a true statement about an array? A: An array expands automatically when its 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.

A: An array expands automatically when its full.

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 calling code. A: I B: II C: Both I and II. D: Neither I nor II.

A: I

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.

A: Line r1

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 does compile, but throws an exception at runtime.

A: Six

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

A: buses.length Arrays use the length variable to determine the number of elements. ArrayList use the size variable to determine the number of arrays.

01: What symbol is used for a varsargs method parameter? A: .. B: ... C: -- D:---

B: ...

17: What does the following output? String[] os = new String[] { "Mac", "Linux", "Windows" }; Arrays.sort(os); System.out.print(Arrays.binarySearch(os, "Mac")); A: 0 B: 1 C: 2 D: The output is not defined.

B: 1

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.print(result); } } A: 0 B: 1 C: The code does not compile. D: The code does compile, but throws an exception at runtime.

B: 1

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: 1

09: What are the names of the methods to do searching and sorting respectively on arrays. A: Arrays.binarySearch() and Arrays.linearSort() B: Arrays.binarySearch() and Arrays.Sort() C: Arrays.search() and Arrays.linearSort() D: Arrays.search() and Arrays.sort()

B: Arrays.binarySearch() and Arrays.Sort()

26:Which is the first line to prevent this code from compiling and running without error ? char[][] ticTacToe = new char[3][3]; //r1 <------Here on 18 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: Line r2

08: 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 does compile, but throws an exception at runtime.

B: Seven

14: Which statement most accurately represents the relationship between searching and sorting with respect to the Arrays class? A: If the array is not sorted, calling Arrays.binarySearch() will be accurate, but slower than if 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.

B: The array does not need to be sorted before calling Arrays.binarySearch() to get an accurate result.

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: obj[3][3] = "X"; 9: System.out.println(game[3][3]); 10: } 11: } A: X B: The code does not compile. C: The code does compile, but throws a NullPointerException at runtime. D: The code does compile, but throws a different exception at runtime.

B: The code does not compile. Line 8 attempts to store a String in an array meant for an int.

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

B: Three

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: Three The Array itself is an object. All references in the array are set to null and do not count as objects. Assignment creates the other two objects.

39: How many dimensions does the moreBools allow? boolean[][] bools[], moreBools; A: One dimension B: Two dimensions C: Three dimensions D: Four dimensions

B: Two dimensions

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 an NullPointerException

B: Wolfie

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

B: [1,10,9] Notice the elements of the array are String and not int. This means they will be sorted alphabetically, because of that the number 1 is sorted before 9.

40: What is the result of running the following 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 does compile, but throws an exception at runtime.

B: [] Empty array is not null it is just empty.

02: Fill in the blank in the following code to get the first element from the varsargs parameter. public void toss (Frisbee... f) { Frisbee first = ________ } A: f B: f[0] C: f[1] D: None of the above

B: f[0]

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[][][], nums4b[][][];

B: int[][][] nums2a, nums2b;

11: Which of the following references the first and last elements 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]

B: trains[0] and trains[trains.length - 1]

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.

C: -3 The rule is to take the negative index of where it would be inserted and subtract 1. Indexes are zero based.

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.

C: 2

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, one); System.out.print(result); } } A: 0 B: 1 C: The code does not compile. D: The code does compile, but throws an exception at runtime.

C: The code does not compile.

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 does compile, but throws an exception at runtime.

C: The code does not compile. Size is used for an ArrayList

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 an NullPointerException

C: The code throws an ArrayIndexOutOfBoundsException

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 NullPointerException D: The code throws an ArrayIndexOutOfBoundsException

C: The code throws an NullPointerException

24: How many dimensions does the array reference moreBools allow? boolean[][][] bools moreBools; A: One dimension B: Two dimensions C: Three dimensions D: Four dimensions

C: Three dimensions

04: How many of the following are legal declarations? []double lion; double[] tiger; double bear[]; A: None B: One C: Two D: Three

C: Two Braces are not allowed to appear before the type.

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

C: Two Since no elements are being provided on initialization the size is required, because it cannot be inferred. float[] lion = new float[]; ERROR float[] tiger = new float[1]; float[] bear = new[] float; ERROR float[] ohMy = new[1] float;

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: Two String lion [] = new String[] {"lion"}; String tiger [] = new String[1] {"tiger"}; ERROR String bear [] = new String[] {}; String ohMy [] = new String[0] {}; ERROR When using an array initializer you are not allowed to specify the size separately. The size is inferred from the number of elements listed.

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: [Ljava.lang.String;@74a14482]

07: Which of the following create an empty two-dimensional array with dimensions 2x2? 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[2x2];

C: int[][] blue = new int[2][2];

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: m3

05: Given the following two methods, which method call will not compile? public void printStormName(String... names) { System.out.print(Arrays.toString(names)); } public void printStormNames(String[] names) { System.out.print(Arrays.toString(names)); } A: printStormName("Arlene"); B: printStormName(new String[] ( "Bret" )); C: printStormNames("Cindy"); D: printStormNames(new String[] ( "Don" ));

C: printStormNames("Cindy"); varsargs parameter = ... can receive an Array or Single [] parameter = [] can only receive an Array (only) A: printStormName("Arlene"); calls varsarg with single B: printStormName(new String[] ( "Bret" )); calls varsarg with array C: printStormNames("Cindy"); calls array with single > ERROR D: printStormNames(new String[] ( "Don" )); calls array with array

22: What happens when calling the following method with a non-null and non-empty array? public static void addStationName(String[] name) { 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 with the value of Times Square. C: The code does not compile. D: It throws an exception.

D: It throws an exception. ArrayIndexOutOfBoundsException

03: Which of the following are primitives? int[] lowercase = new int[0]; Integer[] uppercase = new Integer[0]; A: Only lowercase B: Only Uppercase C: Both lowercase and uppercase D: Neither lowercase and uppercase

D: Neither lowercase and uppercase All arrays are objects, even though int is a primitive.

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: None of the above.

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 does compile, but throws a NullPointerException at runtime. D: The code does compile, but throws a different exception at runtime.

D: The code does compile, but throws a different exception at runtime. Line 8 assigns a string to an int this throws an ArrayStoreException, because the type is incorrect. obj[3] is really an int[].

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 does compile, but throws an exception at runtime.

D: The code does compile, but throws an exception at runtime. The use of the <= operator throws an ArrayIndexOutOfBoundsException.

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: The code does not compile. varsargs syntax cannot be used for a variable

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.

D: The code does not compile. Arrays expose a length variable, they do not have a length method.

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.

D: The output is not defined. requires sort.

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}};

D: int[] nums[] = new int[][] { {0, 0}}; Remember how to read multidimensional arrays. [2][1] means 2 of 1. Not 1 of 2 and 1 of 1. Option D specifies an array of 1 with a length of 2.

34: Diagram Here. A: B: C: D:

Diagram Here.

21: Diagram here. A: B: C: D:

Diagram here.


Related study sets

Pelvic girdle: Bones & ligaments

View Set

Strategic and Entrepreneurial Thinking Final

View Set

Auxiliary Verbs "Be," "Do," "Have"

View Set

Brandman - Quantitative Literacy Level A

View Set

Adult health Chapter 24: Nursing Management: Patients With Intestinal and Rectal Disorders

View Set