Java Switch Quiz Online Test

Time limit: 0

Quiz-summary

0 of 12 questions completed

Questions:

1

2

3

4

5

6

7

8

9

10

11

12

Information

Java Switch Quiz contains 12 single and multiple choice questions. Switch quiz questions are designed in such a way that it will help you understand how switch statement works in Java. At the end of the quiz, result will be displayed along with your score and switch quiz answers.

There is no time limit to complete the quiz. Click Start Quiz button to start the Java Switch quiz online.

You have already completed the quiz before. Hence you can not start it again.

Option 2 is the correct choice. There is no compilation error in the program. It is valid syntax to have default case first inside the switch statement. Since there is a matching case for variable i value, 1 will be printed. Since the break statement is mentioned at the end of the matching case, next cases will not be evaluated. So the program will output 1.

Incorrect answer.

Option 2 is the correct choice. There is no compilation error in the program. It is valid syntax to have default case first inside the switch statement. Since there is a matching case for variable i value, 1 will be printed. Since the break statement is mentioned at the end of the matching case, next cases will not be evaluated. So the program will output 1.

Option 2 is the correct choice. There is no compilation or run time error in the program. Program defines two constants ON and OFF with values of 0 and 1 respectively at the start of the class and these values are referred in the switch statement later. Since the value of state matches with the case ON, “On” will be printed. It is not mandatory for default case to appear last. It will not be evaluated though since there is a match.

Incorrect answer.

Option 2 is the correct choice. There is no compilation or run time error in the program. Program defines two constants ON and OFF with values of 0 and 1 respectively at the start of the class and these values are referred in the switch statement later. Since the value of state matches with the case ON, “On” will be printed. It is not mandatory for default case to appear last. It will not be evaluated though since there is a match.

Option 4 is the correct choice. Since there is no matching case for state variable, default case will be executed which will print “Unknown”. There is no break statement after default case, so control will fall through the next cases as well and hence “Off” will be printed. Program output will be “UnknownOff”.

Incorrect answer.

Option 4 is the correct choice. Since there is no matching case for state variable, default case will be executed which will print “Unknown”. There is no break statement after default case, so control will fall through the next cases as well and hence “Off” will be printed. Program output will be “UnknownOff”.

Option 1 is the correct choice. Only literals or constants can be used as case values. Here, char variables are not declared as final, so they cannot be used as case values. Program will give compilation error “case expressions must be constant expressions”.

Incorrect answer.

Option 1 is the correct choice. Only literals or constants can be used as case values. Here, char variables are not declared as final, so they cannot be used as case values. Program will give compilation error “case expressions must be constant expressions”.

Option 4 is the correct choice. There are no compilation errors in the program. Variables of type byte, short, char and integer can be used in switch statement. Plus, if variable is declared as final (constant), it can be used in case expression. Since there is a matching case for variable state, “Off” will be printed when you run the program.

Incorrect answer.

Option 4 is the correct choice. There are no compilation errors in the program. Variables of type byte, short, char and integer can be used in switch statement. Plus, if variable is declared as final (constant), it can be used in case expression. Since there is a matching case for variable state, “Off” will be printed when you run the program.

Option 4 is the correct choice. There are no compilation errors in the program. All byte, short and char variables are widened to integer values. So character ‘d’ becomes 100 as per its ASCII value. Since there is a matching case, 100 will be printed. Since there is no break statement after it “No match” will also be printed. Output of the program will be “100No match”.

Incorrect answer.

Option 4 is the correct choice. There are no compilation errors in the program. All byte, short and char variables are widened to integer values. So character ‘d’ becomes 100 as per its ASCII value. Since there is a matching case, 100 will be printed. Since there is no break statement after it “No match” will also be printed. Output of the program will be “100No match”.

Option 3 is the correct choice. Empty cases are valid in switch statement. You can club together all matching cases and write common code for all of them. Here, 9 is a matching case so “789” will be printed.

Incorrect answer.

Option 3 is the correct choice. Empty cases are valid in switch statement. You can club together all matching cases and write common code for all of them. Here, 9 is a matching case so “789” will be printed.

Option 4 is the correct choice. All byte, short and char values are promoted to integer values so ‘a’ is represented by ASCII 97 value, ‘b’ is 98 and so on. Since there is a matching case, “a” will be printed followed by “b” and “c” in the absence of break statements. Output of the program will be “abc”.

Incorrect answer.

Option 4 is the correct choice. All byte, short and char values are promoted to integer values so ‘a’ is represented by ASCII 97 value, ‘b’ is 98 and so on. Since there is a matching case, “a” will be printed followed by “b” and “c” in the absence of break statements. Output of the program will be “abc”.

Option 1 is the correct choice. Since char values are represented by corresponding ASCII values, ‘a’ is 97. Since case value 97 is given two times, program will give compilation error “Duplicate case”.

Incorrect answer.

Option 1 is the correct choice. Since char values are represented by corresponding ASCII values, ‘a’ is 97. Since case value 97 is given two times, program will give compilation error “Duplicate case”.

Option 2 is the correct choice. There are no compilation errors in the program. Number starting with 0 in Java is treated as octal number which is a valid integer. Number 8 in decimal is 010 in octal so “Let’s eat” will be printed.

Incorrect answer.

Option 2 is the correct choice. There are no compilation errors in the program. Number starting with 0 in Java is treated as octal number which is a valid integer. Number 8 in decimal is 010 in octal so “Let’s eat” will be printed.

Option 1 is the correct choice. Since group variable is defined inside the switch, it is local to that block only. It cannot be access outside the switch statement. So program will give compilation error “group cannot be resolved to a variable”.

Incorrect answer.

Option 1 is the correct choice. Since group variable is defined inside the switch, it is local to that block only. It cannot be access outside the switch statement. So program will give compilation error “group cannot be resolved to a variable”.