Java IF ELSE IF Control Statements Interview MCQ Questions and Answers

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.



1) An IF-ELSE statement is also called ___.
A) Branching statement
B) Control statement
C) Block statements
D) All
Answer [=]
D
Explanation:

Block statements are those that are usually surrounded by Braces { and }. So, a WHILE statement is also called a Block statement.

2) An IF or ELSE IF statement accepts ___ as input before branching.
A) boolean
B) int
C) float
D) char
Answer [=]
A
3) An IF statement in Java is also a ___ statement.
A) boolean
B) conditional
C) iterative
D) optional
Answer [=]
B
4) Java style IF-ELSE statements are similar to ___.
A) C style
B) C++ Style
C) Both C and C++ style
D) None
Answer [=]
C
5) State TRUE or FALSE. Every IF statement must be followed by an ELSE of ELSE-IF statement.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
B
6) An ELSE statement must be preceded by ___ statement in Java.
A) IF
B) ELSE IF
C) IF or ELSE IF
D) None
Answer [=]
C
7) State TRUE or FALSE. A Single-Line comment or Multiline-comment is allowed in between if () and Left Brace ( { ).
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
B
Explanation:
if(a>10)//testing comment
{

}


8) State TRUE or FALSE. An IF statement code must be defined in between two Braces.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
A
Explanation:
Single line of code does not need Braces {}
if(a>9)
  System.out.println("NINE");
9) State TRUE of FALSE. The code inside an ELSE statement may be surrounded by Braces.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
B
Explanation:
if(true)
{ }
else
{
  //code line 1
  //code line 2
}
10) State TRUE or FALSE. An ELSE or ELSE-IF statement in Java can not exist alone without IF statement.
A) FALSE
B) TRUE
C) -
D) -
Answer [=]
B
11) The condition of an IF statement evaluates to boolean only if the expression contains?
A) logical operators
B) relational operators
C) boolean operands
D) All
Answer [=]
D
12) If the condition of an IF-statement is false, which is true below.
A) IF block is executed.
B) ELSE block is executed.
C) Both IF and ELSE blocks are skipped.
D) Both IF and ELSE blocks are executed.
Answer [=]
B
Explanation:

If the condition is TRUE, IF-block is executed. Otherwise, ELSE-block is executed.

13) What is maximum lines of code that can be written inside a Java style IF, ELSE or IF-ELSE block?
A) 32
B) 64
C) 512
D) None
Answer [=]
D
Explanation:

There is no such limit on the number of lines of code in any block or statement in Java.

14) An IF-ELSE statement is better than a SWITCH statement in which scenario below?
A) Checking for More-than condition
B) Checking for Less-than condition
C) Checking for Ranges
D) All
Answer [=]
D
Explanation:
if(a>10 && a<20) { }


15) What is the maximum number of ELSE-IF statements that can present in between starting IF and ending ELSE statements?
A) 32
B) 64
C) 128
D) None
Answer [=]
D
Explanation:

You can write any number of ELSE-IF statements in a Java program.

16) Choose the correct syntax of Java IF statement below.
A)
if(condition)
  //statement
B)
if(condition)
{
  //statement
}
C)
if(condition)
{
  //statement1
  //statement2
}
D) All
Answer [=]
D
17) Choose a wrong statement on Java IF-ELSE syntax below.
A)
if(condition)
  //statement1
else
  //statement2
B)
else
  //statement2
C)
if(condition1)
  //statement1
else if(condition2)
  //statement2
D)
if(condition1)
  //statement1
else if(condition2)
  //statement2
else
  //statement3
Answer [=]
B
Explanation:

"ELSE" and "ELSE IF " statements should always be preceded by a valid IF statement.

18)
What is the output of Java program with IF statement?
if(1)
{
  System.out.println("OK");
}
A) OK
B) No output
C) Compiler error
D) None
Answer [=]
C
Explanation:

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
19) What is the output of the Java program with IF-ELSE statements?
if(TRUE)
  System.out.println("GO");
else
  System.out.println("STOP");
A) GO
B) STOP
C) Compiler error
D) None
Answer [=]
C
Explanation:
Error: TRUE cannot be resolved to a variable
20) What is the output of the Java program?
int a=10;
if(a==9)
  System.out.println("OK ");
  System.out.println("MASTER");					
else
  System.out.println("BYE");
A) OK MASTER
B) BYE
C) Compiler error
D) None
Answer [=]
C
Explanation:

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");
21) What is the output of the Java program?
String name1="FOX", name2="DOG";
if(name1 == "FOX")
  System.out.print("FOX ");
  System.out.println("GOOD");					
if(name2 == "CAT")
  System.out.println("DINO");
A) FOX DINO
B) FOX GOOD DINO
C) Compiler error
D) None
Answer [=]
B
Explanation:

The second Print statement "GOOD" is always executed.



22) What is the output of the Java program?
String name="dino";
if(name == "dino")
	System.out.print("DINO");
System.out.println("GOOD");	
A) DINO GOOD
B) DINO
C) GOOD
D) Compiler error
Answer [=]
C
Explanation:

The second Print statement is not part of the IF statement. So it is always executed.

23) What is the output of the Java program with IF-ELSE-IF statements?
int marks=55;
if(marks >= 80)
  System.out.println("DISTINCTION");
else if(marks >=35)
  System.out.println("PASS");
else
  System.out.println("FAIL");
A) DISTINCTION
B) PASS
C) FAIL
D) Compiler error
Answer [=]
B
24) What is is the output of the Java program?
int marks=85;
if(marks >= 80)
  System.out.println("DISTINCTION");
else if(marks >=35)
  System.out.println("PASS");
A) DISTINCTION
B) PASS
C) Compiler error
D) None
Answer [=]
A
Explanation:

It is ok to skip the ELSE statement.

25) What is the output of Java program below?
float temp = 98.4f;
if(temp > 98.4)
{
  System.out.println("SUMMER");
}
else
{
  System.out.println("UNKNOWN");
}
A) SUMMER
B) UNKNOWN
C) Compiler error
D) None
Answer [=]
A
26) What is the output of the Java program?
long num = 123L;
if(num > 123)
{
	System.out.println("TIGER");
}
else
{
	System.out.println("BIRD");
}
A) TIGER
B) BIRD
C) Compiler error
D) None
Answer [=]
B
27) What is the output of the Java program?
int horses = 10;
int camels = 5;
if(horses > 5)
{
  if(camels > 3)
  {
    System.out.println("FOREST");
  }
}
else
{
   System.out.println("CITY");
}
A) FOREST
B) CITY
C) Compiler error
D) None
Answer [=]
A
Explanation:

Nesting of IF and ELSE is allowed in Java.

28) What is the output of the Java program?
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");
A) TOWN
B) FOREST AMAZON
C) UNKNOWN
D) Compiler error
Answer [=]
D
Explanation:

ELSE-IF statement should not contain more than one statement without braces { }.

29) What is the output of the Java program?
int marks=29;
if(marks > 29);
   System.out.print("PASS ");
System.out.println("RANK");	
A) RANK
B) PASS
C) PASS RANK
D) Compiler error
Answer [=]
C
Explanation:

Notice the immediate Semicolon (;) after the IF. It ends the IF block. So whatever is next or below it will be executed always.

30) What is the output of the Java program below?
if(3>1)
{
  4;
}
A) 0
B) 4
C) Compiler error
D) None
Answer [=]
C
Explanation:
4; is not a valid statement.
31) What is the output of the Java program with IF statement?
if(true)
{
   break;
   System.out.println("ELEPHANT");
}
A) No output
B) ELEPHANT
C) Compiler error
D) None
Answer [=]
C
Explanation:
Error: break cannot be used outside of
 a loop or a switch


 

 

ExamTray App is now Available on Google Play EXAMTRAY android app link