AP Computer Science

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Consider the following code segment. int x = 0; x++; x += 1; x = x + 1; x -= -1; System.out.println(x); What is printed when the code segment has been executed?

4

int num = 5; num *= 2; num %= 6; What is the value of num after the code segment is executed?

4

What is printed as a result of executing the following statement? System.out.println( 404/10*10+1);

401

Consider the following code segment. int a = 100; while (a > 1) { System.out.println("$"); a /= 2; } How many $'s are printed as a result of executing the code segment?

6

Consider the following code segment. int outerMax = 10; int innerMax = 5; for (int outer = 0; outer < outerMax; outer++) { for (int inner = 0; inner <= innerMax; inner++) { System.out.println(outer + inner); } } How many values will be printed when the code segment is executed?

60

Consider the following code segment. int quant = 20; int unitPrice = 4; int ship = 8; int total; if (quant > 10) { unitPrice = 3; } if (quant > 20) { ship = 0; } total = quant * unitPrice + ship; What is the value of total after this code segment has been executed?

68

Consider the following code segment. int k = 35; while (k >= 0) { System.out.println("X"); k -= 5; } How many times will the string "X" be printed as a result of executing the code segment?

8

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 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. I. int max = Integer.MIN_VALUE for(int value : arr) { if (max<value) { max=value; } } II. int max=0 boolean first = true; for (int value : arr) { if (first) { max=value; first=false; } else if (max<value) { max=value; } } III. int max = arr[0]; for (int k=1; k<arr.length; k++) { if (max<arr[k]) { max = arr[k]; } } 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 two code segments. Assume that variables x and y have been declared as int variables and have been assigned integer values. I. int result = 0; if (x > y) { result = x - y; System.out.print(result); } else if (x < y) { result = y - x; System.out.print(result); } else { System.out.print(result); } II. if (x < y) { System.out.print(y - x); } else { System.out.print(x - y); } Which of the following correctly compares the outputs of the two code segments? a. Code segment I and code segment II produce the same output for all values of x and y. b. Code segment I and code segment II produce the same output only when x is equal to y. c. Code segment I and code segment II produce the same output only when x is not equal to y. d. Code segment I and code segment II produce the same output only when x is less than y. e. Code segment I and code segment II do not produce the same output for any values of x and y.

a. Code segment I and code segment II produce the same output for all values of x and y.

Consider the following code segment, which is intended to display 6.0. double fact1 = 1 / 2; double fact2 = 3 * 4; double product = fact1 * fact2; System.out.println(product); Which of the following best describes the error, if any, in the code segment? a. There are no errors and the code works as intended. b. Either the numerator or the denominator of the fraction 1 / 2 should be cast as double. c. The expression fact1 * fact 2 should be cast as double. d. The expressions 1 / 2 and 3 * 4 should both be cast as double. e. The variables fact1 and fact2 should both be declared as int.

b. Either the numerator or the denominator of the fraction 1 / 2 should be cast as double.

Which statement correctly declares a variable that can store a temperature rounded to the nearest tenth of a degree? a. boolean patientTemp; b. double patientTemp; c. int patientTemp; d. patientTemp = 0; e. patientTemp = 0.0;

b. double patientTemp;

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? a.int j = 1; while (j < 10) { j += 2; System.out.print(j); } b. int j = 1; while (j < 10) { System.out.print(j); j += 2; } c. int j = 1; while (j <= 10) { j += 2; System.out.print(j); } D int j = 1; d. int j = 1; while (j >= 10) { j += 2; System.out.print(j); } e. int j = 1; while (j >= 10) { System.out.print(j); j += 2; }

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

Consider the following code segment. String first = new String("duck"); String second = new String("duck"); String third = new String("goose"); if (first == second) { System.out.print("A"); } else if (second == third) { System.out.print("B"); } else if (first.equals(second)) { System.out.print("C"); } else if (second.equals(third)) { System.out.print("D"); } else { System.out.print("E"); } What is printed as a result of executing the code segment?

c

In the following expression, sunny and windy are properly declared and initialized boolean variables. !sunny && !windy Which of the following is equivalent to the expression above? a. sunny || windy b. !sunny || !windy c. !(sunny || windy) d. !(sunny && windy) e. !(sunny && !windy)

c. !(sunny || windy)

A teacher determines student percentages in a course as the points a student earns divided by the total points available in the grading period. Points are awarded only in whole number increments, but student percentages are to be stored as decimals. The following code segment appears in a program used to compute student percentages. Points that a student earns are stored in pointsEarned, the total points available in the grading period are stored in totalPoints, and the student percentage is stored in percentage. int pointsEarned; /* missing code */ Which of the following is most appropriate to replace /* missing code */ in the program? a. int totalPoints; int percentage; b. double totalPoints; int percentage; c. int totalPoints; double percentage; d. int totalPoints; boolean percentage; e. double totalPoints; boolean percentage;

c. int totalPoints; double percentage;

Consider the following code segment. int x = /* initial value not shown */; int y = /* initial value not shown */; int z = x; z /= y; z += 2; Which of the following best describes the behavior of the code segment? a. It sets z to 2. b. It sets z to x. c. It sets z to (1 / y) + 2. d. It sets z to (x / y) + 2. e. It sets z to (x + 2) / y.

d. It sets z to (x / y) + 2.

Consider the following code segment. int x; int y; x = 3; y = /* missing expression */; x = 1 + 2 * y; System.out.print(x); System.out.println(y); Which of the following can be used as a replacement for /* missing expression */ so that the code segment prints 94 ? a. 3 b. x c. x-1 d. x+1 e. 1-2*x

d. x+1

Consider the following code segment, which uses properly declared and initialized int variables x and y and the String variable result. String result = ""; if (x < 5) { if (y > 0) { result += "a"; } else { result += "b"; } } else if (x > 10) { if (y < 0) { result += "c"; } else if (y < 10) { result += "d"; } result += "e"; } result += "f"; What is the value of result after the code segment is executed if x has the value 15 and y has the value 5 ?

def

Assume that the int variables a, b, c, and low have been properly declared and initialized. The code segment below is intended to print the sum of the greatest two of the three values but does not work in some cases. if (a > b && b > c) { low = c; } if (a > b && c > b) { low = b; } else { low = a; } System.out.println(a + b + c - low); For which of the following values of a, b, and c does the code segment NOT print the correct value? a. a = 1, b = 1, c = 2 b. a = 1, b = 2, c = 1 c. a = 1, b = 2, c = 3 d. a = 2, b = 2, c = 2 e. a = 3, b = 2, c = 1

e. a = 3, b = 2, c = 1

Consider the following code segment. boolean a = true; boolean b = true; System.out.print((b || (!a || b)) + " "); System.out.print(((!b || !a) && a) + " "); System.out.println(!(a && b) && b); What output is produced when this code segment is executed?

true false false

Consider the following code segment. int x = 3; int y = -1; if (x - 2 > y) { x -= y; } if (y + 3 >= x) { y += x; } System.out.print("x = " + x + " y = " + y); What is printed as a result of the execution of the code segment?

x = 4 y = 3

The volume of a cylinder is equal to the height times the area of the circular base. The area of the circular base is equal to π (pi) times the square of the radius. The code segment below is intended to compute and print the volume of a cylinder with radius r and height h. Assume that the double variables r, h, and pi have been properly declared and initialized. /* missing code */ System.out.print(volume); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? I. double baseArea = pi * r * r;double volume = baseArea * h; II. double volume = pi * r * r;volume = volume * h; III. double volume = pi * r * r * h;

I, II, and III

In the code segment below, the int variable temp represents a temperature in degrees Fahrenheit. The code segment is intended to print a string based on the value of temp. The following table shows the string that should be printed for different temperature ranges. Temp Range String to Print 31 and below "cold" 32-50 "cool" 51-70 "moderate" 71 and above "warm" String weather; if (temp <= 31) { weather = "cold"; } else { weather = "cool"; } if (temp >= 51) { weather = "moderate"; } else { weather = "warm"; } System.out.print(weather); Which of the following test cases can be used to show that the code does NOT work as intended? I. temp = 30 II. temp = 51 III. temp = 60

II and III only

Which of the following arithmetic expressions evaluates to 1 ? 2 / 5 % 3 2 / (5 % 3) 2 / 5 + 1

II and III only

Consider the following incomplete code segment, which is intended to print the sum of the digits in num. For example, when num is 12345, the code segment should print 15, which represents the sum 1 + 2 + 3 + 4 + 5. int num = 12345; int sum = 0; /* missing loop header */ { sum += num % 10; num /= 10; } System.out.println(sum); Which of the following should replace /* missing loop header */ so that the code segment will work as intended? a. while (num > 0) b. while (num >= 0) c. while (num > 1) d. while (num > 2) e. while (num > sum)

a. while (num > 0)

In the following expression, j, k, and m are properly declared and initialized int variables. !((j == k) && (k > m)) Which of the following is equivalent to the expression above a. (j != k) || (k < m) b. (j != k) || (k <= m) c. (j == k) || (k < m) d. (j != k) && (k <= m) e. (j == k) && (k < m)

b. (j != k) || (k <= m)

In the following expression, sweet, salty, and sour are properly declared and initialized boolean variables. sweet && (salty || sour) Which of the following expressions is equivalent to the expression above? a. (sweet && salty) || sour b. (sweet && salty) || (sweet && sour) c. (sweet && salty) && (sweet && sour) d. (sweet || salty) && sour e. (sweet || salty) && (sweet || sour)

b. (sweet && salty) || (sweet && sour)

Consider the following code segment. int x = 4; int y = 6; x -= y; y += x; Which of the following best describes the behavior of the code segment? a. The value of x and the value of y have not been changed. b. Both the value of x and the value of y have been decreased. c. Both the value of x and the value of y have been increased. d. The value of x has been decreased and the value of y has been increased. e. The value of x has been increased and the value of y has been decreased.

b. Both the value of x and the value of y have been decreased.

A store sells rope only in whole-foot increments. Given three lengths of rope, in feet, the following code segment is intended to display the minimum length of rope, in feet, that must be purchased so that the rope is long enough to be cut into the three lengths. For example, for lengths of 2.8 feet, 3 feet, and 5 feet, the minimum length of rope that must be purchased is 11 feet. For these inputs, the code segment should display 11. As another example, for lengths of 1.1 feet, 3.2 feet, and 2 feet, the minimum length of rope that must be purchased is 7 feet. For these inputs, the code segment should display 7. double len1; double len2; double len3; double total = len1 + len2 + len3; int minLength = (int) (total + 0.5); System.out.print(minLength); Which of the following best describes the behavior of the code segment? a. The code segment works as intended for all nonnegative values of len1, len2, and len3. b. The code segment works as intended but only when the sum of the three lengths is an integer or the decimal part of the sum of the three lengths is greater than or equal to 0.5. c. The code segment does not work as intended for some values of len1, len2, and len3. It could be corrected by casting len1, len2, and len3 to int before adding them together and assigning the sum to minLength. d. The code segment does not work as intended for some values of len1, len2, and len3. It could be corrected by declaring minLength as a double and casting it to an int in the System.out.print statement. e. The code segment does not work as intended for any values of len1, len2, and len3. It returns the sum of 0.5 and the three lengths for all values of len1, len2, and len3.

b. The code segment works as intended but only when the sum of the three lengths is an integer or the decimal part of the sum of the three lengths is greater than or equal to 0.5.

Consider the following code segments, which differ only in their loop header. Code Segment I for (int i = 0; i < 10; i++) { System.out.print( "*" ); } Code Segment II for (int i = 1; i <= 10; i++) { System.out.print( "*" ); } Which of the following best explains how the difference in the two loop headers affects the output? a. The output of the code segments is the same because the loops in each code segment terminate when i is 10. b. The output of the code segments is the same because the loops in both code segments iterate 10 times. c. The output of the code segments is different because code segment I iterates from i = 0 to i = 9 and code segment II iterates from i = 1 to i = 10. d. The output of the code segments is different because code segment I iterates from i = 0 to i = 10 and code segment II iterates from i = 1 to i = 11. e. Neither code segment produces output because both loop conditions are initially false.

b. The output of the code segments is the same because the loops in both code segments iterate 10 times.

Consider the following code segment, which is intended to calculate the average of two quiz scores. double avg = 15 + 20; avg /= 2; Which of the following best describes the behavior of the code segment? a. The code segment stores 17 in avg because 17 is the result of the integer division of 35 by 2. b. The code segment stores 18 in avg because 18 is the result of the integer division of 35 by 2. c. The code segment stores 17.5 in avg because 17.5 is the result of the floating point division of 35.0 by 2. d. The code segment does not compile because int values cannot be added to double values. e. The code segment does not compile because a double value cannot be divided by an int value.

c. The code segment stores 17.5 in avg because 17.5 is the result of the floating point division of 35.0 by 2.

Consider the following code segment, which is intended to display 0.5. int num1 = 5; int num2 = 10; double ans = num1 / num2; System.out.print(ans); Which of the following best describes the error, if any, in the code segment? a. There is no error and the code works as intended. b. The code should have cast the expression num1 / num2 to double. c. The code should have cast either num1 or num2 in the expression num1 / num2 to double. d. The code should have declared ans as an int. e. The code should have initialized num1 to 5.0 and num2 to 10.0.

c. The code should have cast either num1 or num2 in the expression num1 / num2 to double.

Consider the following code segment. int n = 6; for (int i = 1; i < n; i = i + 2) // Line 2 { System.out.print(i + " "); } Which of the following best explains how changing i < n to i <= n in line 2 will change the result? a. An additional value will be printed because the for loop will iterate one additional time. b. One fewer value will be printed because the for loop will iterate one fewer time. c. There will be no change to the program output because the loop will iterate the same number of times. d. An infinite loop will occur because the loop condition will never be false. e. The body of the loop will not execute at all because the loop condition will initially be false.

c. There will be no change to the program output because the loop will iterate the same number of times.

Consider the following code segment. int a = 1; while (a <= 2) { int c = 1; while (/* missing condition */) { System.out.print("*"); c++; } a++; } The code segment is intended to print "******". Which of the following can be used to replace /* missing condition */ so that the code segment works as intended? a. c <= 2 b. c < 3 c. c <= 3 d. c > 2 e. c >= 3

c. c <= 3

Consider the following two code segments where the int variable choice has been properly declared and initialized. Code Segment A if (choice > 10) { System.out.println("blue"); } else if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } Code Segment B if (choice > 10) { System.out.println("blue"); } if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } Assume that both code segments initialize choice to the same integer value. Which of the following best describes the conditions on the initial value of the variable choice that will cause the two code segments to produce different output? a. choice < 5 b. choice >= 5 and choice <= 10 c. choice > 10 d. choice == 5 or choice == 10 e. There is no value for choice that will cause the two code segments to produce different output.

c. choice > 10

Consider the following method. public String mystery(String word, int num) { String result = ""; for (int k = num; k >= 0; k--) { result += word.substring(0, k); } return result; } What is returned as a result of the call mystery("computer", 3) ?

comcoc

Consider the following code segment. String temp = "comp"; System.out.print(temp.substring(0) + " " + temp.substring(1) + " " + temp.substring(2) + " " + temp.substring(3)); What is printed when the code segment is executed?

comp omp mp p

Consider the following code segment, where k and count are properly declared and initialized int variables. k++; k++; count++; k--; count++; k--; Which of the following best describes the behavior of the code segment? a. The code segment leaves both k and count unchanged. b. The code segment increases both k and count by 2. c. The code segment increases k by 4 and count by 2. d. The code segment leaves k unchanged and increases count by 2. e. The code segment increases k by 2 and leaves count unchanged.

d. The code segment leaves k unchanged and increases count by 2.

Consider the following variable declarations and initializations. int a = 2; int b = 6; int c = 3; Which of the following expressions evaluates to false ? a. a < b == c < b b. a > b == b < c c. a < b != b < c d. a < b != c < b e. a > b != b > c

d. a < b != c < b

Consider the following code segment. for (int k = 0; k < 4; k++) { /* missing loop header */ { System.out.print(k); } System.out.println(); } The code segment is intended to produce the following output. 0 11 222 3333 Which of the following can be used to replace /* missing loop header */ so that the code segment will work as intended? a. for (int h = 0; h < k; h++) b. for (int h = 1; h < k + 1; h++) c. for (int h = 0; h < 3; h++) d. for (int h = k; h >= 0; h--) e. for (int h = k; h <= 0; h--)

d. for (int h = k; h >= 0; h--)

Consider the following code segment. int count = 0; for (int k = 0; k < 10; k++) { count++; } System.out.println(count); Which of the following code segments will produce the same output as the code segment above? a. int count = 0; for (int k = 1; k < 10; k++) { count++; } System.out.println(count); b. int count = 1; for (int k = 1; k <= 10; k++) { count++; } System.out.println(count); c. int count = 1; for (int k = 0; k <= 9; k++) { count++; } System.out.println(count); d. int count = 0; for (int k = 9; k >= 0; k--) { count++; } System.out.println(count); e. int count = 0; for (int k = 10; k >= 0; k--) { count++; } System.out.println(count);

d. int count = 0; for (int k = 9; k >= 0; k--) { count++; } System.out.println(count);

Which of the following statements assigns a random integer between 1 and 10, inclusive, to rn ? a. int rn = (int) (Math.random()) * 10; b. int rn = (int) (Math.random()) * 10 + 1; c. int rn = (int) (Math.random() * 10); d. int rn = (int) (Math.random() * 10) + 1; e. int rn = (int) (Math.random() + 1) * 10;

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

Consider the following code segment, which is intended to set the Boolean variable inRange to true if the integer value num is greater than min value and less than max value. Otherwise inRange is set to false. Assume that inRange, num, min, and max have been properly declared and initialized. boolean isBigger; boolean isSmaller; boolean inRange; if (num < max) { isSmaller = true; } else { isSmaller = false; } if (num > min) { isBigger = true; } else { isBigger = false; } if (isBigger == isSmaller) { inRange = true; } else { inRange = false; } Which of the following values of num, min, and max can be used to show that the code does NOT work as intended? a. num = 20, min = 30, max = 50 b. num = 30, min = 20, max = 40 c. num = 40, min = 10, max = 40 d. num = 50, min = 50, max = 50 e. num = 60, min = 40, max = 50

d. num = 50, min = 50, max = 50

Consider the following code segment, which is intended to print the sum of all the odd integers from 0 up to and including 101. int r = 0; int sum = 0; /* missing loop header */ { if (r % 2 == 1) { sum += r; } r++; } System.out.println(sum); Which of the following could replace /* missing loop header */ to ensure that the code segment will work as intended? a. while (r <= 100) b. while (sum <= 100) c. while (r < 101) d. while (r <= 101) e. while (sum <= 101)

d. while (r <= 101)

Consider the following code segment, which is intended to store the sum of all multiples of 10 between 10 and 100, inclusive (10 + 20 + ... + 100), in the variable total. int x = 100; int total = 0; while( /* missing code */ ) { total = total + x; x = x - 10; } Which of the following can be used as a replacement for /* missing code */ so that the code segment works as intended? a. x < 100 b. x <= 100 c. x > 10 d. x >= 10 e. x != 10

d. x >= 10

Consider the following code segment in which the int variables a and b have been properly declared and initialized. if (a < b) { a++; } else if (b < a) { b++; } else { a++; b++; } Which of the following code segments is equivalent to the code segment above? a. if (a <= b + 1) { a++; } else if (b <= a - 1) { b++; } else { a++; b++; } b. if (a + 1 <= b) { a++; } else if (b - 1 <= a) { b++; } else { a++; b++; } c. if (a - b < 0) { a++; } else if (b - a > 0) { b++; } else { a++; b++; } d. if (a != b) { a++; } else if (b != a) { b++; } else { a++; b++; } e. if (a == b) { a++; b++; } else if (a < b) { a++; } else { b++; }

e. if (a == b) { a++; b++; } else if (a < b) { a++; } else { b++; }

Consider the following two code segments, which are both intended to determine the longest of the three strings "pea", "pear", and "pearl" that occur in String str. For example, if str has the value "the pear in the bowl", the code segments should both print "pear" and if str has the value "the pea and the pearl", the code segments should both print "pearl". Assume that str contains at least one instance of "pea". I. if (str.indexOf("pea") >= 0) { System.out.println("pea"); } else if (str.indexOf("pear") >= 0) { System.out.println("pear"); } else if (str.indexOf("pearl") >= 0) { System.out.println("pearl"); } II. if (str.indexOf("pearl") >= 0) { System.out.println("pearl"); } else if (str.indexOf("pear") >= 0) { System.out.println("pear"); } else if (str.indexOf("pea") >= 0) { System.out.println("pea"); } Which of the following best describes the output produced by code segment I and code segment II? a. Both code segment I and code segment II produce correct output for all values of str. b. Neither code segment I nor code segment II produce correct output for all values of str. c. Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pear" but not "pearl". d. Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pearl". e. Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pea" but not "pear".

e. Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pea" but not "pear".

Consider the following code segment. for (int i = 0; i < 5; i++) // Line 1 { for (int j = 0; j < 5; j++) { int k = i + j; System.out.print(k + " "); } } Which of the following best describes the result of changing i < 5 to i > 5 in line 1? a. The numbers will be printed in the reverse order as they were in the original code segment because the outer loop will occur in reverse order. b. Five additional values will be printed because the outer for loop will iterate one additional time. c. An infinite loop will occur because the termination condition of the loop will never be reached. d. There will be no change to the program output. e. Nothing will be printed because the body of the outer for loop will not execute at all.

e. Nothing will be printed because the body of the outer for loop will not execute at all.

Consider the following code segment. for (int j = 0; j < 4; j++) { for (int k = 0; k < j; k++) { System.out.println("hello"); } } Which of the following best explains how changing the inner for loop header to for (int k = j; k < 4; k++) will affect the output of the code segment? a. The output of the code segment will be unchanged. b. The string "hello" will be printed three fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop. c. The string "hello" will be printed four fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop. d. The string "hello" will be printed three additional times because the inner loop will iterate one additional time for each iteration of the outer loop. e. The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

e. The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

Consider the following code segment. int total = 0; for (int k = 0; k <= 100; k += 2) { total += k; } Which of the following for loops could be used to replace the for loop in the original code segment so that the original and the revised code segments store the same value in total? a. for (int k = 0; k < 100; k += 2) { total += k + 1; } b. for (int k = 1; k < 101; k += 2) { total += k - 1; } c. for (int k = 0; k <= 101; k += 2) { total += k + 1; } d. for (int k = 1; k <= 101; k += 2) { total += k + 1; } e. for (int k = 1; k <= 101; k += 2) { total += k - 1; }

e. for (int k = 1; k <= 101; k += 2) { total += k - 1; }

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? a. for (int k = 0; k < 7; k += 2) { System.out.print(k); } b. for (int k = 0; k <= 7; k += 2) { System.out.print(k); } c. for (int k = 0; k <= 8; k += 2) { System.out.print(k + 1); } d. for (int k = 1; k < 7; k += 2) { System.out.print(k + 1); } e. for (int k = 1; k <= 8; k += 2) { System.out.print(k); }

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

Consider the following code segment. int a = 1; String result = ""; while (a < 20) { result += a; a += 5; } System.out.println(result); What, if anything, is printed as a result of executing the code segment?

161116

Consider the following code segment. double d = 0.25; int i = 3; double diff = d - i; System.out.print((int)diff - 0.5); What is printed as a result of executing the code segment?

-2.5

Consider the following code segment. String str = "AP-CSA"; for (int i = 0; i < str.length(); i++) { if (str.substring(i, i + 1).equals("A")) { System.out.print(i + " "); } } What is printed as a result of executing the code segment?

0 5

Consider the following code segment. double p = 10.6; double n = -0.2; System.out.println((int) (p + 0.5)); System.out.print((int) (n - 0.5)); What is printed as a result of executing the code segment?

11 0

Consider the following code segment. int m = 8; int n = 3; if (m + n > 10) { System.out.print(m + n); } if (m - n > 0) { System.out.print(m - n); } What, if anything, is printed as a result of executing the code segment?

115

Consider the following code segment. int j = 10; int k = 8; j += 2; k += j; System.out.print(j); System.out.print(" "); System.out.println(k); What is printed when the code segment is executed?

12 20

Consider the following code segment. int a = 4; int b = 5; a++; b++; int c = a + b; a -= 1; System.out.println(a + c); What is printed when the code segment is executed?

15

Consider the following code segment. int j = 1; while (j <= 5) { for (int k = 4; k > 1; k--) { System.out.println("ha"); // line 6 } j++; } How many times will the print statement on line 6 execute?

15

Consider the following method. public static int mystery(String string1, String string2) { String temp = string1; int position = 0; int result = 0; while(temp.indexOf(string2) >= 0) { position = temp.indexOf(string2); result++; temp = temp.substring(position + 1); } return result; } The following code segment appears in another method in the same class. System.out.println(mystery("Mississippi", "si")); What, if anything, is printed as a result of executing the code segment?

2

Consider the code segment below. int x = 10; int y = 20; System.out.print(y + x / y); What is printed as a result of executing the code segment?

20

Consider the following code segment. int x = 7; if (x < 7) { x = 2 * x; } if (x % 3 == 1) { x = x + 2; } System.out.print(3 * x); What is printed as a result of executing the code segment?

27

Consider the following code segment. double a = 7; int b = (int) (a / 2); double c = (double) b / 2; System.out.print(b); System.out.print(" "); System.out.print(c); What is printed as a result of executing the code segment?

3 1.5

Consider the following code segment. int a = 1; int b = 2; int c = 3; int d = 4; double x = a + b * c % d; What is the value of x when the code segment has been executed?

3.0

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 code segment. int count = 0; int number = 20; while (number > 0) { number = number / 2; count++; } What will be the value of count after executing the code segment?

5

The following code segment prints one or more characters based on the values of boolean variables b1 and b2. Assume that b1 and b2 have been properly declared and initialized. if (!b1 || b2) { System.out.print("A"); } else { System.out.print("B"); } if (!(b1 || b2)) { System.out.print("C"); } else { System.out.print("D"); } if (b1 && !b1) { System.out.print("E"); } If b1 and b2 are both initialized to true, what is printed when the code segment has finished executing?

AD

Consider the following code segment. System.out.print("Ready"); // Line 1 System.out.print("Set"); // Line 2 System.out.print("Go!"); // Line 3 The code segment is intended to produce the following output but may not work as intended. Ready Set Go! Which change, if any, can be made so that the code segment produces the intended output?

Changing print to println in lines 1 and 2

Consider the following code segment. System.out.println(hello); // Line 1 System.out.print(world); // Line 2 The code segment is intended to produce the following output but does not work as intended. hello world Which of the following changes can be made so that the code segment produces the intended output?

Enclosing hello in line 1 and world in line 2 in quotation marks

Consider the following code segment. System.out.print("Hello!"); System.out.println("How "); System.out.print("are "); System.out.print("you?"); What is printed as a result of executing the code segment?

Hello!How are you?

Consider the following while loop. Assume that the int variable k has been properly declared and initialized. while (k < 0) { System.out.print("*"); k++; } Which of the following ranges of initial values for k will guarantee that at least one "*" character is printed? I. k < 0 II. k = 0 III. k > 0

I only

Each of the following code segments is intended to print the word Hello. Which of the following code segments works as intended? I. System.out.print("Hello"); II. System.out.print(Hello); III. System.out.print(He);System.out.print(llo);

I only

Consider the following code segment. int x = 10; int y = 20; /* missing code */ System.out.print(top / bottom); Which of the following replacements for /* missing code */ will cause an ArithmeticException to occur? I. int top = x - y;int bottom = y - x; II. int top = 2 * x;int bottom = y - top; III. int top = x + y;int bottom = 2 * top;

II only

Consider the method digitSum below, which takes a positive integer parameter as input. public int digitSum(int num) { int total = 0; while (num > 0) { total += num % 10; num /= 10; } return total; } Which of the following code segments could replace the while loop in the method digitSum without changing the value returned by the method? I. for (int h = 0; h < num; h++) { total += num % 10; num /= 10; } II. for (int j = num; j > 0; j--) { total += j % 10; } III. for (int k = num; k > 0; k /= 10) { total += k % 10; }

III only

Which of the following statements stores the value 3 in x ? a. int x = 4 / 7; b. int x = 7 / 3; c. int x = 7 / 4; d. int x = 5 % 8; e. int x = 8 % 5;

e. int x = 8 % 5;

Consider the following code segment: /* data type 1 */ x = 0.5; /* data type 2 */ y = true; Which of the following best describes the data types that should be used to replace/* data type 1 */ and /* data type 2 */ so that the code segment compiles without error?

The variable x should be declared as a double and the variable y should be declared as a boolean.

Consider the following code segment. System.out.println("W"); System.out.println("X"); System.out.print("Y"); System.out.print("Z"); What is printed as a result of executing the code segment?

W X YZ

Consider the following code segment. int a = 1; int b = 0; int c = -1; if ((b + 1) == a) { b++; c += b; } if (c == a) { a--; b = 4; } What are the values of a, b, and c after this code segment has been executed?

a = 1, b = 1, and c = 0

Consider the following code segment. System.out.print("cat "); System.out.println("dog "); System.out.println("horse "); System.out.print("cow "); What is printed as a result of executing the code segment?

cat dog horse cow

Consider the following code segment. boolean a = true; boolean b = false; System.out.print((a == !b) != false); What is printed as a result of executing this code segment?

true

Consider the following code segment. int a = 10; int b = 5 * 2; System.out.print(a == b); What is printed as a result of executing the code segment?

true

Consider the following expression. (3 + 4 == 5) != (3 + 4 >= 5) What value, if any, does the expression evaluate to?

true

Consider the following code segment. String myString = new String("my string"); String yourString = new String(); yourString = "my string"; boolean dotEquals = myString.equals(yourString); boolean equalsEquals = (myString == yourString); System.out.print(dotEquals + " " + equalsEquals); What is printed as a result of executing the code segment?

true false

Consider the following code segment. String str1 = new String("Happy"); String str2 = new String("Happy"); System.out.print(str1.equals(str2) + " "); System.out.print(str2.equals(str1) + " "); System.out.print(str1 == str2); What is printed as a result of executing the code segment?

true true false


संबंधित स्टडी सेट्स

Chapter 67: Management of Patients With Cerebrovascular Disorders

View Set

CCHS 9th Literature/Comp , CCHS GA-Foundations of Algebra , CCHS Health and Personal Fitness ( 1 full credit) , CCHS Introduction to Business and Technology, 6/21 (FIRST)

View Set

PrepU & Picmonic: CH 29 Nonmalignant Hematologic Disorders

View Set

Principles of Marketing Final Exam

View Set

Chapter 4 - Radio Frequency Components, Measurements, and Mathematics

View Set

The Kinetic Molecular Theory of Matter

View Set

33 Diuretics, 34 Antihyperlipidemic Drugs, 37 Anticoagulant and Thrombolytic Drugs, 38 Cardiotonic and Inotropic Drugs, 39 Antiarrhythmic Drugs

View Set

Modules 4 - 7: Ethernet Concepts Exam

View Set