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

View Answer Answer:- d) All

Q2) Which of these are selection statements in Java?

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

View Answer Answer:- a) if()

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

View Answer Answer:- 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

View Answer Answer:- b) switch

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

a) True
b) False

View Answer Answer:- a) True

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

View Answer Answer:- d) Hi 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

View Answer Answer:- b) Know Program

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.

Also see:- Difference between float and double in Java

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

View Answer Answer:- 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

View Answer Answer:- b) Know Program

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

View Answer Answer:- a) Hi

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do 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 *