AP Comp Sci Final 1

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

Consider the following method. public static void sort(String[] arr) { for (int pass = arr.length - 1; pass >= 1; pass--) { String large = arr[0]; int index = 0; for (int k = 0; k <= pass; k++) { if ((arr[k].compareTo(large)) > 0) { large = arr[k]; index = k; } } arr[index] = arr[pass]; arr[pass] = large; } } Assume arr is the following array. "Ann" "Mike" "Walt" "Lisa" "Shari" "Jose" "Mary" "Bill" What is the intermediate value of arr after two iterations of the outer for loop in the call sort(arr)?

"Ann" "Mike" "Bill" "Lisa" "Mary" "Jose" "Shari" "Walt"

Consider the following method. A 5-line code segment reads as follows. Line 1: public static String scramble, open parenthesis, String word comma int how Far, close parenthesis. Line 2: open brace. Line 3: return word, dot, substring, open parenthesis, how Far plus 1 comma word, dot, length, open parenthesis, close parenthesis, close parenthesis, plus. Line 4: word, dot, substring, open parenthesis, 0 comma how Far, close parenthesis, semicolon. Line 5: close brace. What value is returned as a result of the call scramble("compiler", 3)?

"ilercom"

Consider the following code segment. int[][] array2D = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; for (int[] i : array2D) { for (int x : i) { System.out.print(x + " "); } System.out.println(" "); } How many times will the statement System.out.print(x + " ") be executed?

16 times

A pair of number cubes is used in a game of chance. Each number cube has six sides, numbered from 1 to 6, inclusive, and there is an equal probability for each of the numbers to appear on the top side (indicating the cube's value) when the number cube is rolled. The following incomplete statement appears in a program that computes the sum of the values produced by rolling two number cubes. int sum = / * missing code * / ; Which of the following replacements for /* missing code */ would best simulate the value produced as a result of rolling two number cubes?

2 + (int) (Math.random() * 6) + (int) (Math.random() * 6)

Assume mat is defined as follows. int dim = 4; int[][] mat = new int[dim][dim]; Consider the following code segment. int sum = 0; for (int row = 0; row < dim; row++) { sum = sum + mat[row][dim - 1]; } Assume that mat contains the following values before the code segment is executed. Note that mat[0][3] is 2. 0 1 2 3 0 1 1 2 2 1 1 2 2 4 2 1 3 2 6 3 1 4 2 8 What value will sum contain after the code segment is executed?

20

Consider the following code segment. String str = "a black cat sat on a table"; int counter = 0; for (int i = 0; i < str.length() - 1; i++) { if (str.substring(i, i + 1).equals("a") && !str.substring(i + 1, i + 2).equals("b")) { counter++; } } System.out.println(counter); What is printed as a result of executing this code segment?

5

Consider the following two-dimensional array definition. int[][] data = new int[5][10]; Consider the following code segment, where all elements in data have been initialized. for (int j = 0; j < data.length; j++) { for (int k = 0; k < data[0].length; k++) { if (j == k) { System.out.println(data[j][k]); } } } How many times is the println method called when the code segment is executed?

5

Consider the following method.A 5-line code segment reads as follows. Line 1: public void mystery, open parenthesis, int, open square bracket, close square bracket, data, close parenthesis. Line 2: open brace. Line 3: for, open parenthesis, int k equals 0, semicolon, k less than data, dot, length minus 1, semicolon, k, plus, plus, close parenthesis. Line 4: data, open square bracket, k plus 1, close square bracket, equals, data, open square bracket, k, close square bracket, plus, data, open square bracket, k plus 1, close square bracket, semicolon. Line 5: close brace. The following code segment appears in another method in the same class. A 5-line code segment reads as follows. Line 1: int, open square bracket, close square bracket, values, equals, open brace, 5 comma 2 comma 1 comma 3 comma 8, close brace, semicolon. Line 2: mystery, open parenthesis, values, close parenthesis, semicolon. Line 3: for, open parenthesis, int v, colon, values, close parenthesis. Line 4: System, dot, out, dot, print, open parenthesis, v, plus, open double quote, space, close double quote, close parenthesis, semicolon. Line 5: System, dot, out, dot, print l n, open parenthesis, close parenthesis, semicolon. What is printed as a result of executing the code segment?

5 7 8 11 19

Consider the following code segment. int start = 4; int end = 5; boolean keepGoing = true; if (start < end && keepGoing) { if (end > 0) { start += 2; end++; } else { end += 3; } } if (start < end) { if (end == 0) { end += 2; start++; } else { end += 4; } } What is the value of end after the code segment is executed?

6

Consider the following code segment. A 17-line code segment reads as follows. Line 1: int, open square bracket, close square bracket, old Array equals, open brace, 1 comma 2 comma 3 comma 4 comma 5 comma 6 comma 7 comma 8 comma 9, close brace, semicolon. Line 2: int, open square bracket, close square bracket, open square bracket, close square bracket, new Array equals new int, open square bracket, 3, close square bracket, open square bracket, 3, close square bracket, semicolon. Line 3: blank. Line 4: int row equals 0, semicolon. Line 5: int col equals 0, semicolon. Line 6: for, open parenthesis, int value, colon, old Array, close parenthesis. Line 7: open brace. Line 8: new Array, open square bracket, row, close square bracket, open square bracket, col, close square bracket, equals value, semicolon. Line 9: row, plus, plus, semicolon. Line 10: if, open parenthesis, open parenthesis, row modulo 3, close parenthesis, equals, equals, 0, close parenthesis. Line 11: open brace. Line 12: col, plus, plus, semicolon. Line 13: row equals 0, semicolon. Line 14: close brace. Line 15: close brace. Line 16: blank. Line 17: System, dot, out, dot, print l n, open parenthesis, new Array, open square bracket, 0, close square bracket, open square bracket, 2, close square bracket, close parenthesis, semicolon. What is printed as a result of executing the code segment?

7

Consider the following code segment. String str = "CompSci"; System.out.println(str.substring(0, 3)); int num = str.length(); What is the value of num when the code segment is executed?

7

Consider the following method. A 13-line code segment reads as follows. Line 1: public void test, open parenthesis, int x, close parenthesis. Line 2: open brace. Line 3: int y, semicolon. Line 4: blank. Line 5: if, open parenthesis, x, modulo, 2 equals, equals, 0, close parenthesis. Line 6: y equals 3, semicolon. Line 7: else if, open parenthesis, x greater than 9, close parenthesis. Line 8: y equals 5, semicolon. Line 9: else. Line 10: y equals 1, semicolon. Line 11: blank. Line 12: System, dot, out, dot, print l n, open parenthesis, open double quote, y, equals, close double quote, plus, y, close parenthesis, semicolon. Line 13: close brace. Which of the following test data sets would test each possible output for the method?

8, 9, 11

Consider the following code segment. int a = 5; int b = 4; int c = 2; a *= 3; b += a; b /= c; System.out.print(b); What is printed when the code segment is executed?

9

The class Worker is defined below. The class includes the method getEarnings, which is intended to return the total amount earned by the worker. public class Worker { private double hourlyRate; private double hoursWorked; private double earnings; public Worker(double rate, double hours) { hourlyRate = rate; hoursWorked = hours; } private void calculateEarnings() { double earnings = 0.0; earnings += hourlyRate * hoursWorked; } public double getEarnings() { calculateEarnings(); return earnings; } } The following code segment appears in a method in a class other than Worker. The code segment is intended to print the value 800.0, but instead prints a different value because of an error in the Worker class. Worker bob = new Worker(20.0, 40.0); System.out.println(bob.getEarnings()); Which of the following best explains why an incorrect value is printed?

The variable earnings in the calculateEarnings method is a local variable.

Consider the following code segment. if (false && true || false) { if (false || true && false) { System.out.print("First"); } else { System.out.print("Second"); } } if (true || true && false) { System.out.print("Third"); } What is printed as a result of executing the code segment?

Third

Consider the following methods. /** Precondition: a > 0 and b > 0 */ public static int methodOne(int a, int b) { int loopCount = 0; for (int i = 0; i < a / b; i++) { loopCount++; } return loopCount; } /** Precondition: a > 0 and b > 0 */ public static int methodTwo(int a, int b) { int loopCount = 0; int i = 0; while (i < a) { loopCount++; i += b; } return loopCount; } Which of the following best describes the conditions under which methodOne and methodTwo return the same value?

When a % b is equal to zero

Consider the following code segment. Assume num is a properly declared and initialized int variable. if (num > 0) { if (num % 2 == 0) { System.out.println("A"); } else { System.out.println("B"); } } Which of the following best describes the result of executing the code segment?

When num is a positive even integer, "A" is printed; when num is a positive odd integer, "B" is printed; otherwise, nothing is printed.

Consider the following code segment. ArrayList<String> numbers = new ArrayList<String>(); numbers.add("one"); numbers.add("two"); numbers.add(0, "three"); numbers.set(2, "four"); numbers.add("five"); numbers.remove(1); Which of the following represents the contents of numbers after the code segment has been executed?

["three", "four", "five"]

Consider the following code segment. ArrayList<Integer> oldList = new ArrayList(); oldList.add(100); oldList.add(200); oldList.add(300); oldList.add(400); ArrayList<Integer> newList = new ArrayList(); newList.add(oldList.remove(1)); newList.add(oldList.get(2)); System.out.println(newList); What, if anything, is printed as a result of executing the code segment?

[200, 400]

Consider the following method. A 9-line code segment reads as follows: Line 1: public Array List, open angular bracket, Integer, close angular bracket, mystery, open parenthesis, int n, close parenthesis. Line 2: open brace. Line 3: Array List, open angular bracket, Integer, close angular bracket, seq equals new Array List, open angular bracket, Integer, close angular bracket, open parenthesis, close parenthesis, semicolon. Line 4: blank. Line 5: for, open parenthesis, int k equals 1, semicolon, k less than or equal to n, semicolon, k, plus, plus, close parenthesis. Line 6: seq, dot, add, open parenthesis, new Integer, open parenthesis, k, multiplication asterisk, k, plus, 3, close parenthesis, close parenthesis, semicolon. Line 7: blank. Line 8: return seq, semicolon. Line 9: close brace. Which of the following is printed as a result of executing the following statement? System.out.println(mystery ( 6 ) ) ;

[4, 7, 12, 19, 28, 39]

Consider the following code segment. ArrayList<Double> conditionRating = new ArrayList<Double>(); conditionRating.add(9.84); conditionRating.add(8.93); conditionRating.add(7.65); conditionRating.add(6.24); conditionRating.remove(2); conditionRating.set(2, 7.63); System.out.println(conditionRating); What is printed when this code segment is executed?

[9.84, 8.93, 7.63]

Assume that x and y are boolean variables and have been properly initialized. An expression reads as follows: open parenthesis, x, ampersand, ampersand, y, close parenthesis, ampersand, ampersand, exclamatory mark, open parenthesis, x, double vertical bar, y, close parenthesis. Which of the following best describes the result of evaluating the expression above?

false always

Consider the following code segment. for (int k = 1; k <= 7; k += 2) { System.out.print(k); } Which of the following code segments will produce the same output as the code segment above?

for (int k = 1; k <= 8; k += 2) { System.out.print(k); }

Assume that x and y are boolean variables and have been properly initialized. An expression reads as follows: open parenthesis, x, double vertical bar, y, close parenthesis, ampersand, ampersand, x. Which of the following always evaluates to the same value as the expression above?

x

Consider the following Boolean expression in which the int variables x and y have been properly declared and initialized. (x <= 10) == (y > 25) Which of the following values for x and y will result in the expression evaluating to true ?

x = 10 and y = 30

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information. Consider the following sort method. This method correctly sorts the elements of array data into increasing order. A 19-line programming code reads as follows. Line 1: public static void sort, open parenthesis, int, open square bracket, close square bracket, data, close parenthesis. Line 2: open brace. Line 3: for, open parenthesis, int j equals 0, semicolon, j less than data, dot, length minus 1, semicolon, j, plus, plus, close parenthesis. Line 4: open brace. Line 5: int m equals j, semicolon. Line 6: for, open parenthesis, int k equals j plus 1, semicolon, k less than data, dot, length, semicolon, k, plus, plus, close parenthesis. Line 7: open brace. Line 8: if, open parenthesis, data, open square bracket, k, close square bracket, less than data, open square bracket, m, close square bracket, close parenthesis, forward slash, asterisk, Compare values, asterisk, forward slash. Line 9: open brace. Line 10: m equals k, semicolon. Line 11: close brace. Line 12: close brace. Line 13: int temp equals data, open square bracket, m, close square bracket, semicolon, forward slash, asterisk, Assign to temp, asterisk, forward slash. Line 14: data, open square bracket, m, close square bracket, equals data, open square bracket, j, close square bracket, semicolon. Line 15: data, open square bracket, j, close square bracket, equals temp semicolon. Line 16: blank. Line 17: forward slash, asterisk, End of outer loop, asterisk, forward slash. Line 18: close brace. Line 19: close brace. Assume that sort is called with the array {6, 3, 2, 5, 4, 1}. What will the value of data be after three passes of the outer loop (i.e., when j = 2 at the point indicated by / * End of outer loop * /) ?

{1, 2, 3, 5, 4, 6}

Consider the following method. A 10-line code segment reads as follows. Line 1: public static int, open square bracket, close square bracket, operation, open parenthesis, int, open square bracket, close square bracket, open square bracket, close square bracket, matrix comma int r comma int c, close parenthesis. Line 2: open brace. Line 3: int, open square bracket, close square bracket, result equals new int, open square bracket, matrix, dot, length, close square bracket, semicolon. Line 4: blank. Line 5: for, open parenthesis, int j equals 0, semicolon, j less than matrix, dot, length, semicolon, j plus, plus, close parenthesis. Line 6: open brace. Line 7: result, open square bracket, j, close square bracket, equals, matrix, open square bracket, r, close square bracket, open square bracket, j, close square bracket, asterisk, matrix, open square bracket, j, close square bracket, open square bracket, c, close square bracket, semicolon. Line 8: close brace. Line 9: return result, semicolon. Line 10: close brace. The following code segment appears in another method in the same class. A 3-line code segment reads as follows. Line 1: int, open square bracket, close square bracket, open square bracket, close square bracket, mat equals, open brace, open brace, 3 comma 2 comma 1 comma 4, close brace comma open brace, 1 comma 2 comma 3 comma 4, close brace comma open brace, 2 comma 2 comma 1 comma 2, close brace comma open brace, 1 comma 1 comma 1 comma 1, close brace, close brace, semicolon. Line 3: blank. Line 4: int, open square bracket, close square bracket, arr equals operation, open parenthesis, mat comma 1 comma 2, close parenthesis, semicolon. Which of the following represents the contents of arr as a result of executing the code segment?

{1, 6, 3, 4}

Consider the following code segment. A 19-line code segment reads as follows. Line 1: int, open square bracket, close square bracket, open square bracket, close square bracket, mat equals new int, open square bracket, 3, close square bracket, open square bracket, 4, close square bracket, semicolon. Line 2: for, open parenthesis, int row equals 0, semicolon, row less than mat, dot, length, semicolon, row, plus, plus, close parenthesis. Line 3: open brace. Line 4: for, open parenthesis, int col equals 0, semicolon, col less than mat, open square bracket, 0, close square bracket, dot, length, semicolon, col, plus, plus, close parenthesis. Line 5: open brace. Line 6: if, open parenthesis, row less than col, close parenthesis. Line 7: open brace. Line 8: mat, open square bracket, row, close square bracket, open square bracket, col, close square bracket, equals 1, semicolon. Line 9: close brace. Line 10: else if, open parenthesis, row, equals, equals, col, close parenthesis. Line 11: open brace. Line 12: mat, open square bracket, row, close square bracket, open square bracket, col, close square bracket, equals 2, semicolon. Line 13: close brace. Line 14: else. Line 15: open brace. Line 16: mat, open square bracket, row, close square bracket, open square bracket, col, close square bracket, equals 3, semicolon. Line 17: close brace. Line 18: close brace. Line 19: close brace. What are the contents of mat after the code segment has been executed?

{{2,1,1,1} {3,2,1,1} {3,3,2,1}}

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information. Consider the following sort method. This method correctly sorts the elements of array data into increasing order. A 19-line programming code reads as follows. Line 1: public static void sort, open parenthesis, int, open square bracket, close square bracket, data, close parenthesis. Line 2: open brace. Line 3: for, open parenthesis, int j equals 0, semicolon, j less than data, dot, length minus 1, semicolon, j, plus, plus, close parenthesis. Line 4: open brace. Line 5: int m equals j, semicolon. Line 6: for, open parenthesis, int k equals j plus 1, semicolon, k less than data, dot, length, semicolon, k, plus, plus, close parenthesis. Line 7: open brace. Line 8: if, open parenthesis, data, open square bracket, k, close square bracket, less than data, open square bracket, m, close square bracket, close parenthesis, forward slash, asterisk, Compare values, asterisk, forward slash. Line 9: open brace. Line 10: m equals k, semicolon. Line 11: close brace. Line 12: close brace. Line 13: int temp equals data, open square bracket, m, close square bracket, semicolon, forward slash, asterisk, Assign to temp, asterisk, forward slash. Line 14: data, open square bracket, m, close square bracket, equals data, open square bracket, j, close square bracket, semicolon. Line 15: data, open square bracket, j, close square bracket, equals temp semicolon. Line 16: blank. Line 17: forward slash, asterisk, End of outer loop, asterisk, forward slash. Line 18: close brace. Line 19: close brace. Assume that sort is called with the array {1, 2, 3, 4, 5, 6}. How many times will the expression indicated by /* Compare values */ and the statement indicated by /* Assign to temp */ execute?

Compare values / Assign to temp 15 / 5

Consider the following class definition. public class Value { private int num; public int getNum() { return num; } // There may be instance variables, constructors, and methods not shown. } The following method appears in a class other than Value. It is intended to sum all the num instance variables of the Value objects in its ArrayList parameter. /** Precondition: valueList is not null */ public static int getTotal(ArrayList<Value> valueList) { int total = 0; /* missing code */ return total; } Which of the following code segments can replace /* missing code */ so the getTotal method works as intended? for (int x = 0; x < valueList.size(); x++) { total += valueList.get(x).getNum(); } for (Value v : valueList) { total += v.getNum(); } for (Value v : valueList) { total += getNum(v); }

I and II

Consider the problem of finding the maximum value in an array of integers. The following code segments are proposed solutions to the problem. Assume that the variable arr has been defined as an array of int values and has been initialized with one or more values. Three code segments. The first segment has 8 lines of code that read as follows. Line 1: int max equals Integer, dot, MIN, underscore, VALUE, semicolon. Line 2: for, open parenthesis, int value, colon, arr, close parenthesis. Line 3: open brace. Line 4: if, open parenthesis, max less than value, close parenthesis. Line 5: open brace. Line 6: max equals value, semicolon. Line 7: close brace. Line 8: close brace. The second segment has 14 lines of code that read as follows. Line 1: int max equals 0, semicolon. Line 2: boolean first equals true, semicolon. Line 3: for, open parenthesis, int value, colon, arr, close parenthesis. Line 4: open brace. Line 5: if, open parenthesis, first, close parenthesis. Line 6: open brace. Line 7: max equals value, semicolon. Line 8: first equals false, semicolon. Line 9: close brace. Line 10: else if, open parenthesis, max less than value, close parenthesis. Line 11: open brace. Line 12: max equals value, semicolon. Line 13: close brace. Line 14: close brace. The third segment has 8 lines of code that read as follows. Line 1: int max equals arr, open square bracket, 0, close square bracket, semicolon. Line 2: for, open parenthesis, int k equals 1, semicolon, k less than arr, dot, length, semicolon, k, plus, plus, close parenthesis. Line 3: open brace. Line 4: if, open parenthesis, max less than arr, open square bracket, k, close square bracket, close parenthesis. Line 5: open brace. Line 6: max equals arr, open square bracket, k, close square bracket, semicolon. Line 7: close brace. Line 8: close brace. Which of the code segments will always correctly assign the maximum element of the array to the variable max ?

I, II, and III

Consider the following method. // precondition: arr contains no duplicates; // the elements in arr are in sorted order; // 0 ≤ low ≤ arr.length; low - 1 ≤ high < arr.length public static int mystery(int[] arr, int low, int high, int num) { int mid = (low + high) / 2; if (low > high) { return low; } else if (arr[mid] < num) { return mystery(arr, mid + 1, high, num); } else if (arr[mid] > num) { return mystery(arr, low, mid - 1, num); } else // arr{mid] == num { return mid; } } How many calls to mystery (including the initial call) are made as a result of the call mystery(arr, 0, arr.length - 1, 14) if arr is the following array? 0 1 2 3 4 5 6 7 arr 11 13 25 26 29 30 31 32

4

In the following code segment, assume that the ArrayList wordList has been initialized to contain the String values ["apple", "banana", "coconut", "lemon", "orange", "pear"]. int count = 0; for (String word : wordList) { if (word.indexOf("a") >= 0) { count++; } } System.out.println(count); What is printed as a result of executing the code segment?

4

Consider the following code segment. int count = 5; while (count < 100) { count = count * 2; } count = count + 1; What will be the value of count as a result of executing the code segment?

161

Consider the following code segment. double num = 9 / 4; System.out.print(num); System.out.print(" "); System.out.print((int) num); What is printed as a result of executing the code segment?

2.0 2

Consider the following code segment. int[][] values = {{1, 2, 3}, {4, 5, 6}}; int x = 0; for (int j = 0; j < values.length; j++) { for (int k = 0; k < values[0].length; k++) { if (k == 0) { values[j][k] *= 2; } x += values[j][k]; } } What is the value of x after the code segment is executed?

26

Consider the following code segment. int val = 48; int div = 6; while ((val % 2 == 0) && div > 0) { if (val % div == 0) { System.out.print(val + " "); } val /= 2; div--; } What is printed when the code segment is executed?

48 12 6

Assume that mat has been declared as a 4×4 array of integers and has been initialized to contain all 1s. Consider the following code segment. int n = mat.length; for (int j = 1; j < n; j++) { for (int k = 1; k < n; k++) { mat[j][k] = mat[j - 1][k] + mat[j][k - 1]; } } What is the value of mat[2][2] after the code segment has completed execution?

6

Consider the following code segment. int[][] anArray = new int[10][8]; for (int j = 0; j < 8; j++) { for (int k = 0; k < 10; k++) { anArray[j][k] = 5; } } The code segment causes an ArrayIndexOutOfBoundsException to be thrown. How many elements in anArray will be set to 5 before the exception is thrown?

8

Consider the following class definitions. public class ClassA { public String getValue() { return "A"; } public void showValue() { System.out.print(getValue()); } } public class ClassB extends ClassA { public String getValue() { return "B"; } } The following code segment appears in a class other than ClassA or ClassB. ClassA obj = new ClassB(); obj.showValue(); What, if anything, is printed when the code segment is executed?

B

At a certain high school students receive letter grades based on the following scale. Letter grades listed out based on integer score. The data listed are as follows. Integer Score: 93 or above, Letter Grade: A. Integer Score: From 84 to 92 inclusive, Letter Grade: B. Integer Score: From 75 to 83 inclusive, Letter Grade: C. Integer Score: Below 75, Letter Grade: F. Which of the following code segments will assign the correct string to grade for a given integer score ? Three code segments. The first code segment has 8 lines of code that read as follows. Line 1: if, open parenthesis, score greater than or equal to 93, close parenthesis. Line 2: grade equals, open double quote, A, close double quote, semicolon. Line 3: if, open parenthesis, score greater than or equal to 84, ampersand, ampersand, score less than or equal to 92, close parenthesis. Line 4: grade equals, open double quote, B, close double quote, semicolon. Line 5: if, open parenthesis, score greater than or equal to 75, ampersand, ampersand, score less than or equal to 83, close parenthesis. Line 6: grade equals, open double quote, C, close double quote, semicolon. Line 7: if, open parenthesis, score less than 75, close parenthesis. Line 8: grade equals, open double quote, F, close double quote, semicolon. The second code segment has 8 lines of code that read as follows. Line 1: if, open parenthesis, score greater than or equal to 93, close parenthesis. Line 2: grade equals, open double quote, A, close double quote, semicolon. Line 3: if, open parenthesis, 84 less than or equal to score, less than equal to 92, close parenthesis. Line 4: grade equals, open double quote, B, close double quote, semicolon. Line 5: if, open parenthesis, 75 less than or equal to score less than or equal to 83, close parenthesis. Line 6: grade equals, open double quote, C, close double quote, semicolon. Line 7: if, open parenthesis, score less than 75, close parenthesis. Line 8: grade equals, open double quote, F, close double quote, semicolon. The third code segment has 8 lines of code that read as follows. Line 1: if, open parenthesis, score greater than or equal to 93, close parenthesis. Line 2: grade equals, open double quote, A, close double quote, semicolon. Line 3: else if, open parenthesis, score greater than or equal to 84, close parenthesis. Line 4: grade equals, open double quote, B, close double quote, semicolon. Line 5: else if, open parenthesis, score greater than or equal to 75, close parenthesis. Line 6: grade equals, open double quote, C, close double quote, semicolon. Line 7: else. Line 8: grade equals, open double quote, F, close double quote, semicolon.

I and III only

Consider the following code segment. A 6-line code segment reads as follows. Line 1: int x equals 1, semicolon. Line 2: while, open parenthesis, forward slash, asterisk, missing code, asterisk, forward slash, close parenthesis. Line 3: open brace. Line 4: System, dot, out, dot, print, open parenthesis, x, plus, open double quote, space, close double quote, close parenthesis, semicolon. Line 5: x equals x plus 2, semicolon. Line 6: close brace. Consider the following possible replacements for /* missing code */. x < 6 x != 6 x < 7 Which of the proposed replacements for /* missing code */ will cause the code segment to print only the values 1 3 5?

I and III only

1.B 1/1 MC point In the code segment below, assume that the int variable n has been properly declared and initialized. The code segment is intended to print a value that is 1 more than twice the value of n. /* missing code */ System.out.print(result); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? int result = 2 * n; result = result + 1; int result = n + 1; result = result * 2; int result = (n + 1) * 2;

I only

Consider the following code segment. Assume that num3 > num2 > 0. int num1 = 0; int num2 = /* initial value not shown */; int num3 = /* initial value not shown */; while (num2 < num3) { num1 += num2; num2++; } Which of the following best describes the contents of num1 as a result of executing the code segment

The sum of all integers from num2 to num3 - 1, inclusive

Consider the following code segment. ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(new Integer(37)); nums.add(new Integer(3)); nums.add(new Integer(0)); nums.add(1, new Integer(2)); nums.set(0, new Integer(1)); nums.remove(2); System.out.println(nums); What is printed as a result of executing the code segment?

[1, 2, 0]

Consider the following code segment. ArrayList<Integer> numList = new ArrayList<Integer>(); numList.add(3); numList.add(2); numList.add(1); numList.add(1, 0); numList.set(0, 2); System.out.print(numList); What is printed by the code segment?

[2, 0, 2, 1]

Consider the following code segment. ArrayList<String> items = new ArrayList<String>(); items.add("A"); items.add("B"); items.add("C"); items.add(0, "D"); items.remove(3); items.add(0, "E"); System.out.println(items); What is printed as a result of executing the code segment?

[E, D, A, B]

Consider the following code segment. A 9-line code segment reads as follows. Line 1: List, open angular bracket, String, close angular bracket, animals equals new Array List, open angular bracket, String, close angular bracket, open parenthesis, close parenthesis, semicolon. Line 2: blank. Line 3: animals, dot, add, open parenthesis, open double quote, dog, close double quote, close parenthesis, semicolon. Line 4: animals, dot, add, open parenthesis, open double quote, cat, close double quote, close parenthesis, semicolon. Line 5: animals, dot, add, open parenthesis, open double quote, snake, close double quote, close parenthesis, semicolon. Line 6: animals, dot, set, open parenthesis, 2 comma, space, open double quote, lizard, close double quote, close parenthesis, semicolon. Line 7: animals, dot, add, open parenthesis, 1 comma, space, open double quote, fish, close double quote, close parenthesis, semicolon. Line 8: animals, dot, remove, open parenthesis, 3, close parenthesis, semicolon. Line 9: System, dot, out, dot, print l n, open parenthesis, animals, close parenthesis, semicolon. What is printed as a result of executing the code segment?

[dog, fish, cat]

Consider the following code segment. int x = 7; int y = 4; boolean a = false; boolean b = false; if (x > y) { if (x % y >= 3) { a = true; x -= y; } else { x += y; } } if (x < y) { if (y % x >= 3) { b = true; x -= y; } else { x += y; } } What are the values of a, b, and x after the code segment has been executed?

a = true, b = false, x = 7

Consider the following code segment. for (int j = 1; j < 10; j += 2) { System.out.print(j); } Which of the following code segments will produce the same output as the code segment above?

int j = 1; while (j < 10) { System.out.print(j); j += 2; }

Assume that an array of integer values has been declared as follows and has been initialized. int[] arr = new int[10]; Which of the following code segments correctly interchanges the value of arr[0] and arr[5] ?

int k = arr[0]; arr[0] = arr[5]; arr[5] = k;

Which of the following statements assigns a random integer between 25 and 60, inclusive, to rn ?

int rn = (int) (Math.random() * 36) + 25;

Consider the following methods, which appear in the same class. public int function1(int i, int j) { return i + j; } public int function2(int i, int j) { return j - i; } Which of the following statements, if located in a method in the same class, will initialize the variable x to 11?

int x = function1(4, 5) + function2(1, 3);

Consider the following incomplete method that is intended to return an array that contains the contents of its first array parameter followed by the contents of its second array parameter. A 12-line code segment reads as follows. Line 1: public static int, open square bracket, close square bracket, append, open parenthesis, int, open square bracket, close square bracket, a1 comma int, open square bracket, close square bracket, a2, close parenthesis. Line 2: open brace. Line 3: int, open square bracket, close square bracket, result equals new int, open square bracket, a1, dot, length plus a2, dot, length, close square bracket, semicolon. Line 4: blank. Line 5: for, open parenthesis, int j equals 0, semicolon, j less than a1, dot, length, semicolon, j, plus, plus, close parenthesis. Line 6: result, open square bracket, j, close square bracket, equals a1, open square bracket, j, close square bracket, semicolon. Line 7: blank. Line 8: for, open parenthesis, int k equals 0, semicolon, k less than a2, dot, length, semicolon, k, plus, plus, close parenthesis. Line 9: result, open square bracket, forward slash, asterisk, index, asterisk, forward slash, close square bracket, equals a2, open square bracket, k, close square bracket, semicolon. Line 10: blank. Line 11: return result, semicolon. Line 12: close brace. Which of the following expressions can be used to replace /* index */ so that append will work as intended?

k + a1.length

The following incomplete method is intended to sort its array parameter arr in increasing order. // postcondition: arr is sorted in increasing order public static void sortArray(int[] arr) { int j, k; for (j = arr.length - 1; j > 0; j--) { int pos = j; for ( /* missing code */ ) { if (arr[k] > arr[pos]) { pos = k; } } swap(arr, j, pos); } } Assume that swap(arr, j, pos) exchanges the values of arr[j] and arr[pos]. Which of the following could be used to replace /* missing code */ so that executing the code segment sorts the values in array arr?

k = j - 1; k >= 0; k--

Consider the following code segment. for (int x = 0; x <= 4; x++) // Line 1 { for (int y = 0; y < 4; y++) // Line 3 { System.out.print("a"); } System.out.println(); } Which of the following best explains the effect of simultaneously changing x <= 4 to x < 4 in line 1 and y < 4 to y <= 4 in line 3 ?

"a" will be printed the same number of times because while the number of output lines will decrease by 1, the length of each line will increase by 1.

Consider the following code segment, which is intended to assign to num a random integer value between min and max, inclusive. Assume that min and max are integer variables and that the value of max is greater than the value of min. double rn = Math.random(); int num = /* missing code */; Which of the following could be used to replace /* missing code */ so that the code segment works as intended?

(int) (rn * (max - min + 1)) + min

int k = a random number such that 1 ≤ k ≤ n ; for (int p = 2; p <= k; p++) for (int r = 1; r < k; r++) System.out.println("Hello"); What is the maximum number of times that Hello will be printed?

(n - 1)^2

Consider the following recursive method. public static void stars(int num) { if (num == 1) { return; } stars(num - 1); for (int i = 0; i < num; i++) { System.out.print("*"); } System.out.println(); } What is printed as a result of the method call stars(5) ?

** *** **** *****

Consider the following code segment. int val = 1; while (val <= 6) { for (int k = 0; k <= 2; k++) { System.out.println("Surprise!"); } val++; } How many times is the string "Surprise!" printed as a result of executing the code segment?

18

Consider the following correct implementation of the selection sort algorithm. public static void selectionSort(int[] elements) { for (int j = 0; j < elements.length - 1; j++) { int minIndex = j; for (int k = j + 1; k < elements.length; k++) { if (elements[k] < elements[minIndex]) { minIndex = k; } } if (j != minIndex) { int temp = elements[j]; elements[j] = elements[minIndex]; elements[minIndex] = temp; // Line 19 } } } The following declaration and method call appear in a method in the same class as selectionSort. int[] arr = {9, 8, 7, 6, 5}; selectionSort(arr); How many times is the statement elements[minIndex] = temp; in line 19 of the method executed as a result of the call to selectionSort ?

2

Consider the following code segment. String oldStr = "ABCDEF"; String newStr = oldStr.substring(1, 3) + oldStr.substring(4); System.out.println(newStr); What is printed as a result of executing the code segment?

BCEF

Consider the following code segment. String[][] letters = {{"A", "B", "C", "D"}, {"E", "F", "G", "H"}, {"I", "J", "K", "L"}}; for (int col = 1; col < letters[0].length; col++) { for (int row = 1; row < letters.length; row++) { System.out.print(letters[row][col] + " "); } System.out.println(); } What is printed as a result of executing this code segment?

F J G K H L

Consider the following statement. boolean x = (5 < 8) == (5 == 8); What is the value of x after the statement has been executed?

False

Which of the following expressions evaluate to 3.5 ? (double) 2 / 4 + 3 (double) (2 / 4) + 3 (double) (2 / 4 + 3)

I only

Consider the following code segments. I. int k = 1; while (k < 20) { if (k % 3 == 1) System.out.print( k + " "); k = k + 3; } II. for (int k = 1; k < 20; k++) { if (k % 3 == 1) System.out.print( k + " "); } III. for (int k = 1; k < 20; k = k + 3) { System.out.print( k + " "); } Which of the code segments above will produce the following output? 1 4 7 10 13 16 19

I, II, and III

Consider the following incomplete method that is intended to return a string formed by concatenating elements from the parameter words. The elements to be concatenated start with startIndex and continue through the last element of words and should appear in reverse order in the resulting string. An 11-line code segment reads as follows. Line 1: forward slash, asterisk, asterisk, Precondition, colon, words, dot, length greater than 0, semicolon. Line 2: asterisk, start Index greater than or equal to 0. Line 3: asterisk, forward slash. Line 4: public static String concat Words, open parenthesis, String, open square bracket, close square bracket, words comma int start Index, close parenthesis. Line 5: open brace. Line 6: String result equals, open double quote, close double quote, semicolon. Line 7: blank. Line 8: forward slash, asterisk, missing code, asterisk, forward slash. Line 9: blank. Line 10: return result, semicolon. Line 11: close brace. For example, the following code segment uses a call to the concatWords method. A 2-line code segment reads as follows. Line 1: String, open square bracket, close square bracket, things, equals, open brace, open double quote, Bear, close double quote, comma, open double quote, Apple, close double quote, comma, open double quote, Gorilla, close double quote, comma, open double quote, House, close double quote, comma, open double quote, Car, close double quote, close brace, semicolon. Line 2: System, dot, out, dot, print l n, open parenthesis, concat Words, open parenthesis, things comma 2, close parenthesis, close parenthesis, semicolon. When the code segment is executed, the string "CarHouseGorilla" is printed. The following three code segments have been proposed as replacements for / * missing code * /. Three code segments. The first segment has 4 lines of code that read as follows. Line 1: for, open parenthesis, int k equals start Index, semicolon, k less than words, dot, length, semicolon, k, plus, plus, close parenthesis. Line 2: open brace. Line 3: result, plus, equals, words, open square bracket, k, close square bracket, plus words, open square bracket, words, dot, length minus k minus 1, close square bracket, semicolon. Line 4: close brace. The second segment has 6 lines of code that read as follows. Line 1: int k equals words, dot, length minus 1, semicolon. Line 2: while, open parenthesis, k greater than or equal to start Index, close parenthesis. Line 3: open brace. Line 4: result, plus, equals, words, open square bracket, k, close square bracket, semicolon. Line 5: k, minus, minus, semicolon. Line 6: close brace. The third segment has 11 lines of code that read as follows. Line 1: String, open square bracket, close square bracket, temp equals new String, open square bracket, words, dot, length, close square bracket, semicolon. Line 2: for, open parenthesis, int k equals 0, semicolon, k less than equals words, dot, length, forward slash, 2, semicolon, k, plus, plus, close parenthesis. Line 3: open brace. Line 4: temp, open square bracket, k, close square bracket, equals, words, open square bracket, words, dot, length minus k minus 1, close square bracket, semicolon. Line 5: temp, open square bracket, words, dot, length minus k minus 1, close square bracket, equals words, open square bracket, k, close square bracket, semicolon. Line 6: close brace. Line 7: blank. Line 8: for, open parenthesis, int k equals 0, semicolon, k less than temp, dot, length minus start Index, semicolon, k, plus, plus, close parenthesis. Line 9: open brace. Line 10: result plus equals temp, open square bracket, k, close square bracket, semicolon. Line 11: close brace. Which of these code segments can be used to replace /* missing code */ so that concatWords will work as intended?

II and III

Consider the following method, which is intended to calculate and return the expression (x+y)2|a−b|−−−−−√ ( x + y ) 2 | a − b | . public double calculate(double x, double y, double a, double b) { return /* missing code */; } Which of the following can replace /* missing code */ so that the method works as intended?

Math.sqrt(Math.pow(x + y, 2) / Math.abs(a - b))

Consider the following methods, which appear in the same class. public void printSum(int x, double y) { System.out.println(x + y); } public void printProduct(double x, int y) { System.out.println(x * y); } Consider the following code segment, which appears in a method in the same class as printSum and printProduct. int num1 = 5; double num2 = 10.0; printSum(num1, num2); printProduct(num1, num2); What, if anything, is printed as a result of executing the code segment?

Nothing is printed because the code does not compile.

Assume that the boolean variables a and b have been declared and initialized. Consider the following expression. (a && (b || !a)) == a && b Which of the following best describes the conditions under which the expression will evaluate to true?

Only when b is true

Consider the following code segment, which traverses two integer arrays of equal length. If any element of arr1 is smaller than the corresponding (i.e., at the same index) element of minArray, the code segment should replace the element of minArray with the corresponding element of arr1. After the code segment executes, minArray should hold the smaller of the two elements originally found at the same indices in arr1 and minArray and arr1 should remain unchanged. for (int c = 0; c < arr1.length; c++) { if (arr1[c] < minArray[c]) { arr1[c] = minArray[c]; } else { minArray[c] = arr1[c]; } } Which of the following changes will ensure that the code segment always works as intended?

Removing lines 5-8

In the code segment below, assume that the int variables a and b have been properly declared and initialized. int c = a; int d = b; c += 3; d--; double num = c; num /= d; Which of the following best describes the behavior of the code segment?

The code segment stores the value of (a + 3) / (b - 1) in the variable num.

Consider the following code segment, which is intended to print the sum of all elements of an array. int[] arr = {10, 5, 1, 20, 6, 25}; int sum = 0; for (int k = 0; k <= arr.length; k++) { sum += arr[k]; } System.out.println("The sum is " + sum); A runtime error occurs when the code segment is executed. Which of the following changes should be made so that the code segment works as intended?

The for loop header should be replaced with for (int k = 0; k < arr.length; k++).

A two-dimensional array arr is to be created with the following contents. boolean[][] arr = {{false, true, false}, {false, false, true}}; Which of the following code segments can be used to correctly create and initialize arr ?

boolean arr[][] = new boolean[2][3]; arr[0][1] = true; arr[1][2] = true;

Consider the following method. public double myMethod(int a, boolean b) { /* implementation not shown */ } Which of the following lines of code, if located in a method in the same class as myMethod, will compile without error?

double result = myMethod(0, false);

The following method is intended to return true if and only if the parameter val is a multiple of 4 but is not a multiple of 100 unless it is also a multiple of 400. The method does not always work correctly. public boolean isLeapYear(int val) { if ((val % 4) == 0) { return true; } else { return (val % 400) == 0; } } Which of the following method calls will return an incorrect response?

isLeapYear(1900)

Consider the following class definition. public class Tester { private int num1; private int num2; /* missing constructor */ } The following statement appears in a method in a class other than Tester. It is intended to create a new Tester object t with its attributes set to 10 and 20. Tester t = new Tester(10, 20); Which of the following can be used to replace /* missing constructor */ so that the object t is correctly created?

public Tester(int first, int second) { num1 = first; num2 = second; }

Assume that the following variable declarations have been made. double d = Math.random(); double r; Which of the following assigns a value to r from the uniform distribution over the range 0.5 ≤ r < 5.5 ?

r = d * 5.0 + 0.5;

Consider the following code segment. String str = "0"; str += str + 0 + 8; System.out.println(str); What is printed as a result of executing the code segment?

0008

Consider the following code segment. int num = 1; int count = 0; while (num <= 10) { if (num % 2 == 0 && num % 3 == 0) { count++; } num++; } What value is stored in the variable count as a result of executing the code segment?

1

Consider the following code segment. int x = 5; x += 6 * 2; x -= 3 / 2; What value is stored in x after the code segment executes?

16

Assume that the boolean variables a, b, c, and d have been declared and initialized. Consider the following expression. !( !( a && b ) || ( c || !d )) Which of the following is equivalent to the expression?

( a && b ) && ( !c && d )

Consider the following code segment. for (int outer = 0; outer < n; outer++) { for (int inner = 0; inner <= outer; inner++) { System.out.print(outer + " "); } } If n has been declared as an integer with the value 4, what is printed as a result of executing the code segment?

0 1 1 2 2 2 3 3 3 3

Consider the following code segment. int k = 0; while (k < 10) { System.out.print((k % 3) + " "); if ((k % 3) == 0) k = k + 2; else k++; } What is printed as a result of executing the code segment?

0 2 0 2 0 2 0

Consider the following code segment. A 7-line code segment reads as follows. Line 1: for, open parenthesis, int k equals 0, semicolon, k less than 20, semicolon, k equals k plus 2, close parenthesis. Line 2: open brace. Line 3: if, open parenthesis, k modulo 3, equals, equals, 1, close parenthesis. Line 4: open brace. Line 5: System, dot, out, dot, print, open parenthesis, k plus, open double quote, space, close double quote, close parenthesis, semicolon. Line 6: close brace. Line 7: close brace. What is printed as a result of executing the code segment?

4 10 16

Consider the following method, inCommon, which takes two Integer ArrayList parameters. The method returns true if the same integer value appears in both lists at least one time, and false otherwise. public static boolean inCommon(ArrayList<Integer> a, ArrayList<Integer> b) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) // Line 5 { if (a.get(i).equals(b.get(j))) { return true; } } } return false; } Which of the following best explains the impact to the inCommon method when line 5 is replaced by for (int j = b.size() - 1; j > 0; j--) ?

After the change, the method will never check the first element in list b.

Consider the following statement, which is intended to create an ArrayList named theater_club to store elements of type Student. Assume that the Student class has been properly defined and includes a no-parameter constructor. ArrayList<Student> theater_club = new /* missing code */; Which choice can replace /* missing code */ so that the statement compiles without error?

ArrayList<Student>()

Consider the following method. public void conditionalTest(int a, int b) { if ((a > 0) && (b > 0)) { if (a > b) System.out.println("A"); else System.out.println("B"); } else if ((b < 0) || (a < 0)) System.out.println("C"); else System.out.println("D"); } What is printed as a result of the call conditionalTest(3, -2)?

C

Consider the following code segment. System.out.print("One"); // Line 1 System.out.print("Two"); // Line 2 System.out.print("Three"); // Line 3 System.out.print("Four"); // Line 4 The code segment is intended to produce the following output, but does not work as intended. OneTwo ThreeFour Which of the following changes can be made so that the code segment produces the intended output?

Changing print to println in line 2 only

Consider the following code segment. A 3-line code segment reads as follows. Line 1: for, open parenthesis, int k equals 1, semicolon, k less than equals 100, semicolon, k, plus, plus, close parenthesis. Line 2: if, open parenthesis, open parenthesis, k, modulo, 4, close parenthesis, equals, equals 0, close parenthesis. Line 4: System, dot, out, dot, print l n, open parenthesis, k, close parenthesis, semicolon. Which of the following code segments will produce the same output as the code segment above?

for (int k=4; k<= 100; k=k+4 ) System.out.println(k);

Consider the following code segment. ArrayList<String> animals = new ArrayList<>(); animals.add("fox"); animals.add(0, "squirrel"); animals.add("deer"); animals.set(2, "groundhog"); animals.add(1, "mouse"); System.out.println(animals.get(2) + " and " + animals.get(3)); What is printed as a result of executing the code segment?

fox and groundhog

Consider the following method. public void doSomething() { System.out.println("Something has been done"); } Each of the following statements appears in a method in the same class as doSomething. Which of the following statements are valid uses of the method doSomething ? doSomething(); String output = doSomething(); System.out.println(doSomething());

I only

Consider the following method. A 15-line code segment reads as follows. Line 1: forward slash, asterisk, asterisk, Precondition, colon, arr contains only positive values comma dot, comma. Line 2: asterisk, forward slash. Line 3: public static void do Some, open parenthesis, int, open square bracket, close square bracket, arr comma int lim, close parenthesis. Line 4: open brace. Line 5: int v equals 0, semicolon. Line 6: int k equals 0, semicolon. Line 7: while, open parenthesis, k less than arr, dot, length, ampersand, ampersand, arr, open square bracket, k, close square bracket, less than lim, close parenthesis. Line 8: open brace. Line 9: if, open parenthesis, arr, open square bracket, k, close square bracket, greater than v, close parenthesis. Line 10: open brace. Line 11: v equals arr, open square bracket, k, close square bracket, semicolon, forward slash, asterisk, Statement S, asterisk, forward slash. Line 12: close brace. Line 13: k, plus, plus, semicolon, forward slash, asterisk, Statement T, asterisk, forward slash. Line 14: close brace. Line 15: close brace. 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? A table is shown with three columns labeled Value of lim, Executions of Statement S, and Executions of Statement T. Row 1 reads 5, 0, and 5. Row 2 reads 7, 4, and 9. Row 3 reads 3, 5, and 2.

II only

Consider the following code segment. A 6-line code segment reads as follows. Line 1: int sum equals 0, semicolon. Line 2: int k equals 1, semicolon. Line 3: while, open parenthesis, sum less than 12, double vertical bar, k less than 4, close parenthesis. Line 4: sum, plus, equals k, semicolon. Line 5: blank. Line 6: System, dot, out, dot, print l n, open parenthesis, sum, close parenthesis, semicolon. What is printed as a result of executing the code segment?

Nothing is printed due to an infinite loop.

Consider the following method. An 18-line code segment reads as follows. Line 1: forward slash, asterisk, asterisk, Precondition, colon, values has at least one row, asterisk, forward slash. Line 2: public static int calculate, open parenthesis, int, open square bracket, close square bracket, open square bracket, close square bracket, values, close parenthesis. Line 3: open brace. Line 4: int found equals values, open square bracket, 0, close square bracket, open square bracket, 0, close square bracket, semicolon. Line 5: int result equals 0, semicolon. Line 6: for, open parenthesis, int, open square bracket, close square bracket, row, colon, values, close parenthesis. Line 7: open brace. Line 8: for, open parenthesis, int y equals 0, semicolon, y less than row, dot, length, semicolon, y plus, plus, close parenthesis. Line 9: open brace. Line 10: if, open parenthesis, row, open square bracket, y, close square bracket, greater than found, close parenthesis. Line 11: open brace. Line 12: found equals row, open square bracket, y, close square bracket, semicolon. Line 13: result equals y, semicolon. Line 14: close brace. Line 15: close brace. Line 16: close brace. Line 17: return result, semicolon. Line 18: close brace. Which of the following best describes what is returned by the calculate method?

The column index of an element with the largest value in the two-dimensional array

Consider the following code segments, which are each intended to convert grades from a 100-point scale to a 4.0-point scale and print the result. A grade of 90 or above should yield a 4.0, a grade of 80 to 89 should yield a 3.0, a grade of 70 to 79 should yield a 2.0, and any grade lower than 70 should yield a 0.0. Assume that grade is an int variable that has been properly declared and initialized. Code Segment I double points = 0.0; if (grade > 89) { points += 4.0; } else if (grade > 79) { points += 3.0; } else if (grade > 69) { points += 2.0; } else { points += 0.0; } System.out.println(points); Code Segment II double points = 0.0; if (grade > 89) { points += 4.0; } if (grade > 79) { grade += 3.0; } if (grade > 69) { points += 2.0; } if (grade < 70) { points += 0.0; } System.out.println(points); Which of the following statements correctly compares the values printed by the two methods?

The two code segments print the same value only when grade is below 80.

Consider the following code segment. num += num; num *= num; Assume that num has been previously declared and initialized to contain an integer value. Which of the following best describes the behavior of the code segment?

The value of num is the square of twice its original value.

Consider the following class definition. public class FishTank { private double numGallons; private boolean saltWater; public FishTank(double gals, boolean sw) { numGallons = gals; saltWater = sw; } public double getNumGallons() { return numGallons; } public boolean isSaltWater() { if (saltWater) { return "Salt Water"; } else { return "Fresh Water"; } } } Which of the following best explains the reason why the class will not compile?

The value returned by the isSaltWater method is not compatible with the return type of the method.

Consider the following class definition. public class Book { private int pages; public int getPages() { return pages; } // There may be instance variables, constructors, and methods not shown. } The following code segment is intended to store in maxPages the greatest number of pages found in any Book object in the array bookArr. Book[] bookArr = { /* initial values not shown */ }; int maxPages = bookArr[0].getPages(); for (Book b : bookArr) { /* missing code */ } Which of the following can replace /* missing code */ so the code segment works as intended?

if (b.getPages() > maxPages) { maxPages = b.getPages(); }

Which of the following statements assigns a random integer between 1 and 10, inclusive, to rn ?

int rn = (int) (Math.random() * 10) + 1;


Set pelajaran terkait

RN Metabolism 3.0 Case Study Test Part 1 and Part 2

View Set

Lesson 116 - Box Fill and Series Circuits (Master Bedroom) Quiz

View Set

System Analysis and Design (Unit 1: System Analysis Fundamentals, Lesson 3: Systems Development Tools and Lesson 4:)

View Set

Maternal Child Nursing Theory - Chapter 25: Growth and development of the Preschool-aged Child: 3 to 6 years

View Set