TB LoopsandConditionalsPracticeTest
TB LoopsandConditionalsPracticeTest
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;
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; ?
(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.
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.
I.
A && B
II.
!A && !B
Which of the following best describes the relationship between values produced by expression I and expression II?
(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.
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?
(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.
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");
}
(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.
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
!str.substring(i + 1, i + 2).equals("b"))
{
counter++;
}
}
System.out.println(counter);
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.
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.
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");
}
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
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.
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 ?
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");
}
Which of the following code segments will produce the same output as the code segment above?
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;
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?
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);
0123
0123
0123
Which of the following can be used to replace /* missing loop header */ so that the code segment works as
intended?
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
How many values will be printed when the code segment is executed?
(A) 45
(B) 50
(C) 55
(D) 60
(E) 66
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);
25. The following method is intended to print the number of digits in the parameter num.
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.
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)
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++
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++)
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)
{
System.out.print("First");
}
else
{
System.out.print("Second");
}
}
if (true || true && false)
{
System.out.print("Third");
}
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;
}
}
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
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).
(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.
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.
(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.
int a = 10;
int b = 5 * 2;
System.out.print(a == b);
(A) 5
(B) 10
(C) 10 == 10
(D) true
(E) false
int num = 1;
while (num < 5)
{
System.out.print("A");
num += 2;
}
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.
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.
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.
int x = 7;
if (x < 7)
{
x = 2 * x;
}
if (x % 3 == 1)
{
x = x + 2;
}
System.out.print(3 * x);
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.
(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.
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.
(b) Complete the playRound method below. You must use the spin method appropriately in order to earn full
credit.
The following code segment appears in another method in the same class as wordPlay.
System.out.println(wordPlay("Computer Science"));
46. Consider the following Boolean expression in which the int variables x and y have been properly declared
and initialized.
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
Which of the following code segments will produce the same output as the code segment above?
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;
}