Chapter 5
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;
The condition short circuits and the assignment statement is not executed
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye". The expression (!done && x <= y) is true.
True
The statement if (a >= b) a++; else b--; will do the same thing as the statement if (a < b) b--; else a++;.
True
The statement { } is a legal block.
True
The break statement does which of the following?
transfers control out of the current control structure such as a loop or switch statement
If a break occurs within the innermost loop of a nested loop that is three levels deep
when the break is encountered just the innermost loop is "broken"
Of the following if statements, which one correctly executes three instructions if the condition is true?
{ if (x < 0) a = b * 2; y = x; z = a - y; }
If x is an int where x = 0, what will x be after the following loop terminates? while (x < 100) x *= 2;
64
if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
3
if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?
5
How many times will the following loop iterate? int x = 10; while (x > 0) { System.out.println(x); x--; }
10 times
If x is an int where x = 1, what will x be after the following loop terminates? while (x < 100) x *= 2;
128
if (a > 0) if (b < 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
2
Every Interator
A) has a hasNext( ) method
Which of the following are true statements about check boxes?
All the above
The idea that program instructions execute in order (linearly) unless otherwise specified through a conditional statement is known as
Flow of control
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')
Conditions 2, 3 and 4 are all true, Condition 1 is not
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye". The expression (done | | s.compareTo(t) < 0) is true.
False
For the questions below, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye". The expression (s.concat(t).length( ) < y) is true.
False
In Java, selection statements consist of the if and if-else statements.
False
In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
False
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( ).
False
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.
False
When comparing any primitive type of variable, == should always be used to test to see if two values are equal.
False
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");
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
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';
This code will work correctly only if grade < 70
An if statement may or may not have an else clause, but an else clause must be part of an if statement.
True
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.
True
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?
if (x > 0) x++; if (x < 0) x--; else x = 0;
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;
statement2 will only execute if condition1 is false and condition2 is false