Register for 45 Day Coding Challenge by CodeStudio And Win Some Exciting Prizes

# If-Else Statement MCQ in Java-2

Q1) An IF-ELSE statement is also called _____.

a) Branching statement
b) Control statement
c) Block statements
d) All

Q2) Which of these are selection statements in Java?

a) if()
b) for()
c) continue
d) break

Here if is a selection statement. Ans for() is a looping statement. Continue and break are jump statements.

Q3) What is the maximum number of ELSE-IF statements that can be present in between starting IF and ending ELSE statements?

a) 32
b) 64
c) 128
d) None

We can write any number of ELSE-IF statements in a Java program. There is no restriction.

Q4) Which of these selection statements test only for equality?

a) if
b) switch
c) if & switch
d) none of the mentioned

Q5) An ELSE or ELSE-IF statement in Java can not exist alone without an IF statement.

a) True
b) False

We can’t write ELSE or ELSE-IF statements in Java without an IF statement.

Q6) What is the output of the below program?

``````public class Main
{
public static void main(String[] args) {
int a = 25;
if(a > 5)
System.out.print("Hi ");
if(a < 20)
System.out.print("Hello ");
else
System.out.print("Know Program ");
}
}
``````

a) Hi
b) Hello
c) Know Program
d) Hi Know Program
e) Hello Know Program

Here 25>5 => true; therefore the first if-statement will be executed. In the second if-statement 25 < 20 => false; therefore else block will be executed.

Q7) What is the output of the given below program?

``````public class Main
{
public static void main(String[] args) {
float a = 7.3f;

if(a == 7.3)
System.out.print("Hi");
else
System.out.print("Know Program");
}
}
``````

a) Hi
b) Know Program
c) Compiled Successfully, No Output.
d) Compile-time error

The 7.3 is a double type value but the “a” variable is float type. Both are not the same, hence the condition becomes false, and the ELSE block will be executed.

Q8) Find the output of the given Java program?

``````public class Main
{
public static void main(String[] args) {
double a = 7.3;

if(a = 7.3)
System.out.print("Hi");
else
System.out.print("Know Program");
}
}
``````

a) Hi
b) Know Program
c) Compiled Successfully, No Output.
d) Compile-time error

In conditions, we must use boolean type value. Whenever we use comparisons then it generates a boolean value, but here we are assigning value to the variable. Hence it gives compile-time error.

Q9) What is the output of the given below program?

``````public class Main {
public static void main(String[] args) {

boolean x = true;
boolean y = false;
boolean z = true;

if(x && y || y && z)
System.out.print("Hi");
else
System.out.print("Know Program");
}
}
``````

a) Hi
b) Know Program
c) Compiled Successfully, No Output.
d) Compile-time error

Q10) Find the output of the given Java program?

``````public class Main {
public static void main(String[] args) {

boolean x = true;
boolean y = false;
boolean z = true;

if(x & y | y & z | z)
System.out.print("Hi");
else
System.out.print("Know Program");
}
}``````

a) Hi
b) Know Program
c) Compiled Successfully, No Output.
d) Compile-time error