Chapter 3: Selections Flashcards
What is wrong with the following code?
if i > 0 {
System.out.println(“i is positive”);
}
No paraentheses around the boolean expression (i > 0)
What can be changed about the following code and why?
if (i > 0) {
System.out.println(“i is positive”);
}
Braces can be ommitted if the enclose a single statement.
Write a statement that assigns 1 to x if y is greater than 0.
if (y > 0){
x = 1;
}
Write an if statment that increases pay by 3% if score is greater than 90.
if (score > 90) {
pay *= 1.03
}
What is the output of the following code?
int x = 0;
if (x x = x + 1;
}
System.out.println(“x is “ + x);
“x is 1”
Suppose x = 2 and y = 3. Show the output, if any, of the following code. What is the output if x = 3 and y = 2? What is the output if x = 3 and y = 3?
if (x > 2)
if (y > 2) {
int z = x + y;
System.out.println(“z is “ + z);
}
else
System.out.println(“x is “ + x);
No output if x = 2 and y = 3. Output is “x is 3” if x = 3 and y = 2. Output is “z is 6” if x = 3 and y = 3.
What is wrong in the following code?
if (score >= 60.0)
System.out.println(“D”);
else if (score >= 70.0)
System.out.println(“C”);
else if (score >= 80.0)
System.out.println(“B”);
else if (score >= 90.0)
System.out.println(“A”);
else
System.out.println(“F”);
Wrong order. A score of lets say 95, will end up being a D.
The following statement will evaluate to…
(x != 1) == !(x == 1);
True
Write a boolean expression if the number storred in variable num is between 1 and 100.
(num > 1) && (num
Write a boolean expression for |x-5| < 4.5
(x-5 -4.5)
Correct:
(-10 < x < 0)
(-10 < x) && (x < 0)