Chapter 3 Check Point Questions
Section 3.2
▼3.2.1
List six relational operators.
▼3.2.2
Assuming that x is 1, show the result of the following Boolean expressions:
x > 0 x < 0 x != 0 x >= 0 x != 1
▼3.2.3
Can the following conversions involving casting be allowed? Write a test program to verify it.
boolean b = true; i = (int)b; int i = 1; boolean b = (boolean)i;
Section 3.3
▼3.3.1
Write an if statement that assigns 1 to x if y is greater than 0.
▼3.3.2
Write an if statement that increases pay by 3% if score is greater than 90.
▼3.3.3
What is wrong in the following code?
if radius >= 0 { area = radius * radius * PI; System.out.println("The area for the circle of " + " radius " + radius + " is " + area); }
Section 3.4
▼3.4.1
Write an if statement that increases pay by 3% if score is greater than 90, otherwise increases pay by 1%.
▼3.4.2
What is the output of the code in (a) and (b)
if number is 30? What if number is 35?
(a) if (number % 2 == 0) System.out.println(number + " is even."); System.out.println(number + " is odd."); (b) if (number % 2 == 0) System.out.println(number + " is even."); else System.out.println(number + " is odd.");
Section 3.5
▼3.5.1
Suppose x = 3 and y = 2; show the output, if any, of the following code. What is the output if x = 3 and y = 4? What is the output if x = 2 and y = 2? Draw a flowchart of the code.
if (x > 2) { if (y > 2) { z = x + y; System.out.println("z is " + z); } } else System.out.println("x is " + x);
▼3.5.2
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);
▼3.5.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");
Section 3.6
▼3.6.1
Which of the following statements are equivalent? Which ones are correctly indented?
(a) if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0; (b) if (i > 0) { if (j > 0) x = 0; else if (k > 0) y = 0; } else z = 0; (c) if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0; (d) if (i > 0) if (j > 0) x = 0; else if (k > 0) y = 0; else z = 0;
▼3.6.2
Rewrite the following statement using a Boolean expression:
if (count % 10 == 0) newLine = true; else newLine = false;
▼3.6.3
Are the following statements correct? Which one is better?
(a) if (age < 16) System.out.println ("Cannot get a driver's license"); if (age >= 16) System.out.println ("Can get a driver's license"); (b) if (age < 16) System.out.println ("Cannot get a driver's license"); else System.out.println ("Can get a driver's license");
▼3.6.4
What is the output of the following code if number is 14, 15, or 30?
(a) if (number % 2 == 0) System.out.println (number + " is even"); if (number % 5 == 0) System.out.println (number + " is multiple of 5"); (b) if (number % 2 == 0) System.out.println (number + " is even"); else if (number % 5 == 0) System.out.println (number + " is multiple of 5");
Section 3.7
▼3.7.1
Which of the following is a possible output from invoking Math.random()?
323.4, 0.5, 34, 1.0, 0.0, 0.234
▼3.7.2
a. How do you generate a random integer i such that 0 <= i < 20 ?
b. How do you generate a random integer i such that 10 <= i < 20
c. How do you generate a random integer i such that 10 <= i <= 50
d. Write an expression that returns 0 or 1 randomly.
b. How do you generate a random integer i such that 10 <= i < 20
c. How do you generate a random integer i such that 10 <= i <= 50
d. Write an expression that returns 0 or 1 randomly.
Section 3.9
▼3.9.1
Are the following two statements equivalent?
(a) if (income <= 10000) tax = income * 0.1; else if (income <= 20000) tax = 1000 + (income - 10000) * 0.15; (b) if (income <= 10000) tax = income * 0.1; else if (income > 10000 && income <= 20000) tax = 1000 + (income - 10000) * 0.15;
Section 3.10
▼3.10.1
Assuming that x is 1, show the result of the following Boolean expressions.
(true) && (3 > 4) !(x > 0) && (x > 0) (x > 0) || (x < 0) (x != 0) || (x == 0) (x >= 0) || (x < 0) (x != 1) == !(x == 1)
▼3.10.2
(a) Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100.
(b) Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100 or the number is negative.
(b) Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100 or the number is negative.
▼3.10.3
(a) Write a Boolean expression for |x - 5| < 4.5.
(b) Write a Boolean expression for |x - 5| > 4.5.
(b) Write a Boolean expression for |x - 5| > 4.5.
▼3.10.4
Assume that x and y are int type. Which of the following are legal Java expressions?
x > y > 0 x = y && y x /= y x or y x and y (x != 0) || (x = 0)
▼3.10.5
Are the following two expressions the same?
(a) x % 2 == 0 && x % 3 == 0 (b) x % 6 == 0
▼3.10.6
What is the value of the expression x >= 50 && x <= 100 if x is 45, 67, or 101?
▼3.10.7
Suppose, when you run the following program, you enter the input 2 3 6 from the console. What is the output?
public class Test { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); double x = input.nextDouble(); double y = input.nextDouble(); double z = input.nextDouble(); System.out.println("(x < y && y < z) is " + (x < y && y < z)); System.out.println("(x < y || y < z) is " + (x < y || y < z)); System.out.println("!(x < y) is " + !(x < y)); System.out.println("(x + y < z) is " + (x + y < z)); System.out.println("(x + y > z) is " + (x + y > z)); } }
▼3.10.8
Write a Boolean expression that evaluates to true if age is greater than 13 and less than 18.
▼3.10.9
Write a Boolean expression that evaluates to true if weight is greater than 50 pounds or height is greater than 60 inches.
▼3.10.10
Write a Boolean expression that evaluates to true if weight is greater than 50 pounds and height is greater than 60 inches.
▼3.10.11
Write a Boolean expression that evaluates to true if either weight is greater than 50 pounds or height is greater than 60 inches, but not both.
Section 3.11
▼3.11.1
How many days in the February of a leap year? Which of the following is a leap year? 500, 1000, 2000, 2016, and 2020?
Section 3.12
▼3.12.1
What happens if you enter an integer as 05?
Section 3.13
▼3.13.1
What data types are required for a switch variable? If the keyword break is not used after a case is processed, what is the next statement to be executed? Can you convert a switch statement to an equivalent if statement, or vice versa? What are the advantages of using a switch statement?
▼3.13.2
What is y after the following switch statement is executed? Rewrite the code using an if-else statement.
x = 3; y = 3; switch (x + 3) { case 6: y = 1; default: y += 1; }
▼3.13.3
What is x after the following if-else statement is executed? Use a switch statement to rewrite it and draw the flowchart for the new switch statement.
int x = 1, a = 3; if (a == 1) x += 5; else if (a == 2) x += 10; else if (a == 3) x += 16; else if (a == 4) x += 34;
▼3.13.4
Write a switch statement that displays Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, if day is 0, 1, 2, 3, 4, 5, 6, accordingly.
▼3.13.5
Rewrite the switch statement in Listing 3.8 using an if-else statement.
Section 3.14
▼3.14.1
Suppose that, when you run the following program, you enter the input 2 3 6 from the console. What is the output?
public class Test { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); double x = input.nextDouble(); double y = input.nextDouble(); double z = input.nextDouble(); System.out.println((x < y && y < z) ? "sorted" : "not sorted"); } }
▼3.14.2
Rewrite the following if statements using the conditional operator.
if (ages >= 16) ticketPrice = 20; else ticketPrice = 10;
▼3.14.3
Rewrite the following conditional expressions using if-else statements.
a. score = (x > 10) ? 3 * scale : 4 * scale;
b. tax = (income > 10000) ? income * 0.2 : income * 0.17 + 1000;
c. System.out.println((number % 3 == 0) ? i : j);
a. score = (x > 10) ? 3 * scale : 4 * scale;
b. tax = (income > 10000) ? income * 0.2 : income * 0.17 + 1000;
c. System.out.println((number % 3 == 0) ? i : j);
▼3.14.4
Write conditional expression that returns -1 or 1 randomly.
Section 3.15
▼3.15.1
List the precedence order of the Boolean operators. Evaluate the following expressions:
true || true && false true && true || false
▼3.15.2
True or false? All the binary operators except = are left associative.
▼3.15.3
Evaluate the following expressions:
2 * 2 - 3 > 2 && 4 - 2 > 5 2 * 2 - 3 > 2 || 4 - 2 > 5
▼3.15.4
Is x > 0 && x < 10 the
same as x > 0 && x < 10?
Is x > 0 || x < 10 the same as x > 0 || x < 10?
Is (x > 0 || x < 10) && y < 0 the same as (x > 0 || (x < 10 && y < 0))?
Is x > 0 || x < 10 the same as x > 0 || x < 10?
Is (x > 0 || x < 10) && y < 0 the same as (x > 0 || (x < 10 && y < 0))?