Which of the following is true about the switch statement in Java?
a) It can replace any if statement.
b) It can only compare integer values.
c) It can compare strings and integers.
d) It cannot contain a default case.
What is the purpose of the break statement in a switch case in Java?
a) It skips the current iteration.
b) It exits the switch statement.
c) It moves to the next case.
d) It restarts the switch statement.
Which data types can be used as switch parameters in Java?
a) Only int
b) Only String
c) int, byte, short, char, and enums
d) All primitive data types
What happens if the break statement is omitted in a switch case in Java?
a) It results in a compilation error.
b) It throws a runtime exception.
c) It moves to the next case even if the condition isn't met.
d) It continues executing the code for subsequent cases.
Can a switch statement have a default case in Java?
a) Yes, but it's optional.
b) No, it's mandatory.
c) Yes, but it can only be placed as the first case.
d) No, it's prohibited.
What does the default case do in a switch statement?
a) It executes if no other case matches.
b) It is mandatory for every switch statement.
c) It represents an error condition.
d) It always executes first.
Which statement is used to define a switch case in Java?
a) case
b) when
c) select
d) match
What is the output of the following switch statement?
java
Copy code
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Other day");
}
a) Monday
b) Tuesday
c) Wednesday
d) Other day
In a switch statement, can multiple cases execute the same block of code in Java?
a) No, it's not allowed.
b) Yes, by specifying multiple cases separated by commas.
c) Yes, by using the continue keyword.
d) Yes, by omitting the break statement.
Which of the following statements is true regarding the evaluation of a switch expression in Java?
a) It allows complex expressions like boolean expressions.
b) It can only evaluate constant or constant-like expressions.
c) It can evaluate any arithmetic expression.
d) It can evaluate only String expressions.
Answers:
c) It can compare strings and integers.
b) It exits the switch statement.
c) int, byte, short, char, and enums
d) It continues executing the code for subsequent cases.
a) Yes, but it's optional.
a) It executes if no other case matches.
a) case
c) Wednesday
d) Yes, by omitting the break statement.
b) It can only evaluate constant or constant-like expressions.
No comments:
Post a Comment