CSC Chapter 3
Which of the following is equivalent to x != y?
! (x == y) x > y || x < y x != y means !(x == y) and x > y || x < y.
Which of the following are so called short-circuit operators?
&& ||
What is y after the following statement is executed? x = 0; y = (x > 0) ? 10 : -10;
-10 This conditional operator is correct. It assigns -10 to y since x > 0 is false.
What is the output from System.out.println((int)Math.random( ) * 4)?
0 Casting is performed before the * operator in (int)Math.random() * 4. So, it returns 0.
Which of the following is a possible output from invoking Math.random()?
0.5 and 0.0 Math.random() returns a real value between 0.0 and 1.0, excluding 1.0.
Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x++ > 10).
10 For the && operator, the right operand is not evaluated, if the left operand is evaluated as false.
Suppose x=10 and y=10. What is x after evaluating the expression (y > 10) && (x-- > 10)?
10 For the && operator, the right operand is not evaluated, if the left operand is evaluated as false.
Suppose x=10 and y=10. What is x after evaluating the expression (y >= 10) || (x-- > 10).
10 For the || operator, the right operand is not evaluated, if the left operand is evaluated as true.
Which of the following operators are right-associative.
=
Which of the following statements are true? A. (x > 0 && x < 10) is same as ((x > 0) && (x < 10)) B. (x > 0 || x < 10) is same as ((x > 0) || (x < 10)) C. (x > 0 || x < 10 && y < 0) is same as (x > 0 || (x < 10 && y < 0)) D. (x > 0 || x < 10 && y < 0) is same as ((x > 0 || x < 10) && y < 0)
A B C In D, && is evaluated before the || operator. So (x > 0 || x < 10 && y < 0) is not same as ((x > 0 || x < 10) && y < 0).
Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2: even = (number % 2 == 0) ? true: false; Code 3: even = number % 2 == 0;
All three are correct, but Code 3 is preferred.
What is the output of the following code? boolean even = false; System.out.println((even ? "true" : "false"));
B false Since even is false, the conditional expression yields false. The correct answer is B.
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; }
C The switch control variable cannot be double.The switch value cannot be a floating-point number. So the correct answer is C
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
Analyze the following code: if (x < 100) && (x > 10) System.out.println("x is between 10 and 100");
The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses. The condition for an if statement must be enclosed in the parentheses. The correct answer is A.
Which of the Boolean expressions below is incorrect?
a (true) && (3 => 4) d (x != 0) || (x = 0) e (-10 < x < 0) a: (3 => 4) should be (3 >= 4), d: (x = 0) should be (x == 0), and e: should be (-10 < x) && (x < 0)
Given |x - 2| <= 4, which of the following is true?
c x - 2 <= 4 && x - 2 >= -4 |x - 2| <= 4 means x - 2 <= 4 and x - 2 > -4. So the correct answer is C.
Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true?
if (isPrime)
Assume x = 4 and y = 5, which of the following is true?
x != 4 ^ y == 5
Given |x - 2| >= 4, which of the following is true?
x - 2 >= 4 || x - 2 <= -4 |x - 2| >= 4 means x - 2 >= 4 and x - 2 <= -4. So B is correct.