If-Else Statement MCQ in Java-1

Q1) Java-style IF-ELSE statements are similar to ____.

a) C style
b) C++ Style
c) Both C and C++ style
d) None

View Answer Answer:- c) Both C and C++ style

Q2) An ELSE statement must be preceded by ____ statement in Java.

a) IF
b) ELSE IF
c) IF or ELSE IF
d) None

View Answer Answer:- c) IF or ELSE IF

Q3) Choose the correct syntax of the Java IF statement below.

a)

if(condition)
  //statement

b)

if(condition)
{
  //statement
}

c)

if(condition)
{
  //statement1
  //statement2
}

d) All

View Answer Answer:- d) All

Q4) An IF statement in Java is also a ____ statement.

a) boolean
b) conditional
c) iterative
d) optional

View Answer Answer:- b) conditional

Q5) Every IF statement must be followed by an ELSE of ELSE-IF statement.

a) True
b) False

View Answer Answer:- b) FALSE

Q6) Find the output of the given Java program?

public class Main
{
  public static void main(String[] args) {
    if(1>3)
      System.out.println("Know Program");
  }
}

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

View Answer Answer:- c) Compiled Successfully, No Output.

Here 1>3 hence condition becomes false because 1 is not greater than 3. Therefore if block will not be executed.

Q7) Find the output of the given Java program?

public class Main
{
  public static void main(String[] args) {
    if(5<4)
      System.out.println("Hi");
    else
      System.out.println("Hello");
  }
}

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

View Answer Answer:- b) Hello

Here 5<4 hence the condition becomes false because 5 is not lesser than 4. Therefore else block will be executed.

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

public class Main
{
  public static void main(String[] args) {
    if(a<b)
      System.out.println("Hi");
    else
      System.out.println("Hello");
  }
}

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

View Answer Answer:- d) Compile-time error

In Java, all variables must be declared before using it. Here “a”, and “b” variables are not declared.

Q9) Find the output of the given Java program?

public class Main
{
  public static void main(String[] args) {
    boolean x, y, z;
    x = y = z = true;
    
    if(!x || (!y && !z))
      System.out.println("Hi");
    else
      System.out.println("Hello");
  }
} 

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

View Answer Answer:- b) Hello

The condition (!y && !z) => (!true && !true) => false && false => false. Then, (!x || false) => (!true || false) => false || false => false. Therefore overall condition becomes false, and else block will be executed.

Q10) 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.println("Hi");
    if(a < 20)
      System.out.println("Hello");
    else
      System.out.println("Know Program");
  }
}

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

View Answer Answer:- c) Know Program

In this program, 25 < 5 => condition becomes false so first if-statement will not be executed. Again 25 < 20 => condition becomes false so second if-statement also will not be executed. But the else block of second if-statement will be executed.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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