AP CS unit 1 MCQ part A and B

Ace your homework & exams now with Quizwiz!

Consider the following code segment. int j = 12; 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?

14 22

Consider the following code segment. int num = 4; num *= 2; num %= 6; What is the value of num after the code segment is executed?

2

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

3

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

a) Hello! How are you?

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.5 in avg because 17.5 is the result of the floating point division of 35.0 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 does not compile because int values cannot be added to double values. d) The code segment does not compile because a double value cannot be divided by an int value. e) The code segment stores 17 in avg because 17 is the result of the integer division of 35 by 2.

a) 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 6.0. double fact1 = 1 / 2.0; 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.

a) There are no errors and the code works as intended.

Consider the following code segment, which is intended to display 0.5. int num1 = 0.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.

a) There is no error and the code works as intended.

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 + 1.5)); What is printed as a result of executing the code segment? a) 11 1 b) 10 1 c)11 0 d) 11 0 e) 10 1

b) 10 1

Consider the following code segment. double d = 0.25; int i = 4; double diff = d - i; System.out.print((int)diff - 0.5); What is printed as a result of executing the code segment? a) -4.25 b) 4.25 c) -3.5 d) 3.5 e) 3.75

c) -3.5

Consider the following code segment. System.out.print("hello"); // Line 1 System.out.println("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? a) Inserting System.out.print(); between lines 1 and 2 b) Inserting System.out.println(); between lines 1 and 2 c) Changing print in line 1 to println d) Changing println in line 2 to print e) Enclosing hello in line 1 and world in line 2 in quotation marks

c) Changing print in line 1 to println

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 (x / y) + 2 d) It sets z to (1 / y) + 2 e) It sets z to (x + 2) / y

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

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

c) 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 segment. double a = 9; 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.5 b) 4 1.5 c) 3 2 d) 4 2 e) 4 3

d) 4 2

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

d) The value of x has been decreased and the value of y has been increased.

Consider the following code segment. System.out.print("W"); System.out.println("X"); System.out.println("Y"); System.out.print("Z"); What is printed as a result of executing the code segment? a) WXYZ b) W XYZ c) WX Y Z d) W X YZ e) WX YZ

d) W X YZ

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

d) cat dog horse cow


Related study sets

fluid and electrolyte pre test questions

View Set

Anatomy ch. 5 bone remodeling steps

View Set

WebAssign Chapter 1 - Conceptual Questions

View Set

History Study Guide/ First Semester

View Set

dunya dovletleri 2 dunya muharibesi dim

View Set