Ap Comp Sci Test
public static String changeStr(String str) { String result = ""; for (int i = str.length() - 1; i >= str.length() / 2; i -= 2) { result += str.substring(i, i + 1); } return result; }
"53"
for (int x = 0; x <= 4; x++) // Line 1 { for (int y = 0; y < 4; y++) // Line 3 { System.out.print("a"); } System.out.println(); }
"a" will be printed the same number of times because while the number of output lines will decrease by 1, the length of each line will increase by 1.
public String mystery(String input) { String output = " ";
"optr"
Anything with - and *
--* -** ***
int num = 1; int count = 0; while (num <= 10) { if (num % 2 == 0 && num % 3 == 0) { count++; } num++; }
1
int counter = 0; for (int x = 10; x > 0; x--) { for (int y = x; y <= x; y++) { counter++; // line 6 } }
10
system.out.println(count);
10
public int mystery(int num) { int x = num; while (x > 0) { if (x / 10 % 2 == 0) return x; x = x / 10; } return x; }
103
int count = 5; while (count < 100) { count = count * 2; } count = count + 1;
161
int val = 1; while (val <= 6) { for (int k = 0; k <= 2; k++) { System.out.println("Surprise!"); } val++; }
18
for (int outer = 1; outer <= 6; outer ++)
246 246 46 6 6
for (int k = 0; k<20; k = k + 2)
4, 10, 16
int num = 2574; int result = 0; while (num > 0)
4752
public int getTheResult(int n) { int product = 1; for (int number = 1; number < n; number++) { if (number % 2 == 0) product *= number; } return product; }
48
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);
5
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); } }
60
public static int mystery (int n) mystery (6)
8
int num = 1; while (num < 5) { System.out.println("A"); num += 2; }
AA
int sum = 0; for (int k = 1; k <= 30; k++) { sum += k; } System.out.println("The sum is: " + sum);
Code segment 2 will print the sum of only the odd integers from 1 through 30, inclusive because it starts k at one, increments k by twos, and terminates when k exceeds 30.
11111 2222 333 44 5
D
The code segment is intended to produce the following output. 0123 0123 0123
I and III
x < 6 x != 6 x < 7
I and III only
x < 0 x <= 1 x < 10
I, II, and III
int sum = 0; int k = 1; while (sum < 12 || k < 4) sum += k; System.out.print.ln(sum);
Nothing is printed due to an infinite loop
I. for (int k = 1; k <= 5; k++) { System.out.print(k); } II. for (int k = 5; k >= 1; k--) { System.out.print(k); }
The code segments print the same values but in a different order, because code segment I iterates from 1 to 5 and code segment II iterates from 5 to 1.
printAllCharacters("ABCDEFG");
The method call, which worked correctly before the change, will now cause a run-time error because it attempts to access a character at index 7 in a string whose last element is at index 6.
for (int j = 0; j < 3; j++) { for (int k = 0; k < 4; k++) { System.out.println("Fun"); } }
The string "Fun" will be printed more times because the outer loop will execute more times.
//* Precondition: num > 0 */
The sum of all numbers between 1 and num inclusive
int j = 1; while (j < 5) { int k = 1; while (k < 5) { System.out.println(k); k++; } j++; }
There will be four more values printed because the outer loop will iterate one additional time.
public String wordPlay(String word) { String str = ""; for (int k = 0; k < word.length(); k++) { if (k % 3 == 0) { str = word.substring(k, k + 1) + str; } } return str; }
eeSepC
for (int k = 1; k <= 7; k += 2) { System.out.print(k); }
for (int k = 1; k <= 8; k += 2) { System.out.print(k); }
for (int k = 1; k<= 100; k++)
for (int k = 4; k<=100; k = k+4)
int count = 0; for (int x = 1; x <= 3; x++) { /* missing loop header */ { count++; } } System.out.println(count);
for (int y = 0; y < x; y++)
public static int numDivisors (int inputVal)
inputVal % k == 0
int count = 0; for (int k = 0; k < 10; k++) { count++; } System.out.println(count);
int count = 0; for (int k = 9; k >= 0; k--) { count++; } System.out.println(count);
for (int outer = 0; outer < 3; outer++) { for (/* missing loop header */) { System.out.print(outer + "" + inner + "_"); } }
int inner = outer; inner < 3; inner++
for (int j = 1; j < 10; j += 2) { System.out.print(j); }
int j = 1; while (j < 10) { System.out.print(j); j += 2; }
public static boolean mystery (String str)
mystery "noon"
public static int mystery (int n) which is true of method mystery?
n will always be greater than 2 at // Point B.
Public int compute (int n, int k)
n^k
public int numDigits(int num) { int count = 0; while (/* missing condition */) { count++; num = num / 10; } return count; }
num != 0
int k = 0; /* missing loop header */ { k++; System.out.print(k + " "); }
while (k < 4)
int r = 0; int sum = 0; /* missing loop header */ { if (r % 2 == 1) { sum += r; } r++; } System.out.println(sum);
while (r <= 101)