Search Tutorials


Java Switch MCQ - Multiple Choice Questions And Answers | JavaInUse

Java Inheritance MCQ - Multiple Choice Questions And Answers

Q. How can a switch statement be used in Java?

A. To iterate over a collection
B. To compare two objects
C. To conditionally execute code based on a value
D. To declare variables

Q. What happens if a break statement is not included in a case block of a switch statement?

A. The program terminates
B. The next case block is executed
C. An error is thrown
D. The default case block is executed

Q. Which of the following data types can be used as the argument in a switch statement in Java?

A. float
B. boolean
C. String
D. long

Q. Can a switch statement be used to compare strings in Java?

A. Yes
B. No

Q. In a switch statement, which section is optional?

A. case
B. default

Q. How can multiple cases share the same block of code in a switch statement?

A. By using a nested switch statement
B. By using the continue statement
C. By omitting break statements
D. By listing multiple case labels together separated by a comma

Q. Which keyword is used to declare a switch statement in Java?

A. switch
B. case
C. default
D. break

Q. What value does a switch statement evaluate?

A. Function
B. Variable
C. Expression
D. Method call

Q. Can you use a switch statement with floating-point values?

A. Yes
B. No

Q. Which of the following statements is true regarding the execution of a switch statement?

A. Only one case block is executed
B. Multiple case blocks can be executed
C. The default case block is always executed
D. The order of case blocks does not matter

Q. Which of the following is the correct way to define an Enum in Java?

A.
public enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
B.
enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
C.
class Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
D.
enum class Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

Q. How can you iterate over all values of an Enum in Java?

A.
for (Day day : Day.values()) {
    System.out.println(day);
}
B.
for (int i = 0; i < Day.values().length; i++) {
    System.out.println(Day.values()[i]);
}
C.
for (String day : Day) {
    System.out.println(day);
}
D.
for (int i = 0; i < Day.length; i++) {
    System.out.println(Day[i]);
}

Q. Enum constants can have constructors in Java. Is this statement true or false?

A. True
B. False

Q. How do you access a specific Enum constant value in Java?

A.
Day.MONDAY.value()
B.
Day.MONDAY
C.
MONDAY.value()
D.
MONDAY

Q. Can Enums in Java have abstract methods?

A. Yes
B. No

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

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

Q. How can you compare Enum constants in Java?

A. Using the == operator
B. Using the equals() method
C. Both A and B
D. Enums cannot be compared

Q. Which of the following statements is true about Java Enums?

A. Enums can implement interfaces
B. Enums cannot have fields
C. Enums cannot be used in switch statements
D. Enums cannot have constructors

Q. Can Enums in Java have instance methods?

A. Yes
B. No

Q. How do you access the ordinal value of an Enum constant in Java?

A.
Day.MONDAY.ordinal()
B.
MONDAY.ordinal()
C.
ordinal(Day.MONDAY)
D.
Day.ordinal(MONDAY)