If Else in Java MCQs Exercise I

Ques 1 If Else


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

A To check if a condition is true and execute a block of code if it is
B To assign a value to a variable
C To iterate over a collection of elements
D To declare a method

Ques 2 If Else


Which of the following is the correct syntax for the if-else statement in Java?

A if (condition) { // code block }
B if (condition) { // code block } else { // code block }
C if (condition) // code block else // code block
D if (condition) { // code block } elseif (condition) { // code block }

Ques 3 If Else


How can you represent multiple conditions using if-else statements?

A Using nested if-else statements
B Using the && operator
C Using the || operator
D All of the above

Ques 4 If Else


What is the output of the following code snippet?

int x = 5;
if (x > 10) {
   System.out.println("x is greater than 10");
} else if (x > 7) {
   System.out.println("x is greater than 7");
} else {
   System.out.println("x is less than or equal to 7");
}

A x is greater than 10
B x is greater than 7
C x is less than or equal to 7
D The code will not compile due to an error

Ques 5 If Else


Which of the following is the correct syntax for the ternary operator in Java?

A condition ? value1 : value2
B condition : value1 ? value2
C condition ? value1 : : value2
D condition : value1 : value2