SG_Unit1ProgressCheckMCQPartB_.pdf
Document Details
Uploaded by ResilientBigfoot
AP
Tags
Full Transcript
AP COMPUTER SCIENCE A Scoring Guide Unit 1 Progress Check: MCQ Part B 1. Consider the following code segment. int j = 10; int k = 8; j += 2; k += j; System.ou...
AP COMPUTER SCIENCE A Scoring Guide Unit 1 Progress Check: MCQ Part B 1. 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? (A) 2 2 (A) 2 2 (B) 2 10 (B) 2 10 (C) 10 8 (C) 10 8 (D) 12 12 (D) 12 12 (E) (E) 12 2012 20 2. 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? (A) 0 (A) 0 (B) 1 (B) 1 (C) 2 (C) 2 (D) 3 (D) 3 (E) 4 (E) 4 3. Consider the following code segment. int num = 5; num *= 2; num %= 6; (A) 1 What is the value of num after the code segment is executed? (B) 2 (C) 4 (D) 6 (E) 10 AP Computer Science A Page 1 of 5 Scoring Guide Unit 1 Progress Check: MCQ Part B (A) 1 (B) 2 (C) 4 (D) 6 (E) 10 4. Consider the following code segment. int x = ; int y = ; int z = x; z /= y; z += 2; Which of the following best describes the behavior of the code segment? (A) ItIt (A) sets z to 2. sets z to 2. (B) ItIt (B) sets to x. zsets z to x. (C) (C) ItIt sets zsets z y)to+ to (1 / (1 / y) + 2. 2. (D) (D) ItIt sets zsets z y)to+ to (x / (x / y) + 2. 2. (E) (E) ItIt sets zsets z 2)to/ to (x + (x + 2) / y. y. 5. 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. (A) The code segment stores 17 in avg because 17 is the result of the integer division of 35 by 2. (B) The codecode The segment stores segment stores18 17.5 in avginbecause 18 is the avg because result 17.5 is theofresult the of integer division the floating pointof 35 by 2. division (C)(C) The code segment stores 17.5 in avg because 17.5 is the result of the floating point division of 35.0 of by 35.0 2. by 2. (D) The code segment does not compile because int values cannot be assigned to double values. (E)(D) The The code segment does not compile because int values cannot be assigned to double values. code segment does not compile because a double value cannot be divided by an int value. (E) The code segment does not compile because a double value cannot be divided by an int value. 6. 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? Page 2 of 5 AP Computer Science A Scoring Guide Unit 1 Progress Check: MCQ Part B (A) The value (A) The valueof of x andandthethe valuevalue of not of y have y have not been changed. been changed. (B) Both the value of x and the value of y have been decreased. (C) (B) Both Both thethevalue value of of x and the value x and theofvalue y haveofbeenydecreased. have been increased. (D) The value of x has been decreased and the value of y has been increased. (E) (C) Both The the value value of xof hasx and the value been of y haveand increased been the increased. value of y has been decreased. (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. 7. 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? (A) -2 (A) -2 (B) (B) -2.5 -2.5 (C) -3 -3 (C) -3.25 (D) (E) (D) Nothing -3.25 is printed because an int cannot be subtracted from a double. (E) Nothing is printed because an int cannot be subtracted from a double. 8. 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? (A) 3 1 (A) 3 1 (B) 3 1.0 (B) 3 1.0 (C) 3 1.5 (C) 3 1.5 (D) (D) 3.5 3.5 1.5 1.5 (E) (E) 3.5 3.5 1.75 1.75 AP Computer Science A Page 3 of 5 Scoring Guide Unit 1 Progress Check: MCQ Part B 9. 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? 10 (A) 10 (A) -1 -1 10 (B) 10 (B) 0 0 11 (C) 11 (C) -1 -1 11 (D) 11 (D) 0 0 11 (E) 11 (E) 1 1 10. 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. (A) There is no error and the code works as intended. The code (B) The (B) code should shouldhave cast have the cast expression thenum1 / num2 to expression double. num1 / num2 to double. (C) (C) The code should have cast either num1 or num2 in the expressioninnum1 The code should have cast either num1 or num2 the /expression num1 / num2 to double. num2 to double. (D) (D) The The code should code should have declared have declared ans as an ans int. as an int. (E) The code should have initialized num1 to 5.0 and num2 to 10.0. (E) The code should have initialized num1 to 5.0 and num2 to 10.0. Page 4 of 5 AP Computer Science A Scoring Guide Unit 1 Progress Check: MCQ Part B 11. 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? The The (A) (A) codecode segment segment worksasas intended works intended forfor all nonnegative values of all nonnegative len1, and len1,oflen2, values len2,len3. and len3. (B) The code segment does not work as intended for some values of len1, len2, and len3. The The code segment does not work as intended for some values of len1, len2, and len3. The code segment code segment (B) works only works when the sum of the three lengths is an integer or the decimal works (B) part ofonlythe works sumwhen the sum of the three lengths is an integer or the decimal part of the sum of the of the three lengths is greater three lengths is greater than or than or equal equalto to 0.5.0.5. (C) The The codecode segment segment does doesnot work as not work asintended intended for some for some valuesvalues of len1,of len1, len2, len2, and len3. and len3. It It could could be corrected by casting len1, len2, and len3 to int before adding them together and (C) be corrected by casting len1, len2, and len3 to int before adding them together and assigning assigning the sum to minLength. (D) The the codesumsegment to minLength. does not work as intended for some values of len1, len2, and len3. It could beThecorrected code segmentdeclaring by minLength does not work as intendedasfor a some double and ofcasting values len1, it to an len2, andint in the len3. It could System.out.print statement. (D) be corrected by declaring minLength as a double and casting it to (E) The code segment does not work as intended for any values of len1, len2, and len3. Itan int in the returns System.out.print the sum of 0.5 and the statement. three lengths for all values of len1, len2, and len3. The code segment does not work as intended for any values of len1, len2, and len3. It returns (E) the sum of 0.5 and the three lengths for all values of len1, len2, and len3. 12. 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. (A) There are no errors and the code works as intended. (B) (B) Either Eitherthe numerator the numerator or the or the denominator denominator of the fraction of 1 / the fraction 2 should be cast as1 double. / 2 should be cast as double. (C) (C) The The expression fact1 **fact2 expression fact1 fact2should should beas cast be cast as double. double. (D) The expressions 1 / 2 and 3 * 4 should both be cast as double. The The (E) (D) expressions 1 / 2 and 3 * 4 should both be cast as double. variables fact1 and fact2 should both be declared as int. (E) The variables fact1 and fact2 should both be declared as int. AP Computer Science A Page 5 of 5