Sunday, July 21, 2024

If MCQ

 What does the if statement in Java do?

a) Executes a block of code if a condition is true

b) Terminates the program immediately

c) Skips the next line of code

d) Executes a block of code repeatedly


Which symbol is used for equality comparison in an if condition in Java?

a) =

b) ==

c) !=

d) >


What is the syntax to write an if statement in Java?

a) if { }

b) if () { }

c) if <> { }

d) if [] { }


What is the purpose of the else statement in Java?

a) It is used to create nested conditions

b) It terminates the program if the if condition is false

c) It is executed if the if condition is true

d) It skips the if condition


Which of the following is true about an if-else statement in Java?

a) It can only have an if part without an else

b) It must have an else part with every if

c) It can have only an else part without an if

d) It can have an if part followed by an optional else part


What is the output of the following code snippet?


java

Copy code

int x = 10;

if (x > 5) {

    System.out.println("x is greater than 5");

} else {

    System.out.println("x is less than or equal to 5");

}

a) x is greater than 5

b) x is less than or equal to 5

c) No output

d) Compilation error


How many conditions can be checked in a single if statement in Java?

a) Only one condition

b) Two conditions

c) Multiple conditions separated by commas

d) Unlimited conditions


What is the purpose of the else if statement in Java?

a) It is used to handle multiple conditions in sequence

b) It is used instead of the else statement

c) It is a replacement for the if statement

d) It works as a nested if statement


Which logical operator is used to combine multiple conditions in an if statement in Java?

a) &&

b) ||

c) !

d) &


In Java, can an if statement exist without an else statement?

a) Yes, if can exist without else

b) No, if always requires an else

c) if can exist only with multiple else statements

d) It depends on the condition used in the if


Answers:


a) Executes a block of code if a condition is true

b) ==

b) if () { }

c) It is executed if the if condition is true

d) It can have an if part followed by an optional else part

a) x is greater than 5

a) Only one condition

a) It is used to handle multiple conditions in sequence

a) &&

a) Yes, if can exist without else

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...