AP Computer Science mc

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

Consider the following code segment.List<String> animals = newArrayList<String>();animals.add("dog");animals.add("cat");animals.add("snake");animals.set(2, "lizard");animals.add(1, "fish");animals.remove(3);System.out.println(animals);What is printed as a result of executing the code segment?a. [dog, fish, cat]b. [dog, fish, lizard]c. [dog, lizard, fish]d. [fish, dog, cat]e. The code throws anArrayIndexOutOfBoundsException exception.

a

Consider the following method./* Precondition: arr contains only positive values. /public static void doSome(int[] arr, int lim){int v = 0;int k = 0;while (k < arr.length && arr[k] < lim){if (arr[k] > v){v = arr[k]; / Statement S /}k++; / Statement T /}}Assume that doSome is called and executes without error. Which of the following are possible combinations for the value of lim, the number of times Statement S is executed, and the number of times Statement T is executed?lim: S: T:I. 5 0 5II. 7 4 9III. 3 5 2a. I onlyb. II onlyc. III onlyd. I and III onlye. II and III only

b

onsider the following code segment.for (int k = 0; k< 20; k= k+ 2){if (k % 3== 1) {System.out.print(k+ " "); }}What is printed as a result of executing the code segment?a. 4 16b. 4 10 16c. 0 6 12 18d. 1 4 7 10 13 16 19e. 0 2 4 6 8 10 12 14 16 18

b

Consider the following instance variable and method. private int[] numbers; /* Precondition: numbers contain int values in no particular order. / public int mystery(int num) { for (int k = numbers.length − 1; k >= 0; k−−) { if (numbers[k] < num) { return k; } } return -1; } Which of the following best describes the contents of numbers after the following statement has been executed? int m = mystery(n); a. All values in positions 0 through m are less than n. b. All values in positions m+1 through numbers.length-1 are less than n. c. All values in positions m+1 through numbers.length-1 are greater than or equal to n. d. The smallest value is at position m. e. The largest value that is smaller than n is at position m.

c

Consider the following instance variable nums and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended.For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10], the call findLongest(10) should return 3, the length of the longest consecutive block of 10s.private int[] nums;public int findLongest(int target){int lenCount = 0;int maxLen = 0;Line 1: for (int val : nums)Line 2: {Line 3: if (val == target)Line 4: {Line 5: lenCount++;Line 6: }Line 7: elseLine 8: {Line 9: if (lenCount > maxLen)Line 10: {Line 11: maxLen = lenCount;Line 12: }Line 13: }Line 14: }Line 15: if (lenCount > maxLen)Line 16: {Line 17: maxLen = lenCount;Line 18: }Line 19: return maxLen;}The method findLongest does not work as intended. Which of the following best describes the value returned by a call to findLongest?a. It is the length of the shortest consecutive block of the value target in nums.b. It is the length of the array nums.c. It is the number of occurrences of the value target in nums.d. It is the length of the first consecutive block of the value target in nums.e. It is the length of the last consecutive block of the value target in nums.

c

At a certain high school students receive letter grades based on the following scale.Integer Score --> Letter Grade93 or above --> AFrom 84 to 92 inclusive --> BFrom 75 to 83 inclusive --> CBelow 75 --> FWhich of the following code segments will assign the correct string to grade for a given integer score?I. if (score >= 93)grade = "A";if (score >= 84 && score <= 92)grade = "B";if (score >= 75 && score <= 83)grade = "C";if (score < 75)grade = "F";II. if (score >= 93)grade = "A";if (84 <= score <= 92)grade = "B";if (75 <= score <= 83)grade = "C";if (score < 75)grade = "F";III. if (score >= 93)grade = "A";else if (score >= 84)grade = "B";else if (score >= 75)grade = "C";elsegrade = "F";a. II onlyb. III onlyc. I and II onlyd. I and III onlye. I, II, and III

d

Consider the following method.public static void mystery(List<Integer> nums){for (int k = 0; k < nums.size(); k++){if (nums.get(k).intValue() == 0){nums.remove(k);}}}Assume that a List<Integer> values initially contains the following Integer values.[0, 0, 4, 2, 5, 0, 3, 0]What will values contain as a result of executing mystery(values)?a. [0, 0, 4, 2, 5, 0, 3, 0]b. [4, 2, 5, 3]c. [0, 0, 0, 0, 4, 2, 5, 3]d. [0, 4, 2, 5, 3]e. The code throws an ArrayIndexOutOfBoundsException exception.

d

Consider the following output.1 1 1 1 12 2 2 23 3 34 45Which of the following code segments will produce this output?a. for (int j = 1; j <= 5; j++){for (int k = 1; k <= 5; k++){System.out.print(j + " ");}System.out.println();}b. for (int j = 1; j <= 5; j++){for (int k = 1; k <= j; k++){System.out.print(j + " ");}System.out.println();}c.for (int j = 1; j <= 5; j++){for (int k = 5; k >= 1; k--){System.out.print(j + " ");}System.out.println();}d. for (int j = 1; j <= 5; j++){for (int k = 5; k >= j; k--){System.out.print(j + " ");}System.out.println();}e. for (int j = 1; j <= 5; j++){for (int k = j; k <= 5; k++){System.out.print(k + " ");}System.out.println();}

d


Set pelajaran terkait

Radiographic Procedures II: Unit 1 WB

View Set

Chapter 5: Jesus Christ & the Church

View Set