Java Software Solutions, 8e, Global Edition (Lewis/Loftus)
Chapter 5 Conditionals and Loops
5.1 Multiple-Choice Questions
1) The idea that program instructions execute in order (linearly) unless otherwise specified
through a conditional statement is known as
A) boolean execution
B) conditional statements
C) try and catch
D) sequentiality
E) flow of control
2) Of the following if statements, which one correctly executes three instructions if the condition
is true?
A) if (x < 0)
a = b * 2;
y = x;
z = a – y;
B) {
if (x < 0)
a = b * 2;
y = x;
z = a – y;
}
C) if { (x < 0)
a = b * 2;
y = x;
z = a – y ;
}
D) if (x < 0)
{
a = b * 2;
y = x;
z = a – y;
}
E) B, C and D are all correct, but not A
3) Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x
is negative but leave x alone if x is 0?
A) if (x > 0) x++;
else x—;
B) if (x > 0) x++;
else if (x < 0) x—;
C) if (x > 0) x++;
if (x < 0) x—;
else x = 0;
D) if (x == 0) x = 0;
else x++;
x—;
E) x++;
x—;
4) If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
A) 0
B) 2
C) 3
D) 4
E) 5
5) If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
A) 0
B) 2
C) 3
D) 4
E) 5
6) If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?
A) 0
B) 2
C) 3
D) 4
E) 5
7) Consider the following code that will assign a letter grade of ‘A’, ‘B’, ‘C’, ‘D’, or ‘F’ depending
on a student’s test score.
if (score >= 90) grade = ‘A’;
if (score >= 80) grade = ‘B’;
if (score >= 70) grade = ‘C’;
if (score >= 60) grade = ‘D’;
else grade = ‘F’;
A) This code will work correctly in all cases
B) This code will work correctly only if grade >= 60
C) This code will work correctly only if grade < 60
D) This code will work correctly only if grade < 70
E) This code will not work correctly under any circumstances
8) Assume that count is 0, total is 20 and max is 1. The following statement will do which of the
following? if (count != 0 && total / count > max) max = total / count;
A) The condition short circuits and the assignment statement is not executed
B) The condition short circuits and the assignment statement is executed without problem
C) The condition does not short circuit causing a division by zero error
D) The condition short circuits so that there is no division by zero error when evaluating the
condition, but the assignment statement causes a division by zero error
E) The condition will not compile because it uses improper syntax
9) What is wrong, logically, with the following code?
if (x > 10) System.out.println(“Large”);
else if (x > 6 && x <= 10) System.out.println(“Medium”);
else if (x > 3 && x <= 6) System.out.println(“Small”);
else System.out.println(“Very small”);
A) There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x
<= 6) in the third conditional
B) There is no logical error, but there is no need to have (x > 6) in the second conditional or (x >
3) in the third conditional
C) The logical error is that no matter what value x is, “Very small” is always printed out
D) The logical error is that no matter what value x is, “Large” is always printed out
E) There is nothing wrong with the logic at all
10) Consider the following outline of a nested if-else structure which has more if clauses than
else clauses. Which of the statements below is true regarding this structure?
if (condition1)
if (condition2)
statement1;
else statement2;
A) syntactically it is invalid to have more if clauses than else clauses
B) statement2 will only execute if condition1 is false and condition2 is false
C) statement2 will only execute if condition1 is true and condition2 is false
D) statement2 will only execute if condition1 is false, it does not matter what condition2 is
E) statement2 will never execute
11) Assume that x and y are int variables with x = 5, y = 3, and a and d are char variables with a
= ‘a’ and d = ‘A’, and examine the following conditions:
Condition 1: (x < y && x > 0)
Condition 2: (a != d || x != 5)
Condition 3: !(true && false)
Condition 4: (x > y || a == ‘A’ || d != ‘A’)
A) All 4 Conditions are true
B) Only Condition 2 is true
C) Condition 2 and Condition 4 are true only
D) Conditions 2, 3 and 4 are all true, Condition 1 is not
E) All 4 Conditions are false
12) The break statement does which of the following?
A) ends the program
B) transfers control out of the current control structure such as a loop or switch statement
C) ends the current line of output, returning the cursor
D) denotes the ending of a switch statement
E) indicates the end of line when using System.out.print
13) If a break occurs within the innermost loop of a nested loop that is three levels deep
A) when the break is encountered just the innermost loop is “broken”
B) when the break is encountered, all loops are “broken” and execution continues from after the
while statement (in our example)
C) when the break is encountered, all but the outermost loops are broken, and execution
continues from the next iteration of the while loop (in our example)
D) this is a syntax error unless there are break or continue statements at each loop level
E) none of the above
8
14) Every Interator
A) has a hasNext( ) method
B) has a hasFirst( ) method
C) has a hasNextInt( ) method
D) has a isEmpty( ) method
E) none of the above
15) If x is an int where x = 1, what will x be after the following loop terminates?
while (x < 100)
x *= 2;
A) 2
B) 64
C) 100
D) 128
E) none of the above, this is an infinite loop
16) If x is an int where x = 0, what will x be after the following loop terminates?
while (x < 100)
x *= 2;
A) 2
B) 64
C) 100
D) 128
E) none of the above, this is an infinite loop
17) How many times will the following loop iterate?
int x = 10;
while (x > 0)
{
System.out.println(x);
x—;
}
A) 0 times
B) 1 time
C) 9 times
D) 10 times
E) 11 times
18) Which of the following are true statements about check boxes?
A) they may be checked or unchecked
B) radio buttons are a special kind of check boxes
C) they are Java components
D) you can control whether or not they will be visible
E) all of the above
19) As introduced in the Software Failure, the terminology “risk analysis” means
A) how willing are you to risk the loss of several key programmers working on your project
B) how much are you willing to risk that a particular piece of software you are developing still
contains an error or errors
C) how willing are you to risk that your software will fail once implemented
D) how willing are you to risk that the machines on which your software will run will not work
E) none of the above
5.2 True/False Questions
1) As in the other members of the C family of languages (C, C++, C#), Java interprets a zero
value as false and a non-zero value as true.
2) In Java, selection statements consist of the if and if-else statements.
3) In Java, the symbol “=” and the symbol “==” are used synonymously (interchangeably).
4) When comparing any primitive type of variable, == should always be used to test to see if two
values are equal.
5) The statement { } is a legal block.
6) The statement if (a >= b) a++; else b—; will do the same thing as the statement if (a < b) b—;
else a++;.
7) An if statement may or may not have an else clause, but an else clause must be part of an if
statement.
8) In order to compare int, float and double variables, you can use <, >, ==, !=, <=, >=, but to
compare char and String variables, you must use compareTo( ), equals( ) and equalsIgnoreCase(
).
9) The expression (!done && x <= y) is true.
10) The expression (s.concat(t).length( ) < y) is true.
11) The expression (done | | s.compareTo(t) < 0) is true.
12) Regarding the Software Failure: The operators were warned that, although the Therac-25
had many safety precautions, it might be possible to accidentally overdose a patient.
1) Rewrite the following set of if statements using a nested if-else structure.
if (score >= 90) grade = ‘A’;
if (score >= 80 && score < 90) grade = ‘B’;
if (score >= 70 && score < 80) grade = ‘C’;
if (score >= 60 && score < 70) grade = ‘D’;
if (score < 60) grade = ‘F’;
2) A truth table shows, for the various true or false values of boolean variables, what the result of
a boolean condition is. Fill in the following truth table. Assume that a, b and c are boolean
variables.
a b c a && (!b | | c)
false false false
false false true
false true false
false true true
true false false
true false true
true true false
true true true
3) A truth table shows, for the various true or false values of boolean variables, what the result of
a boolean condition is. Fill in the following truth table. Assume that a, b and c are boolean
variables.
a b c ! (a && b) | | ! (a && c)
false false false
false false true
false true false
false true true
true false false
true false true
true true false
true true true
4) Explain what is meant by short circuiting and provide an example of short circuiting a
condition with && and provide an example of short circuiting a condition with | |.
5) 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—;
6) String s1 is said to overlap String s2 if all of the characters in s1 also appear in s2. Write a set
of code that will set the boolean variable overlap to true if s1 overlaps s2 and false otherwise.
Assume both s1 and s2 have already been input.