Study and learn Interview MCQ Questions and Answers on Java IF ELSE IF Control Statements. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on IF ELSE Control Statements before reading these objective questions.
Block statements are those that are usually surrounded by Braces { and }. So, a WHILE statement is also called a Block statement.
if(a>10)//testing comment { }
Single line of code does not need Braces {} if(a>9) System.out.println("NINE");
if(true) { } else { //code line 1 //code line 2 }
If the condition is TRUE, IF-block is executed. Otherwise, ELSE-block is executed.
There is no such limit on the number of lines of code in any block or statement in Java.
if(a>10 && a<20) { }
You can write any number of ELSE-IF statements in a Java program.
if(condition) //statement
if(condition) { //statement }
if(condition) { //statement1 //statement2 }
if(condition) //statement1 else //statement2
else //statement2
if(condition1) //statement1 else if(condition2) //statement2
if(condition1) //statement1 else if(condition2) //statement2 else //statement3
"ELSE" and "ELSE IF " statements should always be preceded by a valid IF statement.
What is the output of Java program with IF statement? if(1) { System.out.println("OK"); }
The condition inside an IF statement should evaluate to either true/false. The below error is triggered.
Type mismatch: cannot convert from int to boolean
if(TRUE) System.out.println("GO"); else System.out.println("STOP");
Error: TRUE cannot be resolved to a variable
int a=10; if(a==9) System.out.println("OK "); System.out.println("MASTER"); else System.out.println("BYE");
More than 1 statement must be kept within Braces { } if ELSE or ELSE IF is next to the IF statement.
if(a==9) { System.out.println("OK "); System.out.println("MASTER"); } else System.out.println("BYE");
String name1="FOX", name2="DOG"; if(name1 == "FOX") System.out.print("FOX "); System.out.println("GOOD"); if(name2 == "CAT") System.out.println("DINO");
The second Print statement "GOOD" is always executed.
String name="dino"; if(name == "dino") System.out.print("DINO"); System.out.println("GOOD");
The second Print statement is not part of the IF statement. So it is always executed.
int marks=55; if(marks >= 80) System.out.println("DISTINCTION"); else if(marks >=35) System.out.println("PASS"); else System.out.println("FAIL");
int marks=85; if(marks >= 80) System.out.println("DISTINCTION"); else if(marks >=35) System.out.println("PASS");
It is ok to skip the ELSE statement.
float temp = 98.4f; if(temp > 98.4) { System.out.println("SUMMER"); } else { System.out.println("UNKNOWN"); }
long num = 123L; if(num > 123) { System.out.println("TIGER"); } else { System.out.println("BIRD"); }
int horses = 10; int camels = 5; if(horses > 5) { if(camels > 3) { System.out.println("FOREST"); } } else { System.out.println("CITY"); }
Nesting of IF and ELSE is allowed in Java.
int horses = 10; int camels = 5; if(horses < 5) { System.out.println("TOWN"); } else if(horses >=5) System.out.print("FOREST "); System.out.println("AMAZON"); else System.out.println("UNKNOWN");
ELSE-IF statement should not contain more than one statement without braces { }.
int marks=29; if(marks > 29); System.out.print("PASS "); System.out.println("RANK");
Notice the immediate Semicolon (;) after the IF. It ends the IF block. So whatever is next or below it will be executed always.
if(3>1) { 4; }
4; is not a valid statement.
if(true) { break; System.out.println("ELEPHANT"); }
Error: break cannot be used outside of a loop or a switch