How do we use the if/else statement in Java?

Java 11 Certification Practice Questions (For the complete list, refer http://talks.skilltoz.com/java-11-certification-exam-questions/

Topic: Controlling Program Flow

Create and use loops, if/else, and switch statements

Q. What is the result of this?
int i = 1;
char j = 10;
long l = -i + 3 * i / 10;
System.out.println(l);
if (l <= 40 % j)
if (--i >0 )
System.out.println("A");
else
System.out.println("B");
else
System.out.println("C");
Choices

A. Compiler Error
B. Prints “A”
C. Prints “B”
D. Prints “D”
E. Exception at runtime

Answer

Choice B is correct. The code compiles and runs without issues. The value of the variable l becomes -1+(3*0), which is -1 itself. The first if statement evaluates to true because 40%10 is 0 and -1 is less than 0. Hence, the second if statement is evaluated, which is false because –i is 0 which is not greater than 0. Hence, the control goes to the else part which prints “B”. Thus, B the correct answer.

References

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

Leave a Reply

Your email address will not be published. Required fields are marked *