Java if/else MCQ - Multiple Choice Questions And Answers
Q. What is the syntax for the if statement in Java?
A. if(condition) { // code block }B. if { // code block } condition
C. (condition) { // code block } if
D. if condition // code block
Q. How many else if statements can be used after an if statement in Java?
A. Only oneB. Unlimited
C. Up to three
D. Exactly two
Q. Which of the following is the correct if-else syntax in Java?
A. if { // code block } else { // code block }B. if(condition) { // code block } else { // code block }
C. if else { // code block } (condition) { // code block }
D. if(condition) { // code block } { else // code block }
Q. What happens if the condition in an if statement is false?
A. Code block inside if statement is executedB. Code block inside else statement is executed
C. Code block inside if statement is skipped
D. Code block inside else if statement is executed
Q. What is the purpose of the else if statement in Java?
A. To provide an alternative condition checkB. To provide a default block of code to execute
C. To repeat a certain code block
D. To exit the program
Q. What is the syntax for the else if statement in Java?
A. elseif condition { // code block }B. else-if { // code block } condition
C. else if(condition) { // code block }
D. else if { // code block } condition
Q. When would you use nested if-else statements in Java?
A. To handle multiple conditionsB. To check only one condition
C. To repeat a certain code block
D. To exit the program
Q. What happens if there is no else block after an if statement in Java?
A. Syntax errorB. Compiler warning
C. Code block inside if statement is skipped if condition is false
D. Code block inside if statement is executed if condition is false
Q. What is the purpose of the switch statement in Java?
A. To perform arithmetic calculationsB. To select one of many code blocks to execute
C. To repeat a certain code block
D. To exit the program
Q. Which of the following is a valid syntax for a switch statement in Java?
A. switch(expression) case 1: // code block break; case 2: // code block break; default: // code blockB. switch { case 1: // code block break; case 2: // code block break; default: // code block break; }
C. switch(expression) { case 1: // code block break; case 2: // code block break; default: // code block }
D. switch(expression): case 1: // code block case 2: // code block default: // code block