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.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Result
0 out of 12 questions were answered correctly.
Time has elapsed
Your score is 0 out of 0, (0)
Average score
Your score
Category Score
Switch0%
Review Answers
1
2
3
4
5
6
7
8
9
10
11
12
Answered
Review
Question 1 of 12
1. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 1;
switch(i){
default:
System.out.println("Default ");
case 1:
System.out.println("1 ");
break;
case 0:
System.out.println("0 ");
}
}
}
Correct 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.
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.
Question 2 of 12
2. Question
What will happen when you compile and run the following code?
public class Test{
private static final int ON = 1;
private static final int OFF = 0;
public static void main(String[] args){
int state = 1;
switch(state){
case ON:
System.out.println("On");
break;
default:
System.out.println("Unknown");
case OFF:
System.out.println("Off");
}
}
}
Correct 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.
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.
Question 3 of 12
3. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
final int ON = 1;
final int OFF = 0;
int state = 3;
switch(state){
case ON:
System.out.print("On");
break;
default:
System.out.print("Unknown");
case OFF:
System.out.print("Off");
}
}
}
Correct 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”.
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”.
Question 4 of 12
4. Question
What will happen when you compile and run the following code?
public class Test{
static char BLGR_A = 'a';
static char BLGR_B = 'b';
static char BLGR_O = 'o';
public static void main(String[] args) {
char bloodgroup = 'a';
switch(bloodgroup){
case BLGR_A:
System.out.print("A");
case BLGR_B:
System.out.print("B");
case BLGR_O:
System.out.print("O");
break;
}
}
}
Correct 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”.
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”.
Question 5 of 12
5. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
final char ON = '1';
final char OFF = '0';
char state = '0';
switch(state){
case ON:
System.out.println("On");
break;
case OFF:
System.out.println("Off");
break;
default:
assert false;
}
}
}
Correct 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.
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.
Question 6 of 12
6. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
char c = 'd';
switch(c){
case 'a' :
System.out.print("a");
case 'b' :
System.out.print("b");
case 'c' :
System.out.print("c");
case 100 :
System.out.print("100");
default :
System.out.print("No match");
}
}
}
Correct 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”.
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”.
Question 7 of 12
7. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int age = 9;
switch(age){
case 1 : case 2: case 3: case 4: case 5:
System.out.println("12345");
break;
case 6:
System.out.println("6");
break;
case 7 : case 8: case 9:
System.out.println("789");
}
}
}
Correct 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.
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.
Question 8 of 12
8. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int year = 2004;
switch(year){
case 2001: case 2002 : case 2003: case 2004: case 2005:
System.out.print("Normal Year");
break;
case 2004:
System.out.print("Leap Year");
break;
}
}
}
Correct answer.
Option 1 is the correct choice. Duplicate cases are not allowed inside switch statement. Since 2004 appears in two cases, program will give compilation error “Duplicate case”.
Incorrect answer.
Option 1 is the correct choice. Duplicate cases are not allowed inside switch statement. Since 2004 appears in two cases, program will give compilation error “Duplicate case”.
Question 9 of 12
9. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 97;
switch(i){
case 95 :
System.out.print("95");
case 96 :
System.out.print("96");
case 'a' :
System.out.print("a");
case 'b' :
System.out.print("b");
case 'c' :
System.out.print("c");
}
}
}
Correct 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”.
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”.
Question 10 of 12
10. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 97;
switch(i){
case 'a' :
System.out.print("a");
case 'b' :
System.out.print("b");
case 'c' :
System.out.print("c");
case 96 :
System.out.print("97");
case 97 :
System.out.print("96");
}
}
}
Correct 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”.
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”.
Question 11 of 12
11. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int time = 8;
switch(time){
case 7:
System.out.println("Let's drink");
case 9:
System.out.println("Good night");
case 010:
System.out.println("Let's eat");
}
}
}
Correct 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.
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.
Question 12 of 12
12. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int age = 8;
switch(age){
case 4: case 5:{
String group = "Play House";
}
case 6: case 7:case 8:
String group = "Nursery";
}
System.out.println(group);
}
}
Correct 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”.
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”.