if (x < 0)
{
System.out.println("x is negative");
}
else if (x == 0)
{
System.out.println("x is zero");
}
else
{
System.out.println("x is positive");
}
x is negative
This will only print if x has been set to a number less than zero. Has it?
x is zero
This will only print if x has been set to 0. Has it?
x is positive
The first condition is false and x is not equal to zero so the else will execute.
if (!(x < 3 || y > 2))
System.out.println("first case");
else
System.out.println("second case");
first case
This will print if x is greater than or equal 3 and y is less than or equal 2. In this case x is greater than 3 so the first condition is true, but the second condition is false.
second case
This will print if x is less than 3 or y is greater than 2.
At a certain high school students receive letter grades based on the following scale: 93 or above is an A, 84 to 92 is a B, 75 to 83 is a C, and below 75 is an F. Which of the following code segments will assign the correct string to grade for a given integer score?
I. if (score >= 93)
grade = "A";
if (score >= 84 && score < 93)
grade = "B";
if (score >=75 && score < 84)
grade = "C";
if (score < 75)
grade = "F";
II. if (score >= 93)
grade = "A";
if (score >= 84)
grade = "B";
if (score >=75)
grade = "C";
if (score < 75)
grade = "F";
III. if (score >= 93)
grade = "A";
else if (score >= 84)
grade = "B";
else if (score >= 75)
grade = "C";
else
grade = "F";
I and III only
Choice I uses multiple ifβs with logical ands in the conditions to check that the numbers are in range. Choice II wonβt work since if you had a score of 94, it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". Choice III uses ifs with else if to make sure that only one conditional is executed.
II only
Choice II wonβt work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.
III only
III is one of the correct answers. However, choice I is also correct. Choice I uses multiple ifβs with logical ands in the conditions to check that the numbers are in range. Choice III uses ifs with else if to make sure that the only one conditional is executed.
I and II only
Choice II wonβt work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.
I, II, and III
Choice II wonβt work since if you had a score of 94 it would first assign the grade to an "A" but then it would execute the next if and change the grade to a "B" and so on until the grade was set to a "C". This could have been fixed by using else if instead of just if.
This canβt be right as itβs only checking the x variable, however the original statement can solely depend on the y variable in some cases.
(y < 20) || (x > 15 && x < 18)
Thereβs a third condition on x that can affect the output of the statement which is not considered in this solution.
((x > 10) || (x > 15 && x < 18)) || (y < 20)
The commutative property allows the terms to be switched around, while maintaining the value. In this case, the || symbol is used with the commutative property and the statement included the && must stay together to follow the laws of logic.
(x < 10 && y > 20) && (x < 15 || x > 18)
This is the negation of the original statement, thus returning incorrect values.
int x = 3;
int y = 2;
if (x > 2)
x++;
if (y > 1)
y++;
if (x > 2)
System.out.print("first ");
if (y < 3)
System.out.print("second ");
System.out.print("third");
first
This will print, but so will something else.
first second
Are you sure about the "second"? This only prints if y is less than 3, and while it was originally, it changes.
first second third
Are you sure about the "second"? This only prints if y is less than 3, and while it was originally, it changes.
first third
The first will print since x will be greater than 2 and the second wonβt print since y is equal to 3 and not less than it. The third will always print.
int x = 3;
int y = 2;
if (y / x > 0)
System.out.print("first ");
System.out.print("second ");
first
When you do integer division you get an integer result so y / x == 0 and is not greater than 0.
second
The first will not print because integer division will mean that y / x is 0. The second will print since it is not in the body of the if (it would be if there were curly braces around it).
first second
Do you see any curly braces? Indention does not matter in Java.
Nothing will be printed
This would be true if there were curly braces around the two indented statements. Indention does not matter in Java. If you donβt have curly braces then only the first statement following an if is executed if the condition is true.