0% found this document useful (0 votes)
608 views

TB LoopsandConditionalsPracticeTest

This document contains a practice test for AP Computer Science A with questions about loops and conditionals. The questions test understanding of code segments involving if/else statements, while loops, Boolean expressions, and converting values between scales. The correct answers are not shown, only the questions and multiple choice options for each question.

Uploaded by

emi d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
608 views

TB LoopsandConditionalsPracticeTest

This document contains a practice test for AP Computer Science A with questions about loops and conditionals. The questions test understanding of code segments involving if/else statements, while loops, Boolean expressions, and converting values between scales. The correct answers are not shown, only the questions and multiple choice options for each question.

Uploaded by

emi d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

AP COMPUTER SCIENCE A Test Booklet

Loops and Conditionals Practice Test

1. Consider the following code segment.

String str1 = new String("Advanced Placement");


String str2 = new String("Advanced Placement");
if (str1.equals(str2) && str1 == str2)
{
System.out.println("A");
}
else if (str1.equals(str2) && str1 != str2)
{
System.out.println("B");
}
else if (!str1.equals(str2) && str1 == str2)
{
System.out.println("C");
}
else if (!str1.equals(str2) && str1 != str2)
{
System.out.println("D");
}

What, if anything, is printed when the code segment is executed?


(A) A
(B) B
(C) C
(D) D
(E) Nothing is printed.

AP Computer Science A Page 1 of 34


Test Booklet

Loops and Conditionals Practice Test

2. Consider the following code segment, which is intended to simulate a random process. The code is intended to set
the value of the variable event to exactly one of the values 1, 2, or 3, depending on the probability of an
event occurring. The value of event should be set to 1 if the probability is 70 percent or less. The value of
event should be set to 2 if the probability is greater than 70 percent but no more than 80 percent. The value of
event should be set to 3 if the probability is greater than 80 percent. The variable randomNumber is used to
simulate the probability of the event occurring.

int event = 0;
if (randomNumber <= 0.70)
{
event = 1;
}
if (randomNumber <= 0.80)
{
event = 2;
}
else
{
event = 3;
}

The code does not work as intended. Assume that the variable randomNumber has been properly declared and
initialized. Which of the following initializations for randomNumber will demonstrate that the code segment
will not work as intended?
(A) randomNumber = 0.70;
(B) randomNumber = 0.80;
(C) randomNumber = 0.85;
(D) randomNumber = 0.90;
(E) randomNumber = 1.00;

3. Consider the following code segment.

int j = 1;
while (j < 5)
{
int k = 1;
while (k < 5)
{
System.out.println(k);
k++;
}
j++;
}

Which of the following best explains the effect, if any, of changing the first line of code to int j = 0; ?

Page 2 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

(A) There will be one more value printed because the outer loop will iterate one additional time.
(B) There will be four more values printed because the outer loop will iterate one additional time.
(C) There will be one less value printed because the outer loop will iterate one fewer time.
(D) There will be four fewer values printed because the outer loop will iterate one fewer time.
(E) There will be no change to the output of the code segment.

4. Consider the following method definition. The method printAllCharacters is intended to print out every
character in str, starting with the character at index 0.

public static void printAllCharacters(String str)


{
for (int x = 0; x < str.length(); x++) // Line 3
{
System.out.print(str.substring(x, x + 1));
}
}

The following statement is found in the same class as the printAllCharacters method.

printAllCharacters("ABCDEFG");

Which choice best describes the difference, if any, in the behavior of this statement that will result from changing x
< str.length() to x <= str.length() in line 3 of the method?
The method call will print fewer characters than it did before the change because the loop will iterate fewer
(A)
times.
The method call will print more characters than it did before the change because the loop will iterate more
(B)
times.
The method call, which worked correctly before the change, will now cause a run-time error because it
(C)
attempts to access a character at index 7 in a string whose last element is at index 6.
The method call, which worked correctly before the change, will now cause a run-time error because it
(D)
attempts to access a character at index 8 in a string whose last element is at index 7.
(E) The behavior of the code segment will remain unchanged.

5. Consider the following Boolean expressions.

I.
A && B

II.
!A && !B

Which of the following best describes the relationship between values produced by expression I and expression II?

AP Computer Science A Page 3 of 34


Test Booklet

Loops and Conditionals Practice Test

(A) Expression I and expression II evaluate to different values for all values of A and B.
(B) Expression I and expression II evaluate to the same value for all values of A and B.
(C) Expression I and expression II evaluate to the same value only when A and B are the same.
(D) Expression I and expression II evaluate to the same value only when A and B differ.
(E) Expression I and expression II evaluate to the same value whenever A is true.

6. Consider the following two code segments where the int variable choice has been properly declared and
initialized.

Code Segment A
if (choice > 10)
{
System.out.println("blue");
}
else if (choice < 5)
{
System.out.println("red");
}
else
{
System.out.println("yellow");
}

Code Segment B
if (choice > 10)
{
System.out.println("blue");
}
if (choice < 5)
{
System.out.println("red");
}
else
{
System.out.println("yellow");
}

Assume that both code segments initialize choice to the same integer value. Which of the following best
describes the conditions on the initial value of the variable choice that will cause the two code segments to
produce different output?
(A) choice < 5
(B) choice >= 5 and choice <= 10
(C) choice > 10
(D) choice == 5 or choice == 10
(E) There is no value for choice that will cause the two code segments to produce different output.

Page 4 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

7. Consider the following code segments, which are each intended to convert grades from a 100-point scale to a
4.0-point scale and print the result. A grade of 90 or above should yield a 4.0, a grade of 80 to 89 should yield a 3.0,
a grade of 70 to 79 should yield a 2.0, and any grade lower than 70 should yield a 0.0.

Assume that grade is an int variable that has been properly declared and initialized.

Code Segment I
double points = 0.0;
if (grade > 89)
{
points += 4.0;
}
else if (grade > 79)
{
points += 3.0;
}
else if (grade > 69)
{
points += 2.0;
}
else
{
points += 0.0;
}
System.out.println(points);

Code Segment II
double points = 0.0;
if (grade > 89)
{
points += 4.0;
}
if (grade > 79)
{
grade += 3.0;
}
if (grade > 69)
{
points += 2.0;
}
if (grade < 70)
{
points += 0.0;
}
System.out.println(points);

Which of the following statements correctly compares the values printed by the two methods?

AP Computer Science A Page 5 of 34


Test Booklet

Loops and Conditionals Practice Test

(A) The two code segments print the same value only when grade is below 80.
(B) The two code segments print the same value only when grade is 90 or above or grade is below 80.
(C) The two code segments print the same value only when grade is 90 or above.
(D) Both code segments print the same value for all possible values of grade.
(E) The two code segments print different values for all possible values of grade.

Page 6 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

8. Consider the following code segment in which the int variable x has been properly declared and initialized.

if (x % 2 == 1)
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}

Assuming that x is initialized to the same positive integer value as the original, which of the following code
segments will produce the same output as the original code segment?

I.
if (x % 2 == 1)
{
System.out.print("YES");
}
if (x % 2 == 0)
{
System.out.println("NO");
}

II.
if (x % 2 == 1)
{
System.out.println("YES");
}
else if (x % 2 == 0)
{
System.out.println("NO");
}
else
{
System.out.println("NONE");
}

III.
boolean test = x % 2 == 0;
if (test)
{
System.out.println("YES");
}
else
{
System.out.println("NO");

AP Computer Science A Page 7 of 34


Test Booklet

Loops and Conditionals Practice Test

}
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III

9. Consider the following code segments. Code segment 2 is a revision of code segment 1 in which the loop increment
has been changed.

Code Segment 1
int sum = 0;
for (int k = 1; k <= 30; k++)
{
sum += k;
}
System.out.println("The sum is: " + sum);

Code Segment 2
int sum = 0;
for (int k = 1; k <= 30; k = k + 2)
{
sum += k;
}
System.out.println("The sum is: " + sum);

Code segment 1 prints the sum of the integers from 1 through 30, inclusive. Which of the following best explains
how the output changes from code segment 1 to code segment 2 ?
(A) Code segment 1 and code segment 2 will produce the same output.
Code segment 2 will print the sum of only the even integers from 1 through 30, inclusive because it starts
(B)
sum at zero, increments k by twos, and terminates when k exceeds 30.
Code segment 2 will print the sum of only the odd integers from 1 through 30, inclusive because it starts k
(C)
at one, increments k by twos, and terminates when k exceeds 30.
Code segment 2 will print the sum of only the even integers from 1 through 60, inclusive because it starts
(D)
sum at zero, increments k by twos, and iterates 30 times.
Code segment 2 will print the sum of only the odd integers from 1 through 60, inclusive because it starts k
(E)
at one, increments k by twos, and iterates 30 times.

Page 8 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

10. Consider the following code segment.

int num = 1;
int count = 0;
while (num <= 10)
{

{
count++;
}
num++;
}

What value is stored in the variable count as a result of executing the code segment?
(A) 1
(B) 3
(C) 5
(D) 7
(E) 8

11. Consider the following code segment.

String str = "a black cat sat on a table";


int counter = 0;
for (int i = 0; i < str.length() - 1; i++)
{

!str.substring(i + 1, i + 2).equals("b"))
{
counter++;
}
}
System.out.println(counter);

What is printed as a result of executing this code segment?


(A) 1
(B) 2
(C) 3
(D) 5
(E) 6

AP Computer Science A Page 9 of 34


Test Booklet

Loops and Conditionals Practice Test

12. Consider the following two code segments. Code segment II is a revision of code segment I in which the loop
header has been changed.

I.
for (int k = 1; k <= 5; k++)
{
System.out.print(k);
}

II.
for (int k = 5; k >= 1; k--)
{
System.out.print(k);
}

Which of the following best explains how the output changes from code segment I to code segment II?
(A) Both code segments produce the same output, because they both iterate four times.
(B) Both code segments produce the same output, because they both iterate five times.
Code segment I prints more values than code segment II does, because it iterates for one additional value of
(C)
k.
(D) Code segment II prints more values than code segment I, because it iterates for one additional value of k.
The code segments print the same values but in a different order, because code segment I iterates from 1 to
(E)
5 and code segment II iterates from 5 to 1.

13. Consider the following code segment.

String alpha = new String("APCS");


String beta = new String("APCS");
String delta = alpha;
System.out.println(alpha.equals(beta));
System.out.println(alpha == beta);
System.out.println(alpha == delta);

What is printed as a result of executing the code segment?

Page 10 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

false
(A) false
false
false
(B) false
true
true
(C) false
false
true
(D) false
true
true
(E) true
true

14. Assume that object references one, two, and three have been declared and instantiated to be of the same
type. Assume also that one == two evaluates to true and that two.equals(three) evaluates to
false.

Consider the following code segment.

if (one.equals(two))
{
System.out.println("one dot equals two");
}
if (one.equals(three))
{
System.out.println("one dot equals three");
}
if (two == three)
{
System.out.println("two equals equals three");
}

What, if anything, is printed as a result of executing the code segment?


(A) one dot equals two
one dot equals two
(B) one dot equals three
one dot equals three
(C) two equals equals three
one dot equals two
(D) one dot equals three
two equals equals three
(E) Nothing is printed.

AP Computer Science A Page 11 of 34


Test Booklet

Loops and Conditionals Practice Test

15. Consider the following code segment.

int count = 5;
while (count < 100)
{
count = count * 2;
}
count = count + 1;

What will be the value of count as a result of executing the code segment?
(A) 100
(B) 101
(C) 160
(D) 161
(E) 321

16. Consider the following code segment.

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();
}

Which of the following best explains the effect of simultaneously changing x <= 4 to x < 4 in line 1 and y
< 4 to y <= 4 in line 3 ?
"a" will be printed fewer times because while each output line will have the same length as before, the
(A)
number of lines printed will decrease by 1.
"a" will be printed more times because while the number of output lines will be the same as before, the
(B)
length of each output line will increase by 1.
"a" will be printed the same number of times because while the number of output lines will decrease by 1,
(C)
the length of each line will increase by 1.
"a" will be printed more times because both the number of output lines and the length of each line will
(D)
increase by 1.
(E) The output of the code segment will not change in any way.

Page 12 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

17. Consider the following code segment.

if (a < b || c != d)
{
System.out.println("dog");
}
else
{
System.out.println("cat");
}

Assume that the int variables a, b, c, and d have been properly declared and initialized. Which of the
following code segments produces the same output as the given code segment for all values of a, b, c, and d ?

AP Computer Science A Page 13 of 34


Test Booklet

Loops and Conditionals Practice Test

if (a < b && c != d)
{
System.out.println("dog");
}
(A) else
{
System.out.println("cat");
}
if (a < b && c != d)
{
System.out.println("cat");
}
(B) else
{
System.out.println("dog");
}
if (a > b && c == d)
{
System.out.println("cat");
}
(C) else
{
System.out.println("dog");
}
if (a >= b || c == d)
{
System.out.println("cat");
}
(D) else
{
System.out.println("dog");
}
if (a >= b && c == d)
{
System.out.println("cat");
}
(E) else
{
System.out.println("dog");
}

18. Consider the following code segment.

for (int k = 1; k <= 7; k += 2)


{
System.out.print(k);
}

Which of the following code segments will produce the same output as the code segment above?

Page 14 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

for (int k = 0; k < 7; k += 2)


{
(A) System.out.print(k);
}
for (int k = 0; k <= 7; k += 2)
{
(B) System.out.print(k);
}
for (int k = 0; k <= 8; k += 2)
{
(C) System.out.print(k + 1);
}
for (int k = 1; k < 7; k += 2)
{
(D) System.out.print(k + 1);
}
for (int k = 1; k <= 8; k += 2)
{
(E) System.out.print(k);
}

19. Consider the following statement, which assigns a value to b1.

boolean b1 = true && (17 % 3 == 1);

Which of the following assigns the same value to b2 as the value stored in b1 ?
(A) boolean b2 = false || (17 % 3 == 2);
(B) boolean b2 = false && (17 % 3 == 2);
(C) boolean b2 = true || (17 % 3 == 1);
(D) boolean b2 = (true || false) && true;
(E) boolean b2 = (true && false) || true;

20. Consider the following code segment.

int count = 0;
for (int k = 0; k < 10; k++)
{
count++;
}
System.out.println(count);

Which of the following code segments will produce the same output as the code segment above?

AP Computer Science A Page 15 of 34


Test Booklet

Loops and Conditionals Practice Test

int count = 0;
for (int k = 1; k < 10; k++)
{
(A) count++;
}
System.out.println(count);
int count = 1;
for (int k = 1; k <= 10; k++)
{
(B) count++;
}
System.out.println(count);
int count = 1;
for (int k = 0; k <= 9; k++)
{
(C) count++;
}
System.out.println(count);
int count = 0;
for (int k = 9; k >= 0; k--)
{
(D) count++;
}
System.out.println(count);
int count = 0;
for (int k = 10; k >= 0; k--)
{
(E) count++;
}
System.out.println(count);

Page 16 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

21. Consider the following code segment.

/* missing loop header */


{
for (int k = 0; k < 4; k++)
{
System.out.print(k);
}
System.out.println();
}

The code segment is intended to produce the following output.

0123
0123
0123

Which of the following can be used to replace /* missing loop header */ so that the code segment works as
intended?

I. for (int j = 0; j < 3; j++)


II. for (int j = 1; j < 3; j++)
III. for (int j = 1; j <= 3; j++)
(A) I only
(B) II only
(C) III only
(D) I and II
(E) I and III

22. Consider the following code segment.

int counter = 0;
for (int x = 10; x > 0; x--)
{
for (int y = x; y <= x; y++)
{
counter++; // line 6
}
}

How many times will the statement in line 6 be executed as a result of running the code segment?
(A) 0
(B) 1
(C) 10
(D) 11
(E) 20

AP Computer Science A Page 17 of 34


Test Booklet

Loops and Conditionals Practice Test

23. Consider the following code segment.

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);
}
}

How many values will be printed when the code segment is executed?
(A) 45
(B) 50
(C) 55
(D) 60
(E) 66

24. Consider the following code segment.

int x = 3;
int y = -1;
if (x - 2 > y)
{
x -= y;
}
if (y + 3 >= x)
{
y += x;
}
System.out.print("x = " + x + " y = " + y);

What is printed as a result of the execution of the code segment?


(A) x = -1 y = -1
(B) x = 2 y = 1
(C) x = 3 y = 2
(D) x = 4 y = -1
(E) x = 4 y = 3

Page 18 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

25. The following method is intended to print the number of digits in the parameter num.

public int numDigits(int num)


{
int count = 0;
while (/* missing condition */)
{
count++;
num = num / 10;
}
return count;
}

Which of the following can be used to replace /* missing condition */ so that the method will work as
intended?
(A) count != 0
(B) count > 0
(C) num >= 0
(D) num != 0
(E) num == 0

26. The following method is intended to return true if and only if the parameter val is a multiple of 4 but is not
a multiple of 100 unless it is also a multiple of 400. The method does not always work correctly.

public boolean isLeapYear(int val)


{
if ((val % 4) == 0)
{
return true;
}
else
{
return (val % 400) == 0;
}
}

Which of the following method calls will return an incorrect response?


(A) isLeapYear(1900)
(B) isLeapYear(1984)
(C) isLeapYear(2000)
(D) isLeapYear(2001)
(E) isLeapYear(2010)

AP Computer Science A Page 19 of 34


Test Booklet

Loops and Conditionals Practice Test

27. Consider the following code segment, which is intended to print the sum of all the odd integers from 0 up to and
including 101.

int r = 0;
int sum = 0;
/* missing loop header */
{
if (r % 2 == 1)
{
sum += r;
}
r++;
}
System.out.println(sum);

Which of the following could replace /* missing loop header */ to ensure that the code segment will work as
intended?
(A) while (r <= 100)
(B) while (sum <= 100)
(C) while (r < 101)
(D) while (r <= 101)
(E) while (sum <= 101)

28. Consider the following code segment.

for (int outer = 0; outer < 3; outer++)


{
for (/* missing loop header */)
{
System.out.print(outer + "" + inner + "_");
}
}

Which of the following can be used as a replacement for /* missing loop header */ so that the code segment
produces the output 00_01_02_11_12_22_ ?
(A) int inner = 0; inner < 3; inner++
(B) int inner = 1; inner < 3; inner++
(C) int inner = outer - 1; inner < 3; inner++
(D) int inner = outer; inner < 3; inner++
(E) int inner = outer + 1; inner < 3; inner++

Page 20 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

29. Consider the following code segment.

int count = 0;
for (int x = 1; x <= 3; x++)
{
/* missing loop header */
{
count++;
}
}
System.out.println(count);

Which of the following should be used to replace /* missing loop header */ so that the code segment will print
6 as the value of count ?
(A) for (int y = 0; y <= 2; y++)
(B) for (int y = 0; y < 3; y++)
(C) for (int y = 2; y >= 0; y--)
(D) for (int y = 3; y > 0; y--)
(E) for (int y = 0; y < x; y++)

30. Consider the following code segment.

int k = 0;
/* missing loop header */
{
k++;
System.out.print(k + " ");
}

Which of the following can be used as a replacement for /* missing loop header */ so that the code segment
prints out the string "1 2 3 4 "?
(A) while (k < 3)
(B) while (k < 4)
(C) while (k < 5)
(D) while (k <= 4)
(E) while (k <= 5)

AP Computer Science A Page 21 of 34


Test Booklet

Loops and Conditionals Practice Test

31. Consider the following code segment.

if (false && true || false)


{

{
System.out.print("First");
}
else
{
System.out.print("Second");
}
}
if (true || true && false)
{
System.out.print("Third");
}

What is printed as a result of executing the code segment?


(A) First
(B) Second
(C) Third
(D) FirstThird
(E) SecondThird

Page 22 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

32. Consider the following code segment.

int start = 4;
int end = 5;
boolean keepGoing = true;
if (start < end && keepGoing)
{
if (end > 0)
{
start += 2;
end++;
}
else
{
end += 3;
}
}
if (start < end)
{
if (end == 0)
{
end += 2;
start++;
}
else
{
end += 4;
}
}

What is the value of end after the code segment is executed?


(A) 5
(B) 6
(C) 9
(D) 10
(E) 16

AP Computer Science A Page 23 of 34


Test Booklet

Loops and Conditionals Practice Test

33. Consider the following code segment.

int x = 7;
int y = 4;
boolean a = false;
boolean b = false;
if (x > y)
{
if (x % y >= 3)
{
a = true;
x -= y;
}
else
{
x += y;
}
}
if (x < y)
{
if (y % x >= 3)
{
b = true;
x -= y;
}
else
{
x += y;
}
}

What are the values of a, b, and x after the code segment has been executed?
(A) a = true, b = true, x = -1
(B) a = true, b = false, x = 3
(C) a = true, b = false, x = 7
(D) a = false, b = true, x = 3
(E) a = false, b = false, x = 11

Page 24 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

34. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.

This question involves the StringManip class, which is used to perform manipulation on strings.

The class provides the removeSpaces method, whose implementation is not shown. The method takes a string and
returns a new string with spaces removed. For example, removeSpaces("hi how are you") returns
"hihowareyou". The removeSpaces method will be used in part (b).

public class StringManip


{
/** Takes a string str and returns a new string
* with all spaces removed.
*/
public static String removeSpaces(String str)
{ /* implementation not shown */ }

/** Takes a string str and returns a new string


* with the characters reversed, as described in part (a).
*/
public static String reverseString(String str)
{ /* to be implemented in part (a) */ }

/** Determines whether str is a palindrome and prints a message


* indicating the result, as described in part (b).
* Precondition: str contains only lowercase letters and spaces.
*/
public static void palindromeChecker(String str)
{ /* to be implemented in part (b) */ }
}

(a) Write method reverseString, which takes a string str and returns a new string with the characters in str in
reverse order. For example, reverseString("ABCDE") should return "EDCBA".

Complete the reverseString method below by assigning the reversed string to result.

/** Takes a string str and returns a new string


* with the characters reversed.
*/

AP Computer Science A Page 25 of 34


Test Booklet

Loops and Conditionals Practice Test

public static String reverseString(String str)


{
String result = "";
return result;
}

Please respond on separate paper, following directions from your teacher.

For this question, let a palindrome be defined as a string that, when spaces are removed, reads the same forward and
backward. For example, "race car" and "taco cat" are palindromes. You will write method
palindromeChecker, which determines whether a string is a palindrome and prints a message indicating the result.
Examples of the intended behavior of the method are shown in the following table.

Method Call Printed Message


palindromeChecker("taco cat") taco cat is a palindrome
palindromeChecker("laid on no dial") laid on no dial is a palindrome
palindromeChecker("level up") level up is not a palindrome

(b) Write method palindromeChecker below. Assume that reverseString works as specified, regardless of
what you wrote in part (a). You must use reverseString and removeSpaces appropriately to receive full credit.
Your implementation must conform to the examples in the table.

/** Determines whether str is a palindrome and prints a message


* indicating the result, as described in part (b).
* Precondition: str contains only lowercase letters and spaces.
*/
public static void palindromeChecker(String str)

Please respond on separate paper, following directions from your teacher.

35. Consider the following code segment.

int a = 10;
int b = 5 * 2;
System.out.print(a == b);

What is printed as a result of executing the code segment?

Page 26 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

(A) 5
(B) 10
(C) 10 == 10
(D) true
(E) false

36. Consider the following code segment.

int num = 1;
while (num < 5)
{
System.out.print("A");
num += 2;
}

What is printed as a result of executing the code segment?


(A) A
(B) AA
(C) AAA
(D) AAAA
(E) AAAAA

37. Consider the following code segment.

for (int j = 0; j < 3; j++)


{
for (int k = 0; k < 4; k++)
{
System.out.println("Fun");
}
}

Which of the following best explains how changing the outer for loop header to for (int j = 0; j <=
3; j++) affects the output of the code segment?
(A) The output of the code segment will be unchanged.
(B) The string "Fun" will be printed more times because the outer loop will execute more times.
The string "Fun" will be printed more times because the inner loop will execute more times in each
(C)
iteration of the outer loop.
(D) The string "Fun" will be printed fewer times because the outer loop will execute fewer times.
The string "Fun" will be printed fewer times because the inner loop will execute fewer times in each
(E)
iteration of the outer loop.

AP Computer Science A Page 27 of 34


Test Booklet

Loops and Conditionals Practice Test

38. Consider the following code segment.

int val = 1;
while (val <= 6)
{
for (int k = 0; k <= 2; k++)
{
System.out.println("Surprise!");
}
val++;
}

How many times is the string "Surprise!" printed as a result of executing the code segment?
(A) 3
(B) 6
(C) 12
(D) 15
(E) 18

39. A school that does not have air conditioning has published a policy to close school when the outside temperature
reaches or exceeds 95°F. The following code segment is intended to print a message indicating whether or not the
school is open, based on the temperature. Assume that the variable degrees has been properly declared and
initialized with the outside temperature.

if (degrees > 95)


{
System.out.println("School will be closed due to extreme heat");
}
else
{
System.out.println("School is open");
}

Which of the following initializations for degrees, if any, will demonstrate that the code segment may not work
as intended?
(A) degrees = 90;
(B) degrees = 94;
(C) degrees = 95;
(D) degrees = 96;
(E) The code will work as intended for all values of degrees.

Page 28 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

40. Consider the following code segment.

int x = 7;
if (x < 7)
{
x = 2 * x;
}
if (x % 3 == 1)
{
x = x + 2;
}
System.out.print(3 * x);

What is printed as a result of executing the code segment?


(A) 7
(B) 9
(C) 14
(D) 21
(E) 27

41. Consider the following code segment.

double regularPrice = 100;


boolean onClearance = true;
boolean hasCoupon = false;
double finalPrice = regularPrice;
if(onClearance)
{
finalPrice -= finalPrice * 0.25;
}
if(hasCoupon)
{
finalPrice -= 5.0;
}
System.out.println(finalPrice);

What is printed as a result of executing the code segment?


(A) 20.0
(B) 25.0
(C) 70.0
(D) 75.0
(E) 95.0

AP Computer Science A Page 29 of 34


Test Booklet

Loops and Conditionals Practice Test

42. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.

This question involves a game that is played with multiple spinners. You will write two methods in the SpinnerGame
class below.

public class SpinnerGame


{
/** Precondition: min < max
* Simulates a spin of a spinner by returning a random integer
* between min and max, inclusive.
*/
public int spin(int min, int max)
{ /* to be implemented in part (a) */ }

/** Simulates one round of the game as described in part (b).


*/
public void playRound()
{ /* to be implemented in part (b) */ }
}

(a) The spin method simulates a spin of a fair spinner. The method returns a random integer between min and max,
inclusive. Complete the spin method below by assigning this random integer to result.

/** Precondition: min < max


* Simulates a spin of a spinner by returning a random integer
* between min and max, inclusive.
*/
public int spin(int min, int max)
{
int result;
return result;
}

Please respond on separate paper, following directions from your teacher.

Page 30 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

In each round of the game, the player and the computer each spin a spinner. The player spins a spinner numbered 1 to
10 , inclusive, whereas the computer spins a spinner numbered 2 to 8, inclusive.

Based on the results of the spins, a message is printed in the formats shown in the examples below.

If the player obtains a higher result than the computer, the player gains a number of points equal to the positive
difference between the spins. If the computer obtains a higher result than the player, the player loses a number of points
equal to the positive difference between the spins.

In the event of a tie, the player and the computer each spin the spinner a second time. If the sum of the player’s two
spins are greater than the sum of the computer’s two spins, the player gains one point. If the sum of the computer’s two
spins are greater than the sum of the player’s two spins, the player loses one point. In the event of a tie after two spins,
the round is reported as a tie and the player’s score does not change.

Examples of the playRound method’s intended behavior are shown in the following table.

Player Spin Computer Spin Player Spin Computer Spin


Printed String
#1 #1 #2 #2
You win! 3
9 6
points
You lose.
3 7
points
You win! 1
4 4 6 2
points
You lose.
6 6 1 2
points
1 1 8 8 Tie. 0 points

(b) Complete the playRound method below. You must use the spin method appropriately in order to earn full
credit.

/** Simulates one round of the game as described in part (b).


*/
public void playRound()

Please respond on separate paper, following directions from your teacher.

AP Computer Science A Page 31 of 34


Test Booklet

Loops and Conditionals Practice Test

43. Consider the following method.

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;
}

The following code segment appears in another method in the same class as wordPlay.

System.out.println(wordPlay("Computer Science"));

What is printed as a result of executing the code segment?


(A) C
(B) ci tm
(C) eeStm
(D) ncepC
(E) eeSepC

44. Consider the following statement.

boolean x = (5 < 8) == (5 == 8);

What is the value of x after the statement has been executed?


(A) 3
(B) 5
(C) 8
(D) true
(E) false

Page 32 of 34 AP Computer Science A


Test Booklet

Loops and Conditionals Practice Test

45. Consider the following method.

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;
}

What value is returned as a result of the method call changeStr("12345") ?


(A) "4"
(B) "53"
(C) "531"
(D) "543"
(E) "54321"

46. Consider the following Boolean expression in which the int variables x and y have been properly declared
and initialized.

(x <= 10) == (y > 25)

Which of the following values for x and y will result in the expression evaluating to true ?
(A) x = 8 and y = 25
(B) x = 10 and y = 10
(C) x = 10 and y = 30
(D) x = 15 and y = 30
(E) x = 25 and y = 30

47. Consider the following code segment.

for (int j = 1; j < 10; j += 2)


{
System.out.print(j);
}

Which of the following code segments will produce the same output as the code segment above?

AP Computer Science A Page 33 of 34


Test Booklet

Loops and Conditionals Practice Test

int j = 1;
while (j < 10)
{
(A) j += 2;
System.out.print(j);
}
int j = 1;
while (j < 10)
{
(B) System.out.print(j);
j += 2;
}
int j = 1;
while (j <= 10)
{
(C) j += 2;
System.out.print(j);
}
int j = 1;
while (j >= 10)
{
(D) j += 2;
System.out.print(j);
}
int j = 1;
while (j >= 10)
{
(E) System.out.print(j);
j += 2;
}

Page 34 of 34 AP Computer Science A

You might also like