Java Software Solutions, 8e, Global Edition (Lewis/Loftus)
Chapter 6 More Conditionals and Loops
6.1 Multiple-Choice Questions
1) You might choose to use a switch statement instead of nested if-else statements if
A) the variable being tested might equal one of several hundred int values
B) the variable being tested might equal one of only a few int values
C) there are two or more int variables being tested, each of which could be one of several
hundred values
D) there are two or more int variables being tested, each of which could be one of only a few
values
E) none of the above, you would never choose to use a switch statement in place of nested if-else
statements under any circumstance
2) If a switch statement is written that contains no break statements whatsoever,
A) this is a syntax error and an appropriate error message will be generated
B) each of the case clauses will be executed every time the switch statement is encountered
C) this is equivalent to having the switch statement always take the default clause, if one is
present
D) this is not an error, but nothing within the switch statement ever will be executed
E) none of the above
3) A continue statement
A) may be used within a while or a do-while loop, but not a for loop
B) is identical to a break statement within Java loops
C) may be used within any Java loop statement
D) may be used within a for loop, but not within a while or a do-while loop
E) none of the above
}
4) If x is currently equal to 5, what will the value of x be after the switch statement executes?
A) 8
B) 6
C) 11
D) 5
E) 10
5) If x is currently equal to 3, what will the value of x be after the switch statement executes?
A) 5
B) 6
C) 11
D) 10
E) 12
6) The statement if (x < 0) y = x; else y = 0; can be rewritten using a conditional operator as
A) y = (x < 0) ? x : 0;
B) x = (x < 0) ? y : 0;
C) (x < 0) ? y = x : y = 0;
D) y = (x < 0);
E) y = if (x < 0) x : 0;
7) Given the following code, where x = 0, what is the resulting value of x after the for-loop
terminates?
for (int i=0;i<5;i++)
x += i;
A) 0
B) 4
C) 5
D) 10
E) 15
8) The do loop differs from the while loop in that
A) the while loop will always execute the body of the loop at least once
B) the do loop will always execute the body of the loop at least once
C) the do loop will continue to loop while condition in the while statement is false and the while
loop will continue to loop while the condition in the while statement is true
D) the while loop will continue to loop while condition in the while statement is false and the do
loop will continue to loop while the condition in the while statement is true
E) none of the above, there is absolutely no difference between the two types of loops
9) How many times will the following loop iterate?
int x = 10;
do {
System.out.println(x);
x—;
} while (x > 0);
A) 0 times
B) 1 time
C) 9 times
D) 10 times
E) 11 times
10) Given that s is a String, what does the following loop do?
for (int j = s.length( ); j > 0; j—)
System.out.print(s.charAt(j-1));
A) it prints s out backwards
B) it prints s out forwards
C) it prints s out backwards after skipping the last character
D) it prints s out backwards but does not print the 0th character
E) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0
11) The following nested loop structure will execute the inner most statement (x++) how many
times?
for (int j = 0; j < 100; j++)
for (int k = 100; k > 0; k—)
x++;
A) 100
B) 200
C) 10,000
D) 20,000
E) 1,000,000
12) This paint method will draw several bars (sort of like a bar graph). How many bars will be
displayed?
A) 4
B) 5
C) 6
D) 10
E) 20
13) The size of each rectangle (bar)
A) increases in height while staying the same width
B) increases in width while staying the same height
C) increases in both width and height
D) stays the same size
E) decreases in height while staying the same width
14) What will the following code do? Assume s is a String, x is an int initialized to 10, page is a
Graphics object, and this is part of a paint method for an applet.
boolean isVowel = false;
String vowels = “aeiou”;
for (int j = 0; j < s.length( ); j++)
{
for (int k = 0; k < 5; k++)
if (s.charAt(j) == vowels.charAt(k)) isVowel = true;
if (isVowel) page.drawString(“”+s.charAt(j), 10, 15*x++);
else page.drawString(“”+s.charAt(j), 110, 15*x++);
isVowel = false;
}
A) The String s is printed down the applet in two columns, alternating each letter
B) The String s is printed across the applet with vowels printed in one row and all other
characters printed in a row above the vowels
C) The String s is printed across the applet with vowels printed in one row and all other
characters printed in a row below the vowels
D) The String s is printed down the applet in two columns with vowels appearing in the left
column and all other characters in the right column
E) The String s is printed down the applet in two columns with vowels appearing in the right
column and all other characters in the left column
15) Which of the following statements are true about Java loops?
A) all three loop statements are functionally equivalent
B) while loops and do loops are essentially the same; but while loops always execute at least
once
C) if you know the number of times that a loop is to be performed, the best loop statement to use
is a while loop
D) loops may be replaced by an appropriate combination of if-else and switch statements
E) none of the above
6.2 True/False Questions
1) A switch statement must have a default clause.
2) Control in a switch statement jumps to the first matching case.
3) Each case in a switch statement must terminate with a break statement.
4) The following for-loop is an infinite loop.
for (int j = 0; j < 1000; ) i++;
5) It is possible to convert any type of loop (while, do, or for) into any other.
6) The following loop is syntactically valid.
for (int j = 0; j < 1000; j++) j—;
7) In Java, it is possible to create an infinite loop out of while and do loops, but not for-loops.
8) A dialog box is a device which accepts voice commands.
6.3 Free-Form Questions
1) Rewrite the following nested if-else statement as an equivalent switch statement.
if (letter == ‘A’ | | letter == ‘a’) System.out.println(“Excellent”);
else if (letter == ‘B’ | | letter == ‘b’) System.out.println(“You can do better”);
else if (letter == ‘C’ | | letter == ‘c’) System.out.println(“Try harder”);
else if (letter == ‘D’ | | letter == ‘d’) System.out.println(“Try much harder”);
else System.out.println(“Try another major! “);
}
2) Given the following tax table information, write Java code to assign the double taxRate
appropriately given the double pay. Select the selection statement (if, if-else, switch) that makes
the most sense.
If pay is more than 100,000, tax rate is 40%
If pay is more than 60,000 and less than or equal to 100,000, tax rate is 30%
If pay is more than 30,000 and less than or equal to 60,000, tax rate is 20%
If pay is more than 15,000 and less than or equal to 30,000, tax rate is 10%
If pay is more than 5,000 and less than or equal to 15,000, tax rate is 5%
If pay is less than or equal to 5,000, tax rate is 0%
3) Show the output that would occur from the following code, including proper spacing.
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
if (j!=k)
System.out.print(‘ ‘);
else
System.out.print(‘*’);
System.out.println( );
}
4) How many times will the System.out.println(*); statement execute inside of the following
nested for-loops?
for (j=0; j<10; j++)
for (k=10;k>j;k—)
System.out.println(“*”);
5) Write a “query-controlled” loop that will continue to input int values from the user, adding
each to the int value sum, and then ask if the user has another value to input, until the user says
that there are no more values. Assume that cs1.Keyboard has been imported.
6) Write some code that inputs a set of int values and computes its average. Ask the user first
how many int values will be input. Make sure that your code cannot produce a division by zero
error. Assume that cs1.Keyboard has been imported.
7) A data verification loop is a loop that asks the user to input a value within a restricted range
repeatedly until the user has input a value within the requested range. Write a data verification
loop that that inputs an int value x within the range 0 and 100. Assume cs1.Keyboard has been
imported.
8) Describe a situation where you should use a do loop and a situation where you should use a
while loop.
9) Rewrite the following if-else statement using a conditional operator.
if (x > y) z = x; else z = y;
10) Write a paint method to draw out an American flag. The flag will comprise a blue square in
the upper left corner and 13 red and white stripes, starting with red, and alternating. 6 of the 13
stripes appear to the right of the blue square and the last 7 stripes appear beneath the blue square
extending as far right as the other stripes. Use a for-loop to draw the stripes. Do not try to draw
any stars in the blue square.
}
}
11) The following for-loop is odd in that the loop increment value changes during iterations of
the loop. Determine the number of times the loop iterates and the final value of j after the loop
terminates.
int k = 0;
for (j = 0; j < 20; j += k)
k++;
12) Write a set of code that outputs all of the int values between 1 and 100 with 5 values per line,
and each of those 5 values spaced out equally. Use a single for-loop to solve this problem.
13) The following code has a syntax error immediately before the word else. What is the error
and why does it arise? Fix the code so that this statement is a legal if-else statement.
if (x < 0) ; x++;
else x—;