APCS Chapter 7 - Cont.

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

76) 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

48) Why is the following method header invalid? public static int[5] meth(int[] arr, int[] num) a) Methods that return an array cannot specify its size b) Methods cannot have an array as a return type c) Methods cannot have an array as a parameter variable d) Methods cannot have two array parameter variables

a) Methods that return an array cannot specify its size

82) 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]);

55) 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];

22) It may be necessary to &quot;grow&quot; an array when reading inputs because a) the number of inputs may not be known in advance. b) arrays in Java must be resized after every 100 elements. c) arrays are based on powers of two. d) the only alternative is a bounds exception.

a) the number of inputs may not be known in advance.

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

53) 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

84) 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

72) Your program needs to store an integer sequence 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>();

56) 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

108) What will be printed by the statements below? int[] values = { 4, 5, 6, 7}; for (int i = 1; i < values.length; i++) values[i] = values[i - 1] + values[i]; for (int i = 0; i < values.length; i++) System.out.print (values[i] + " "); a)4 9 11 13 b)4 9 15 22 c)9 11 13 7 d)4 5 6 7

b)4 9 15 22

81) 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

61) 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

67) 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)

78) 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)

65) 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)

100) What will be printed by the statements below? int[] values = { 1, 2, 3, 4}; values[2] = 24; values[values[0]] = 86; for (int i = 0; i < values.length; i++) System.out.print (values[i] + " "); a)86 2 24 4 b)86 24 3 4 c)1 86 24 4 d)1 2 24 86

c)1 86 24 4

68) 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

49) What is the output of the following code snippet? public static void main(String[] args) { String[] arr = { "aaa", "bbb, "ccc" }; mystery(arr); System.out.println(arr[0] + " " + arr.length); } public static void mystery(String[] arr) { arr = new String[5]; arr[0] = &quot;ddd&quot;; } a) ddd 5 b) ddd 3 c) aaa 5 d) aaa 3

d) aaa 3

88) 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

51) Select the statement that reveals the logic error in the following method. public static double minimum(double[] data) { double smallest = 0.0; for (int i = 0; i < data.length; i++) { if (data[i] < smallest) { smallest = data[i]; } } return smallest; } a) double m = minimum(new double[] { 1.2, -3.5, 6.6, 2.4 }); b) double m = minimum(new double[] { 1.2, 23.5, 16.6, -23.4 }); c) double m = minimum(new double[] { -10.2, 3.5, 62.6, 21.4 }); d) double m = minimum(new double[] { 12.2, 31.5, 6.6, 2.4 });

d) double m = minimum(new double[] { 12.2, 31.5, 6.6, 2.4 });

101) What will be printed by the statements below? int[] values = { 10, 20, 30, 7}; int[] numbers = values; numbers[2] = 24; values[3] = numbers[0] + 6; System.out.println (numbers[2] + " " + numbers[3] + " " + values[2] + " " + values[3]); a)24 0 30 16 b)24 0 30 6 c)24 7 30 16 d)24 16 24 16

d)24 16 24 16

106) Assume the array of integers values has been created. Which condition must be used in the indicated area so the loop below will assign max the largest value in values? int max = values[0]; for (int current = 1; current < values.length; current++) { if (/* Put condition here */) max = values[current]; } a)current > max b)max > current c)max > values[current] d)values[current] > max

d)values[current] > max

66) 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.

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

83) 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

63) 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

107) What will be printed by the statements below? int[] values = { 10, 20, 30, 40}; for (int i = 1; i < values.length; i++) values[i] = values[i - 1]; for (int i = 0; i < values.length; i++) System.out.print (values[i] + " "); a)10 9 8 7 b)10 19 29 39 c)10 10 10 10 d)9 19 29 39

c)10 10 10 10

87) 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> names2 = 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


Set pelajaran terkait

Ch 16. Socioemotional Development in Late Adulthood

View Set

Statistics Module 5: Chapter 9-10

View Set

100 Books Everyone Should Read (in no particular order)

View Set

French How to ask how someone is doing and responses

View Set

E.1 Analyze the development of informational passages: set 1

View Set