Search Tutorials


Java if/else MCQ - Multiple Choice Questions And Answers | JavaInUse

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 one
B. 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 executed
B. 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 check
B. 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 conditions
B. 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 error
B. 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 calculations
B. 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 block
B. 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





Q. What is the syntax for an if statement in Java?

A. if(expression) { // code block }
B. if { // code block } (expression)
C. (expression) { // code block } if
D. if { // code block } expression

Q. What keyword is used to define the else statement in Java?

A. otherwise
B. else
C. elseif
D. besides

Q. What is the purpose of using if else statements in Java?

A. To repeat a block of code multiple times
B. To control the flow of program execution based on a condition
C. To define a new variable
D. To terminate the program

Q. How many conditions can be checked using a series of if else if statements in Java?

A. Only one
B. Multiple conditions
C. Two conditions
D. Depends on the number of else statements

Q. What happens when no condition is true in an if else if statement in Java?

A. The program throws an error
B. The else block is executed
C. The if block is executed
D. The program skips the entire block

Q. Which operator is used to combine multiple conditions in an if statement?

A. && (logical AND)
B. || (logical OR)
C. ! (logical NOT)
D. & (bitwise AND)

Q. In an if else statement, can the if block exist without the else block?

A. Yes
B. No
C. Depends on the condition
D. Only when the if condition is true

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

A. To end the if block
B. To begin a new if block
C. To check another condition if the preceding if or else if condition is false
D. To execute only if the preceding if condition is true

Q. When would you use nested if else statements in Java?

A. To simplify the code
B. To have multiple conditions within a single if block
C. To only execute the if block
D. To check conditions within conditions

Q. Can you have multiple else statements following an if statement in Java?

A. Yes
B. No
C. Only if conditions are met
D. It depends on the number of if statements

Q. Which of the following correctly defines a Java Enum?

A.
public enum Colors {
    RED, GREEN, BLUE
}
B.
enum Colors {
    RED, GREEN, BLUE
}
C.
class Colors {
    RED, GREEN, BLUE
}
D.
public class Colors {
    enum {
        RED, GREEN, BLUE
    }
}

Q. How do you access an Enum constant in Java?

A.
Colors selectedColor = Colors.RED;
B.
Colors.RED = selectedColor;
C.
Colors.getColor(RED);
D.
Colors.RED.getColor();

Q. Can Enum constants have additional properties in Java?

A. Yes, by defining methods within the Enum class.
B. No, Enum constants cannot have additional properties.
C. Yes, by extending the Enum class.
D. No, Enum constants can only have the default properties.

Q. How do you iterate over all Enum constants in Java?

A.
for (Colors c : Colors.values()) {
    System.out.println(c);
}
B.
for (Colors : values()) {
    System.out.println();
}
C.
for (Colors.values()) {
    System.out.println();
}
D.
for (Color : Colors) {
    System.out.println(Color);
}

Q. Can Enums have abstract methods in Java?

A. Yes, Enums can have abstract methods.
B. No, Enums cannot have abstract methods.
C. Yes, but only if the Enum implements an interface.
D. No, abstract methods are not allowed in Enums.

Q. Which keyword is used to define an Enum in Java?

A. enum
B. class
C. type
D. constant

Q. In Java Enums, which method returns the name of the Enum constant?

A. toString()
B. name()
C. getValue()
D. constantName()

Q. What is the default constructor type for Enum in Java?

A. public
B. private
C. protected
D. default

Q. Enum constants in Java are treated as

A. Objects
B. Variables
C. Primitives
D. Literals

Q. Can Enums have fields and methods in Java?

A. Yes, Enums can have fields and methods.
B. No, Enums cannot have fields and methods.
C. Yes, but only fields are allowed.
D. No, only methods are allowed in Enums.