Control Statements in java IF, ELSE and ELSE IF Tutorial And MCQ
Table of Contents
Study and learn Interview MCQ Questions and Answers on IF , ELSE , ELSE IF Control Statements in java . Attend job interviews easily with these Multiple Choice Questions
Control Statements in java
All control statements take the boolean value as an INPUT and branches control accordingly. Usually, the true condition takes control to one branch of code and the false condition takes control to another branch. The only exception to this is a SWITCH statement which can accept integers like byte, short, int and enum, character type data like char and String.
Control Statements
Type of Control Statements in java
Java language has three types of Control Statements.
- Selection Statements
- Loop or Iteration Statements
- Jump Statements
1. Selection Control Statements in java
A Selection control statement takes a condition (boolean value or relational expression) as input and branches to one of the two possible branching point statements.
If the condition is true, One set of instructions or statements are executed and if the condition is false, another set of instructions are executed.
There are two types of Selection Control Statements.
- IF statement (IF ELSE statements)
- SWITCH statement
2. Loop Control Statements in java
Loop control statements execute a particular set of instructions repeatedly as long as the Loop Condition is satisfied i.e true condition.
There are four types of Loop Control Statements
- WHILE
- DO WHILE
- FOR
- FOR EACH or Advanced FOR or Enhanced FOR
3. Jump Control Statements in java
Jump control statements take the execution control by skipping a number of statements in between. Unlike other control statements, which go in a predictable linear way of execution, Jump control statements work in a non-linear nested way by skipping and jumping a number of statements.
There are three Jump Control statements.
- BREAK
- CONTINUE
- RETURN
Java IF, ELSE and ELSE IF control statements
Java language continues the same features of IF ELSE statements introduced by the C Language. Programmers heavily depend of IF ELSE statements to create good java products with efficiency. Remember that you can put any number of white spaces like TAB, New Line and Space between two tokens or keywords of a Java program. You can write all IF statements of a program in just one line though it is not reading friendly.
1. IF Control Statement
IF statement takes a condition as an input. This condition should evaluate to a boolean type constant like true or false. If you do not use Braces, only one statement or Single statement can be specified inside an if statement. With the use of Braces, you can add any number of statements inside a Single IF statement.
If the condition evaluates to true, statements below IF will be executed. Otherwise, program control goes to the next statement after the IF block.
Syntax:
if( CONDITION ) //STATEMENT; (or) if( CONDITION ) { Statement1; Statement2; ... }
Example: IF statement
The second statement is not part of IF(a<10). Use braces to add if required.
class IFStatement { public static void main(String args[]) { int a=11; if(a<10) System.out.println(a + "<10"); System.out.println("AFRICA"); int b=8; if(b<10) { System.out.println(b + "<10"); System.out.println("POLAND"); } } } //OUTPUT //AFRICA //8<10 //POLAND
2. IF ELSE Control Statement
ELSE statement is a companion to the IF statement. You can not use ELSE without using the IF statement. ELSE block is executed only if IF condition fails or it is false.
Syntax:
if( CONDITION ) //STATEMENT1; else //STATEMENT2 (or) if( CONDITION ) { Statement1; Statement2; ... } else { Statement8; Statement9; ... }
Example: IF ELSE statement with String comparison
“str” is compared with “str2”. Both the String variables hold the same value “ELEPHANT”. So, IF condition evaluates to true and the corresponding statements are executed.
class IF_ELSE_Statement { public static void main(String args[]) { String str="ELEPHANT"; String str2= "ELEPHANT"; if(str == str2) { System.out.println("IF Condition"); } else { System.out.println("ELSE Condition"); } if(str != str2) { System.out.println("IF Condition"); } else { System.out.println("ELSE Condition"); } } } //OUTPUT //IF Condition //ELSE Condition
3. IF ELSE IF Control Statement
IF ELSE-IF ladder is useful to execute a different set of statements for different conditions. We can use as many ELSE-IF statements as we want. Do note that the ELSE statement or block at the end of IF-ELSE-IF is not mandatory or it is optional.
If any of IF or ELSE IF condition matches control goes inside that IF or ELSE IF and then exits without going inside ELSE block at the last.
“float” is promoted to type “double” in this example.
Example:
public class IF_ELSE_IF { public static void main(String[] args) { float a = 1.5f; float b = 2.5f; if(a > 2) { System.out.println("A>2"); } else if(b>2) { System.out.println("B>2"); } else if(a>1) { System.out.println("A>1"); } else { System.out.println("UNKNOWN"); } } } //OUTPUT //B>2
4. NESTED IF, ELSE and ELSE-IF Control Statements
You can nest an IF inside another IF. and You can nest an IF ELSE inside another IF. You can nest IF ELSE inside another ELSE or ELSE-IF and so on. Carefully use braces to demarcate one block from another block. You can add as many conditions as you want. Some programmers write multiple conditions on a single line with the help of relational operators and a single IF.
Example:
public class NESTED_IF { public static void main(String[] args) { float a = 1.5f; float b = 2.5f; if(a > 1) { if(b>=2) { System.out.println("A>1 AND B>2"); } else { System.out.println("A>1 AND B<2"); } } else if(a>2) { System.out.println("A>2"); } else { System.out.println("UNKNOWN"); } } } //OUTPUT //A>1 AND B>2
[WpProQuiz 152]
Control Statements in java Interview MCQ
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
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
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
Answer [=] A
Explanation:
Single line of code does not need Braces {} if(a>9) System.out.println("NINE");
9) The code inside an ELSE statement may be surrounded by Braces ?
A) FALSE
B) TRUE
Answer [=] B
Explanation:
if(true) { } else { //code line 1 //code line 2 }
10) An ELSE or ELSE-IF statement in Java can not exist alone without IF statement ?
A) FALSE
B) TRUE
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 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