AP CS Unit-1
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? A. Changing print to println in lines 1 and 2 B. Changing print to println in line 3 C. Interchanging lines 1 and 3 D. Replacing all three lines with the single statement System.out.println("Ready Set Go!"); E. No change is needed; the code segment works correctly as is.
A. Changing print to println in lines 1 and 2
Each of the following code segments is intended to print the word Hello. Which of the following code segments works as intended? System.out.print("Hello"); System.out.print(Hello); System.out.print(He);System.out.print(llo); A. I only B. II only C. III only D. I and II E. II and III
A. I only
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? 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
B. cat dog horse cow
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 B. -2.5 C. -3 D. -3.25 E. Nothing is printed because an int cannot be subtracted from a double.
B. -2.5
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? A. 2 2 B. 2.0 2 C. 2.0 2.0 D. 2.25 2 E. 2.25 2.0
B. 2.0 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.
B. Both the value of x and the value of y have been decreased.
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.
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; II. int bottom = y - x; int top = 2 * x; III. int bottom = y - top; int top = x + y; int bottom = 2 * top; A. I only B. II only C. III only D. I and II E. II and III
B. II only
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? A. The code segment stores the value of (a + 3) / b in the variable num. B. The code segment stores the value of (a + 3) / (b - 1) in the variable num. C. The code segment stores the value of (a + 3) / (b - 2) in the variable num. D. The code segment stores the value of (a + 3) / (1 - b) in the variable num. E. The code segment causes a runtime error in the last line of code because num is type double and d is type int.
B. The code segment stores the value of (a + 3) / (b - 1) in the variable num.
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.
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. 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 B. 3 1.0 C. 3 1.5 D. 3.5 1.5 E. 3.5 1.75
C. 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? A. 1.0 B. 2.5 C. 3.0 D. 7.0 E. 9.0
C. 3.0
Consider the following code segment. int num = 5; num *= 2; num %= 6; What is the value of num after the code segment is executed? A. 1 B. 2 C. 4 D. 6 E. 10
C. 4
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? A. 2 B. 4 C. 9 D. 9.5 E. 19
C. 9
Consider the following code segment. int x = 5; int y = 6; /* missing code */ z = (x + y) / 2; Which of the following can be used to replace /* missing code */ so that the code segment will compile? I. int z = 0; II. int z; III. boolean z = false; A. I only B. II only C. I and II only D. II and III only E. I, II, and III
C. I and II only
Consider the following code segment. int w = 1; int x = w / 2; double y = 3; int z = (int) (x + y); Which of the following best describes the results of compiling the code segment? A. The code segment compiles without error. B. The code segment does not compile, because the int variable x cannot be assigned the result of the operation w / 2. C. The code segment does not compile, because the integer value 3 cannot be assigned to the double variable y. D. The code segment does not compile, because the operands of the addition operator cannot be of different types int and double. E. The code segment does not compile because the result of the addition operation is of type double and cannot be cast to an int.
C. The code segment does not compile, because the integer value 3 cannot be assigned to the double variable y.
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.
A code segment (not shown) is intended to determine the number of players whose average score in a game exceeds 0.5. A player's average score is stored in avgScore, and the number of players who meet the criterion is stored in the variable count. Which of the following pairs of declarations is most appropriate for the code segment described? A. double avgScore; boolean count; B. double avgScore; double count; C. double avgScore; int count; D. int avgScore; boolean count; E. int avgScore; int count;
C. double avgScore; int count;
The code segment below is intended to calculate the circumference c of a circle with the diameter d of 1.5. The circumference of a circle is equal to its diameter times pi. /* missing declarations */ c = pi * d; Which of the following variable declarations are most appropriate to replace /* missing declarations */ in this code segment? A. int pi = 3.14159; int d = 1.5; final int c; B. final int pi = 3.14159; int d = 1.5; int c; C. final double pi = 3.14159; double d = 1.5; double c; D. double pi = 3.14159; double d = 1.5; final double c = 0.0; E. final double pi = 3.14159; final double d = 1.5; final double c = 0.0;
C. final double pi = 3.14159; double d = 1.5; double c;
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. 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? A. 10 -1 B. 10 0 C. 11 -1 D. 11 0 E. 11 1
D. 11 0
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? A. Hello!Howareyou? B. Hello!How are you? C. Hello! How are you? D. Hello!How are you? E. Hello! How are you?
D. Hello!How are you?
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? A. WXYZ B. W XYZ C. WX YZ D. W X YZ E. W X Y Z
D. W X YZ
Consider the following code segment. double x = (int) (5.5 - 2.5); double y = (int) 5.5 - 2.5; System.out.println(x - y); What is printed as a result of executing the code segment? A. -1.0 B. -0.5 C. 0.0 D. 0.5 E. 1.0
D. 0.5
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? A. 1 B. 1.5 C. 3 D. 20 E. 20.5
D. 20
Which of the following arithmetic expressions evaluates to 1 ? I. 2 / 5 % 3 II. 2 / (5 % 3) III. 2 / 5 + 1 A. I only B. II only C. I and II only D. II and III only E. I, II, and III
D. II and III only
Consider the following code segment, which is intended to find the average of two positive integers, x and y. int x; int y; int sum = x + y; double average = (double) (sum / 2); 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. In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for even values of sum. C. In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be greater than the expected result for even values of sum. D. In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum. E. In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be greater than the expected result for odd values of sum.
D. In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum.
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: /* 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? A. The variable x should be declared as an int and the variable y should be declared as a boolean. B. The variable x should be declared as an int and the variable y should be declared as an int. C. The variable x should be declared as a double and the variable y should be declared as an int. D. The variable x should be declared as a double and the variable y should be declared as a boolean. E. The variable x should be declared as a boolean and the variable y should be declared as a boolean.
D. The variable x should be declared as a double and the variable y should be declared as a boolean.
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. double x = 4.5; int y = (int) x * 2; System.out.print(y); What is printed as a result of executing the code segment? A. 8 B. 8.0 C. 9 D. 9.0 E. 10
E. 10
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 B. 2 10 C. 10 8 D. 12 12 E. 12 20
E. 12 20
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 B. 1 C. 2 D. 3 E. 4
E. 4
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? A. Inserting System.out.print(); between lines 1 and 2 B. Inserting System.out.println(); between lines 1 and 2 C. Changing println in line 1 to print D. Changing print in line 2 to println E. Enclosing hello in line 1 and world in line 2 in quotation marks
E. Enclosing hello in line 1 and world in line 2 in quotation marks
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; II. double volume = baseArea * h; double volume = pi * r * r; III. volume = volume * h; double volume = pi * r * r * h; A. I only B. III only C. I and III only D. II and III only E. I, II, and III
E. I, II, and III
The following code segment is intended to round val to the nearest integer and print the result. double val = -0.7; int roundedVal = (int) (val + 0.5); System.out.println(roundedVal); Which of the following best describes the behavior of the code segment? A. The code segment works as intended. B. The code segment does not work as intended because val and roundedVal should be declared as the same data type. C. The code segment does not work as intended because the expression (val + 0.5) should be cast to a double instead of an int. D. The code segment does not work as intended because val should be cast to an int before 0.5 is added to it. E. The code segment does not work as intended because the expression (int) (val + 0.5) rounds to the nearest integer only when val is positive.
E. The code segment does not work as intended because the expression (int) (val + 0.5) rounds to the nearest integer only when val is positive.