chapter 6 part2

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

78) What is the value of the cnt variable after the execution of the code snippet below? ArrayList<Integer> somenum = new ArrayList<Integer>(); somenum.add(1); somenum.add(2); somenum.add(1); int cnt = 0; for (int index = 0; index < somenum.size(); index++) { if (somenum.get(index) % 2 == 0) { cnt++; } } a) 1 b) 2 c) 3 d) 0

a) 1

99) What is the output of the following code? int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } }; System.out.println(counts[3].length); a) 2 b) 3 c) 4 d) 5

a) 2

60) Which one of the following statements is correct for displaying the value in the second row and the third column of a two-dimensional, size 3 by 4 array? a) System.out.println(arr[1][2]); b) System.out.println(arr[2][3]); c) System.out.println(arr[2][1]); d) System.out.println(arr[3][2]);

a) System.out.println(arr[1][2]);

84) Which one of the following statements is correct for displaying the value in the third row and the fourth column of a two-dimensional 5 by 6 array? a) System.out.println(arr[2][3]); b) System.out.println(arr[3][4]); c) System.out.println(arr[3][2]); d) System.out.println(arr[4][3]);

a) System.out.println(arr[2][3]);

76) What should you check for when calculating the largest value in an array list? a) The array list contains at least one element. b) The array list contains at least two elements. c) The array list contains the minimum value in the first element. d) The array list contains the maximum value in the first element.

a) The array list contains at least one element.

96) What should you check for when calculating the smallest value in an array list? a) The array list contains at least one element. b) The array list contains at least two elements. c) The array list contains the maximum value in the first element. d) The array list contains the minimum value in the first element.

a) The array list contains at least one element.

88) Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); friends.add("Harry"); a) The final size of names is 2; the final size of friends is 3 b) The final size of names is 3; the final size of friends is 2 c) The final size of names is 3; the final size of friends is 3 d) Compilation error

a) The final size of names is 2; the final size of friends is 3

57) Which one of the following statements is the correct definition for a two-dimensional array of 20 rows and 2 columns of the type integer? a) int[][] num = new int[20][2]; b) int[][] num = new int[2][20]; c) int[][] num = new int[20,2]; d) int[][] num = new int[2,20];

a) int[][] num = new int[20][2];

72) Consider the following code snippet: ArrayList<Double> somedata = new ArrayList<Double>(); somedata.add(10.5); What is the size of the array list somedata after the given code snippet is executed? a) 0 b) 1 c) 10 d) 2

b) 1

55) Consider the following code snippet: int cnt = 0; int[][] numarray = new int[2][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { numarray[j][i] = cnt; cnt++; } } What is the value of numarray[1][2] after the code snippet is executed? a) 2 b) 5 c) 3 d) 4

b) 5

85) What is the output of the code snippet below? int[][] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 0, 0, 0, 0 } }; int val = arr[1][2] + arr[1][3]; System.out.println(val); a) 5 b) 6 c) 7 d) 9

b) 6

86) Consider the following code snippet: int[][] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 0, 0, 0, 0 } }; int[][] arr2 = arr; System.out.println(arr2[2][1] + arr2[1][2]); What is the output of the given code snippet on execution? a) 5 b) 6 c) 7 d) 9

b) 6

58) Consider the following code snippet: int[][] numarray = { { 3, 2, 3 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); What is the output of the given code snippet? a) 00 b) 31 c) 30 d) 03

c) 30

83) What is the output of the code snippet below? int[][] numarray = { { 8, 7, 6 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); a) 00 b) 87 c) 80 d) 08

c) 80

63) Consider the following code snippet: int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 } }; int val = arr[0][2] + arr[1][2]; System.out.println(val); What is the output of the given code snippet on execution? a) 5 b) 7 c) 9 d) 10

c) 9

93) What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(1, "Tony"); for (String s : names) { System.out.print(s + ", "); } a) Cal, Bob, Ann b) Ann, Bob c) Ann, Tony d) Cal, Bob, Tony

c) Ann, Tony

61) Consider the following code snippet: int[][] arr = { { 13, 23, 33 }, { 14, 24, 34 } }; Identify the appropriate statement to display the value 24 from the given array? a) System.out.println(arr[1][2]); b) System.out.println(arr[2][2]); c) System.out.println(arr[1][1]); d) System.out.println(arr[2][1]);

c) System.out.println(arr[1][1]);

80) Which one of the following is a correct declaration for a method named passAList that has as arguments an array list myList of size 10 and an integer array intArr of size 20, and that modifies the contents of myList and intArr? a) public static void passAList(ArrayList<Integer> myList(10), int[] intArr) b) public static void passAList(ArrayList<Integer> myList, int[20] intArr) c) public static void passAList(ArrayList<Integer> myList, int[] intArr) d) public static void passAList(ArryaList<Integer> myList, int intArr)

c) public static void passAList(ArrayList<Integer> myList, int[] intArr)

67) Which one of the following is a correct declaration for a method named passAList with the array list num of size 5 as a parameter? a) public static void passAList(ArrayList<Integer> num[5]) b) public static void passAList(ArrayList<Integer> num, 5) c) public static void passAList(ArrayList<Integer> num) d) public static void passAList(num)

c) public static void passAList(ArrayList<Integer> num)

82) How many elements can be stored in a two-dimensional 5 by 6 array? a) 5 b) 6 c) 11 d) 30

d) 30

56) How many elements can be stored in an array of dimension 2 by 3? a) 2 b) 3 c) 5 d) 6

d) 6

70) What is the output of the following code snippet? public static int check(ArrayList<Integer> listData) { int sum = 0; for (int i = 0; i < listData.size(); i++) { sum = sum + listData.get(i); } return sum; } public static void main(String[] args) { ArrayList<Integer> vdata = new ArrayList<Integer>(); int rsum; for (int cnt = 0; cnt < 3; cnt++) { vdata.add(cnt + 1); } rsum = check(vdata); System.out.println(rsum); } a) 4 b) 2 c) 3 d) 6

d) 6

95) What is the output of the following statements? ArrayList<String> cityList = new ArrayList<String>(); cityList.add("London"); cityList.add("New York"); cityList.add("Paris"); cityList.add("Toronto"); cityList.add("Hong Kong"); cityList.add("Singapore"); System.out.print(cityList.size()); System.out.print(" " + cityList.contains("Toronto")); System.out.print(" " + cityList.indexOf("New York")); System.out.println(" " + cityList.isEmpty()); a) 5 true 1 false b) 6 true 2 false c) 5 false 1 false d) 6 true 1 false

d) 6 true 1 false

90) Which statement is true about the code snippet below? public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Double> inputs = new ArrayList<Double>(); int ind = 0; while (in.hasNextDouble()) { inputs.set(ind, in.nextDouble()); ind++; } } a) The code adds all input numbers to the array list b) The code has compile-time error c) The array list is full after 100 numbers are entered d) The code has a run-time error

d) The code has a run-time error

40) What is the value of the count variable after the execution of the given code snippet? ArrayList<Integer> num = new ArrayList<Integer>(); num.add(1); num.add(2); num.add(1); int count = 0; for (int i = 0; i < num.size(); i++) { if (num.get(i) % 2 == 0) { count++; } } a) 1 b) 2 c) 0 d) 3

a) 1

81) What is the value of myArray[1][2] after this code snippet is executed? int count = 0; int[][] myArray = new int[4][5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 4; j++) { myArray[j][i] = count; count++; } } a) 8 b) 9 c) 19 d) 11

b) 9

92) When an integer literal is added to an array list declared as ArrayList<Integer>, Java performs: a) casting b) trimming c) auto-boxing d) auto-fixing

c) auto-boxing

54) Consider the following 2-dimensional array. Select the statement that gives the number of columns in the third row. int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } }; a) int cols = counts[2].size(); b) int cols = counts.length[2]; c) int cols = counts.length; d) int cols = counts[2].length;

d) int cols = counts[2].length;

74) Your program needs to store a sequence of integers of unknown length. Which of the following is most suitable to use? a) An array declared as int[] marks; b) A array list declared as ArrayList<Integer> marks = new ArrayList<Integer>(); c) An array declared as int marks[10000]; d) An array declared as int marks[10];

b) A array list declared as ArrayList<Integer> marks = new ArrayList<Integer>();

39) Which statements are true regarding the differences between arrays and array lists? I. Arrays are better if the size of a collection will not change II. Array lists are more efficient than arrays III. Array lists are easier to use than arrays a) I, II b) I, III c) II, III d) I, II, III

b) I, III

66) Which one of the following statements about declaring an array list as a method parameter is true? a) An array list declared as a method parameter should be accompanied with its size. b) An array list declared as a method parameter is a constant value by default. c) An array list value cannot be modified in a method when the array list is declared as a parameter. d) An array list value can be modified inside the method.

d) An array list value can be modified inside the method.

73) What is the output of the following code snippet? ArrayList<Integer> num; num.add(4); System.out.println(num.size()); a) 1 b) 0 c) 4 d) Error because num is not initialized

d) Error because num is not initialized

94) What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(2, "Tony"); for (String s : names) { System.out.print(s + ", "); } a) Cal, Bob, Ann b) Ann, Bob c) Ann, Cal, Tony d) Array list bound error

d) Array list bound error

89) Consider the following code snippet: public static void main(String[] args) { ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); names.add("Janet"); ArrayList<String> names = reverse(names); } public static ArrayList<String> reverse(ArrayList<String> names) { ArrayList<String> result = new ArrayList<String>(); for (int i = names.size() - 1; i >= 0; i--) { result.add(names.get(i)); } return <String>result; } Which statement is true after the main method is executed? a) names contains "Janet", "Jerry", "John" in this order b) names contains "John", "Jerry", "Janet" in this order c) reverse method has a bound error d) Compilation error due to the return statement in reverse method

d) Compilation error due to the return statement in reverse method

75) Consider the following code snippet: ArrayList<Integer> arrList = new ArrayList<Integer>(); for (int i = 0; i < arrList.size(); i++) { arrList.add(i + 3); } What value is stored in the element of the array list at index 0? a) 0 b) 3 c) 6 d) None

d) None

71) What is the result of the following definition of an array list? ArrayList<Double> points; a) The statement causes a compile time error as the size of the array list is not defined. b) The statement creates an array list of size 0. c) The statement creates an array list with unlimited size. d) The statement defines an array list reference but leaves its value as null.

d) The statement defines an array list reference but leaves its value as null.

79) Which one of the following code snippets accepts the integer input in an array list named num1 and stores the even integers of num1 in another array list named evennum? a) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum.add(num1.get(i)); } } b) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { evennum.add(num1.get(i)); } } c) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { evennum[i] = num1[i]; } } d) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum[i] = num1[i]; } }

a) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum.add(num1.get(i)); } }

68) Consider the following code snippet: public static void check(ArrayList<Integer> chknum1) { int cnt = 0; for(int i = 0; i < chknum1.size(); i++) { if(chknum1.get(i) == 0) { cnt++; } } System.out.println("The total 0 values in the list are: " + cnt); } Which one of the following is true about the check method in the given code snippet? a) The check method counts all the elements with value 0 in an array list passed as a parameter to the method. b) The check method removes all the elements with value 0 from an array list passed as a parameter to the method. c) The check method counts all the elements with value 0 in an array list passed as a parameter to a method and also returns the count. d) The check method adds 0 to the elements of an array list as a parameter to a method and also returns the array list.

a) The check method counts all the elements with value 0 in an array list passed as a parameter to the method.

64) Which one of the following is a valid signature of a method with an integer two-dimensional array parameter of size 10 x 10? a) public static void func(int[][] arr) b) public static void func(int[10][] arr) c) public static void func(int[][10] arr) d) public static void func(int[10][10] arr)

a) public static void func(int[][] arr)

38) The following statement gets an element from position 4 in an array: x = a[4]; What is the equivalent operation using an array list? a) x = a.get(4); b) x = a.get(); c) x = a.get[4]; d) x = a[4];

a) x = a.get(4);

41) Which one of the following code snippets accepts the integer input in an array named num1 and stores the odd integers of num1 in another array named oddnum? a) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { oddnum.add(num1.get(i)); } } b) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { oddnum.add(num1.get(i)); } } c) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { oddnum.add(num1[i]); } } d) ArrayList<Integer> num1; ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1[i] % 2 != 0) { oddnum.add(num1[i]); } }

b) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { oddnum.add(num1.get(i)); } }

91) Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); names.add("Harry"); a) The final size of names is 2; the final size of friends is 3 b) The final size of names is 3; the final size of friends is 2 c) The final size of names is 3; the final size of friends is 3 d) Compilation error

b) The final size of names is 3; the final size of friends is 2

97) Which one of the following is the correct code snippet for calculating the largest value in an integer array list aList? a) int max = 0; for (int count = 1; count < aList.size(); count++) { if (aList.get(count) > max) { max = aList.get(count); } } b) int max = aList.get(0); for (int count = 1; count < aList.size(); count++) { if (aList.get(count) > max) { max = aList.get(count); } } c) int max = aList[1]; for (int count = 1; count < aList.size();count++) { if (aList.get(count) > max) { max = aList.get(count); } } d) int max = aList.get(0); for (int count = 1; count > aList.size();count++) { if (aList.get(count) >= max) { max = aList.get(count); } }

b) int max = aList.get(0); for (int count = 1; count < aList.size(); count++) { if (aList.get(count) > max) { max = aList.get(count); } }

77) Which one of the following is the correct code snippet for calculating the largest value in an integer array list arrList of size 10? a) int maximum = 0; for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } } b) int maximum = arrList.get(0); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } } c) int maximum = arrList.get(1); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } } d) int maximum = arrList.get(0); for (int counter = 1; counter > arrList.size(); counter++) { if (arrList.get(counter) < maximum) { maximum = arrList.get(counter); } }

b) int maximum = arrList.get(0); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } }

59) Which one of the following is the correct definition for initializing data in a two-dimensional array of three rows and two columns? a) int[][] arr = { { 1, 1, 1 }, { 2, 2, 2 }, }; b) int[][] arr = { { 1, 1 }, { 2, 2 }, { 3, 3 } }; c) int[][] arr = { { 1, 1 } { 2, 2 } { 3, 3 } }; d) int[][] arr = { { 1, 1, 1 } { 2, 2, 2 } { 3, 3, 3 } };

b) int[][] arr = { { 1, 1 }, { 2, 2 }, { 3, 3 } };

36) Babbage's machine for automatically producing printed tables was called a) the slide rule. b) the difference engine. c) the mechanical calculator. d) the cog wheel.

b) the difference engine.

98) Consider the following code snippet, where the array lists contain elements that are stored in ascending order: ArrayList<Integer> list1 = new ArrayList<Integer>(); ArrayList<Integer> list2 = new ArrayList<Integer>(); . . . int count = 0; for (int i = 0; i < list1.size() && i < list2.size(); i++) { if (list1.get(i) == list2.get(i)) { count++; } } Which one of the following descriptions is correct for the given code snippet? a) The code snippet finds the highest value out of the two array lists. b) The code snippet finds the lowest value out of the two array lists. c) The code snippet compares the values of two array lists and stores the count of total matches found. d) The code snippet adds the values of the two array lists.

c) The code snippet compares the values of two array lists and stores the count of total matches found.

87) Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = names; friends.add("Harry"); a) The final size of names is 2; the final size of friends is 3 b) The final size of names is 3; the final size of friends is 2 c) The final size of names is 3; the final size of friends is 3 d) Compilation error

c) The final size of names is 3; the final size of friends is 3

62) Consider the following code snippet: int val = arr[0][2]; Which value of arr is stored in the val variable? a) The value in the first row and the second column b) The value in the first row and the first column c) The value in the first row and the third column d) The value in the third row and the second column

c) The value in the first row and the third column

37) Java 7 introduced enhanced syntax for declaring array lists, which is termed a) angle brackets. b) method lists. c) diamond syntax. d) symmetric slants.

c) diamond syntax.

69) Consider the following line of code for calling a method named func1: func1(listData, varData); Which one of the following method headers is valid for func1, where listData is an integer array list and varData is an integer variable? a) public static void func1(int[] listData, int varData) b) public static void func1(ArrayList<Integer> listData, varData) c) public static void func1(ArrayList<Integer> ldata, int vdata) d) public static void func1(int vdata, ArrayList<Integer> ldata)

c) public static void func1(ArrayList<Integer> ldata, int vdata)

65) Consider the following code snippet: ArrayList<Integer> num1 = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { data = in.nextInt(); num1.add(data); if (data == 0 && num1.size() > 3) { num1.remove(num1.size() - 1); } } System.out.println("size is : " + num1.size()); What is the output of the given code snippet if the user enters 1,2,0,0,1 as the input? a) size is : 1 b) size is : 2 c) size is : 4 d) size is : 0

c) size is : 4


Ensembles d'études connexes

Consumer Behavior Exam II Chapter 9

View Set