COP2258 - Ch 3 Questions
Which of the following statements are true?
(x > 0 && x < 10) is same as ((x > 0) && (x < 10)) (x > 0 || x < 10) is same as ((x > 0) || (x < 10)) (x > 0 || x < 10 && y < 0) is same as (x > 0 || (x < 10 && y < 0))
The order of the precedence (from high to low) of the operators binary +, *, &&, ||, ^ is:
*, +, ^, &&, ||
What is the output from System.out.println((int)Math.random() * 4)?
0
What is the possible output from System.out.println((int)(Math.random() * 4))?
0, 1, 2, or 3
What is y after the following switch statement is executed? int x = 3; int y = 4; switch (x + 3) { case 6: y = 0; case 7: y = 1; default: y += 1; }
2`
The "less than or equal to" comparison operator in Java is ______
<=
Which of the following operators are right-associative.
=
The equal comparison operator in Java is _______________
==
What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5?
There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true.
In Java, the word true is ___________
a boolean literal
Suppose income is 4001, what is the output of the following code? if (income > 3000) { System.out.println("Income is greater than 3000"); } else if (income > 4000) { System.out.println("Income is greater than 4000"); }
income is greater than 3000
The following code displays ___________. double temperature = 50; if (temperature >= 100) System.out.println("too hot"); else if (temperature <= 40) System.out.println("too cold"); else System.out.println("just right");
just right
In a relational data model , __________ provides the means for accessing and manipulating data.
language and SQL
Analyze the following code. boolean even = false; if (even) { System.out.println("It is even!"); }
the code displays nothing
Analyze the following code: boolean even = false; if (even = true) { System.out.println("It is even"); }
the program runs fine and displays even = true
Analyze the following program fragment: int x; double d = 1.5; switch (d) { case 1.0: x = 1; case 1.5: x = 2; case 2.0: x = 3; }
the switch variable cannot be double
What is the value of the following expression? true || true && false
true
Suppose x = 1, y = -1, and z = 1. What is the output of the following statement? (Please indent the statement correctly first.) if (x > 0) if (y > 0) System.out.println("x > 0 and y > 0"); else if (z > 0) System.out.println("x < 0 and z > 0");
x>0 and z<0
What is y displayed in the following code? public class Test1 { public static void main(String[] args) { int x = 1; int y = x = x + 1; System.out.println("y is " + y); } }
y is 2 because x + 1 is assigned to x and then x is assigned to y
Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is the best? I: if (age < 16) System.out.println("Cannot get a driver's license"); if (age >= 16) System.out.println("Can get a driver's license"); II: if (age < 16) System.out.println("Cannot get a driver's license"); else System.out.println("Can get a driver's license"); III: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age >= 16) System.out.println("Can get a driver's license"); IV: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age > 16) System.out.println("Can get a driver's license"); else if (age == 16) System.out.println("Can get a driver's license");
II is the best
Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is correct? I: if (age < 16) System.out.println("Cannot get a driver's license"); if (age >= 16) System.out.println("Can get a driver's license"); II: if (age < 16) System.out.println("Cannot get a driver's license"); else System.out.println("Can get a driver's license"); III: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age >= 16) System.out.println("Can get a driver's license"); IV: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age > 16) System.out.println("Can get a driver's license"); else if (age == 16) System.out.println("Can get a driver's license");
all correct statments
Analyze the following code: Code 1: int number = 45; boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: int number = 45; boolean even = (number % 2 == 0);
both codes are correct but code 2 is better
Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.
if (isPrime)
