Assume x is 0. What is the output of the following statement? if (x > 0) System.
ID: 641718 • Letter: A
Question
Assume x is 0. What is the output of the following statement?
if (x > 0)
System.out.print("x is greater than 0");
else if (x < 0)
System.out.print("x is less than 0");
else
System.out.print("x equals 0");
a. x is less than 0
b. x is greater than 0
c. x equals 0
d. None
4. Analyze the following code:
Code 1:
boolean even;
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
boolean even = (number % 2 == 0);
a. Code 2 has syntax errors.
b. Code 1 has syntax errors.
c. Both Code 1 and Code 2 have syntax errors.
d. Both Code 1 and Code 2 are correct, but Code 2 is better.
Explanation / Answer
1 Assume x is 0. What is the output of the following statement?
c. x equals 0
4 Analyze the following code:
Code 1:
boolean even;
if (number % 2 == 0)
even = true;
else
even = false;
Code 2:
boolean even = (number % 2 == 0);
d. Both Code 1 and Code 2 are correct, but Code 2 is better.