AP Computer Science A Unit 5: Loops Test
Consider the following program segment. int count = 0; int p = 1; int n = //200, 400 and 800//; while (p < n){ count++; p = p * 2; } System.out.println(count); Analyze this program and determine the value of count with n equals 200, 400 and 800. Which of the following shows the correct values for count? (A)8 9 10 (B) 8 16 32 (C) 8 10 12 (D) 8 64 4096 (E)8 32 128
A
Consider the following program segment. int p = 21; while ((p % 2 == 0) || (p % 3 == 0)) { System.out.print(p + " "); p = p / 2; } What is printed when the program is executed? (A) 21 10 (B) 10 7 3 (C) 21 14 8 3 (D) 16 12 9 5 (E) ArithmeticException
A
Consider the following program segment. String s1 = "A poet I am not, don't claim to be, but what I wrote did come from me"; String s2 = ","; int n = s1.length(); int index = 0; int items = 0; for (int k = 0; k < n; k++) { String temp = s1.substring(k,k+1); if (temp.equals(s2)) items++; } System.out.println(items); What is printed when the program is executed? (A) 2 (B) 3 (C) 4 (D) 5 (E) 6
A
Consider the following program segment. for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++) for (int z = 0; z < 2; z++) System.out.print("# "); What is printed when the program is executed? (A) # # # # # # # # (B) ######## (C) # # # # # # (D) ###### (E) ##
A
Consider the following program segment. int count = 0; for (double k = 1.0; k < 10.0; k+=0.5); { count++; System.out.print(count + " "); } What is printed when the program is executed? (A) 1 (B) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18 (C) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18 19 20 (D) 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 (E) 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0
A
Consider the following program segment. Scanner input = new Scanner(System.in); System.out.print("Please enter your age ==> "); int age = input.nextInt(); while ((age >= 16) || (age <= 100)) { System.out.print("Please Re-enter ==> "); age = input.nextInt(); } System.out.println("Proceed to take you driver test"); The intent of the program segment is to give a driver test only to people in the [16..100] age range. The program segment should repeat the age request in case the the age is entered erroneously. Which of the following describes the manner of this program's execution? (A) The program is correct; it only accepts people in the proper age range of [16..100]. (B) The program is incorrect; it asks everybody to re-enter in a continuous loop. (C) The program is incorrect; it accepts everybody regardless of age. (D) The program is incorrect; it rejects everybody regardless of age. (E) The program is incorrect; it never enters the loop body.
B
Consider the following program segment. String str1 = "I live at 811 Fleming Trail, Richardson, TX 75081"; String str2 = ""; int n = str1.length(); for (int k = 0; k < n; k++) { String temp = str1.substring(k,k+1); boolean greaterThan = temp.compareTo("A") > 0; boolean lessThan = temp.compareTo("Z") < 0; if (greaterThan && lessThan) str2 = str2 + temp; } System.out.println(str2); What is printed when the program is executed? (A) iftrtx (B) IFTRTX (C) Ila8FTRT7 (D) No output (E) Runtime exception
B
Consider the following program segment. int n = 5; int count = 0; int p, q; for (p = 1; p <= n; p++) { for (q = 1; q <= n-p; q++) { count++; } } System.out.println(count); What is printed when the program is executed? (A) 6 (B) 8 (C) 10 (D) 20 (E) 25
B
Consider the following program segment. int p = 2; int q = 0; while (q < 10) { q += p; p ++; System.out.println(p + " " + q); } What is the last output when the program is executed? (A) 5 9 (B) 6 14 (C) 14 6 (D) 9 16 (E) 7 11
B
Consider the following program segment. int p = 5; int q = 6; int count = 0; for (int x = 1; x <= p; x++) for (int y = 1; y <= q; y++) count++; System.out.println(count); Which of the following statement describes the relationship between variables p, q and count? (A) count equals p + q (B) count equals p * q (C) count equals (p-1) * (q-1) (D) count equals pq (p raised to the power of q) (E) count equals (p + 1) * (p + 1)
B
Consider the following program segment. int x = 105; int y = 42; int rem = 1; int mystery = 1; while (rem != 0) { rem = x % y; if (rem == 0) { mystery = y; } else { x = y; y = rem; } } System.out.println(mystery); This program segment implements an algorithm to compute a mathematical value, called mystery in the program. Which of the following algorithms is implemented? (A) Euclid's LCM Algorithm (Least Common Multiple) (B) Euclid's GCF Algorithm (Greatest Common Factor) (C) Plato's Unity Algorithm (D) Dijksta's Shortest Path Algorithm (E) Descartes's Matrix Algorithm
B
Consider the following program segment and program output. int hi = 1600; int lo = 400; int range = hi - lo + 1; for (int k = 1; k <= 100; k++) { int sat = (int) (Math.random() * range + lo); System.out.print(sat + " "); if (k % 15 == 0) System.out.println(); } OUTPUT: 775 1344 1105 1407 1073 573 838 945 1578 797 424 1329 418 419 1152 1013 689 738 448 1459 847 1424 795 673 768 474 869 618 1543 1595 1034 419 1564 712 1491 595 1227 1387 659 1368 899 860 1060 1453 474 1436 1104 1277 1041 1163 756 759 1102 786 452 1025 1509 490 1294 883 633 1037 1434 616 489 1346 864 1152 585 480 1210 1288 932 1557 1480 1234 768 1075 625 615 894 1592 1541 1554 1328 924 1336 1378 781 1559 783 1284 947 487 1481 1274 484 651 840 730 The intention of the program is to create an algorithm that displays random integers in a [lo..hi] range. In this program example output lo equals 400 and hi equals 1600 for a random SAT range of [400..1600]. A loop follows that displays 100 integers to check the program's accuracy. Which of the following statements is correct about this random program algorithm? I. Every random value generated is in the [400..1600] required range. II. There is no indication that proves the program incorrect. III. The program code and output proves that the algorithm is correctly implemented. (A) I only (B) III only (C) I and II only (D) II and III only (E) I, II and III.
C
Consider the following program segment. String str1 = "MISSISSIPPI"; String str2 = "I"; int p = 0; int q = 0; while (p < str1.length()) { String x = str1.substring(p,p+1); if (x.equals(str2)) { q++; } p++; } System.out.println(q); What type of algorithm is implemented by the program segment? (A) It is an algorithm to find the location of substring str2 in str1. (B) It is an algorithm, to find the location of substring str2 in str1. (C) It is an algorithm to count the occurrences of substring str2 in str1. (D) It is an algorithm to count the occurrences of substring str1 in str2. (E) It is an algorithm, that replaces specified locations in str1 with str2
C
Consider the following program segment. int count = 0; int p = 1; int q = 1; while (p <= 5) { count++; while (q <= 5) { count++; q++; } p++; } System.out.print(count); What is printed when the program is executed? (A) 16 (B) 25 (C) 10 (D) 8 (E) 5
C
Consider the following program segment. int n = 7; int count = 3; int t1 = 1; int t2 = 1; int t3 = 1; while (count <= n) { t3 = t1 + t2; t1 = t2; t2 = t3; count++; } System.out.println(t3); What is printed when the program is executed? (A) 5 (B) 8 (C) 13 (D) 21 (E) 34
C
Consider the following program segment. int p = 1; int sum = 0; while (p <= 5) { sum += p; p++; } double x = (double) sum / p; System.out.println(x); What is printed when the program is executed? (A) 15 (B) 5 (C) 2.5 (D) 2.0 (E) 3.5
C
Consider the following program segment. int x = 0; int y = 0; while (x < 4 || y < 7) { x++; y++; } System.out.println(x + y); What is printed when the program is executed? ( A) 12 (B) 4 (C) 14 (D) 7 (E) 16
C
Consider the following program segment, which looks for the index location of substring s2 in string s1. String s1 = "important"; String s2 = "port"; int n1 = s1.length(); int n2 = s2.length(); int index = 0; boolean match = false; while (/* Missing Code */ ) { String temp = s1.substring(index,index+s2.length()); match = s2.equals(temp); if (!match) index++; } if (match) System.out.println(s2 + " found at index " + index); else System.out.println(s2 + " is not found"); Which while heading must replace /* Missing Code */ for the program to execute correctly? (A) (index < n1 && !match) (B) (index < n1 || !match) (C) (index <= n1 && !match) (D) (index < n1 && index <= n1-n2 && !match) (E) (index < n1 || index || n1-n2 || !match)
D
Consider the following program segment. int count = 1; for (int p = 1; p <= 5; p++) { for (int q = 1; q <= 5; q++) { count++; } System.out.print(count + " "); } What is printed when the program is executed? (A) 1 6 11 16 21 (B) 0 5 10 15 20 (C) 5 10 15 20 25 (D) 6 11 16 21 26 (E) 4 9 14 19 24
D
Consider the following program segment. int n = 10; for (int k = 0; k <= 6; k++) { if ((k % 2 == 0)) n = n + k; else n = n - k; } System.out.println(n); What is printed when the program is executed? (A) 10 (B) 11 (C) 12 (D) 13 (E) 14
D
Consider the following program segment. int n = 10; int count = 0; for (int p = 1; p <= n; p++) for (int q = 1; q <= n-p; q++) count++; System.out.println(count); Which of the following statements describes the relationship between variable n and variable count? (A) When n doubles, count doubles exactly. (B) When n doubles, count doubles roughly. (C) When n doubles, count quadruples exactly. (D) When n doubles, count quadruples roughly. (E) Nothing can be stated about the relationship between n and count.
D
Consider the following program segment. int p = 3; int q = 1; while (q < 10) { q += p; p ++; System.out.print(p + " " + q); } What is printed when the program is executed? (A) 3 3 4 5 5 8 6 12 (B) 4 4 5 8 6 13 (C) 3 34 55 86 12 (D) 4 45 86 13 (E) 1 3 2 4 3 6 4 9 5 13
D
Consider the following program segment. int sum = 0; for (int k = 0; k <= 12; k++) { if ((k % 2 == 0) && (k % 3 == 0)) sum = sum + k; } System.out.println(sum); What is printed when the program is executed? (A) 15 (B) 16 (C) 17 (D) 18 (E) 20
D
The intention of the following program segment is to implement an algorithm to separate a number, n in this case, into individual digits. For instance, in the program example, variable n is 1234 then the program should display 1 2 3 4. Does the following program segment implement the algorithm correctly? int n = 1234; while (n > 0) { int p = n % 10; n = n / 10; System.out.print(p + " "); } (A) No, the program neither separates the individual digits, nor displays them. (B) Yes, the program correctly separates the individual digits, and then displays them. (C) No, the program displays individual digits, but the digits are incorrect. (D) Partially correct, the program displays the individual digits, but in reverse order. (E) The program stops with an ArithmeticException "division by zero" error.
D
Consider the following program segment. String s1 = "TOMATO"; String s2 = ""; int n = s1.length(); int index = n-1; while (index >= 0) { String temp = s1.substring(index,index+1); s2 = s2 + temp; index--; } System.out.println(s2); What is printed when the program is executed? (A) TOMATO (B) IFTRTX (C) TOMATOOTAMOT (D) OTAMOTTOYOTA (E) OTAMOT
E
Consider the following program segment. for (int k = 11; k < 0; k-=2) { System.out.print(k + " "); } What is printed when the program is executed? (A) 11 9 7 5 3 1 (B) 10 8 6 4 2 0 (C) 9 7 5 3 1 (D) 11 (E) No output.
E
Consider the following program segment. int count = 0; for (int k = -4; k < 5; k++) { count++; System.out.print(count + " "); } What is printed when the program is executed? (A) -4 -3 -2 -1 0 1 2 3 4 (B) -5 -4 -3 -2 -1 0 1 2 3 4 5 (C) 9 (D) 1 2 3 4 5 6 7 8 9 10 (E) 1 2 3 4 5 6 7 8 9
E
Consider the following program segment. int p = (int) Math.random() * 10; while (p > 0) { p = p - 2; System.out.print(p + " "); } What is printed when the program is executed? (A) 5 3 1 -1 (B) 6 4 2 0 (C) 4 2 0 (D) 7 5 3 1 -1 (E) No output.
E
Consider the following program segment. int p = 10; while (p > 5) { System.out.print(p + " "); p = (int) (Math.random() * 20); p = p / 2; } Which of the following outputs is not possible? (A) 10 8 (B) 10 9 8 (C) 10 9 6 8 9 (D) 10 (E) 6 8 9
E
Consider the following program segment. int rnd = 0; for (int k = 0; rnd < 5; k++) { rnd = (int) (Math.random() * 10); System.out.println(k + " " + rnd); } Which of the following outputs is not possible? (A) 0 0 1 0 2 1 3 0 4 7 (B) 0 1 1 0 2 6 (C) 0 5 (D) 0 3 1 0 2 2 3 1 4 3 5 2 6 8 (E) 0 3 1 3 2 4 3 6 4 2 5 7
E
Consider the following program segment. int count = 1; for (int x = 1; x < 3; x++) { for (int y = 0; y < 4; y+=2) count+=2; for (int z = 0; z < 4; z+=3) count+=3; } System.out.println(count); What is printed when the program is executed? (A) 10 (B) 12 (C) 16 (D) 19 (E) 21
E