Study and learn Interview MCQ Questions and Answers on Java Switch Case Statements. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes Switch Case before reading these objective questions.
We can implement a SWITCH statement using IF, ELSE IF and ELSE control statements.
SWITCH does not support long constants.
switch(input) { case constant1: //statements; break; case constant2: //statements; break; default: //statements; };
switch(input) { case constant1: //statements; break; case constant2: //statements; break; default: //statements; }
switch(input) { case constant1: //statements; break; case constant2: //statements; break; default case: //statements; };
switch(input) { case constant1: //statements; break; case constant2: //statements; break; default case: //statements; }
The semicolon (;) after SWITCH {} is not required. So option A is wrong though it compiles fine.
int a=10; switch(a) { case 10: System.out.println("TEN"); }
"break;" is optional.
int b=20; switch(b) { default: System.out.println("LION"); }
DEFAULT statement alone can be written without any error.
String animal = "GOAT"; switch(animal) { break: System.out.println("DOMESTIC"); }
Case statements should start with either "case" or "default" only.
String college = "OXFORD"; switch("STANFORD") { case college: System.out.println("EXAM TIME"); break; default: System.out.println("UNKNOWN"); }
case expressions must be constant expressions. So, make the variable final. final String college = "OXFORD";
int num=20; switch(num) { case 10: System.out.println("TEN"); break; case 20: System.out.println("TWENTY"); break; case 30: System.out.println("THIRTY"); }
int num=40; switch(num) { case 5: System.out.println("FIVE"); break; case 35+5: System.out.println("FORTY"); break; case 20+30: System.out.println("FIFTY"); }
It is allowed to write expressions that result in constant values.
int persons = 45; int random = 45; switch(random) { case persons: System.out.print("CRICKET "); default: System.out.println("RUGBY"); }
Error: case expressions must be constant expressions So, make the variable final. final int persons = 45; //Then, output will be CRICKET
switch(15) { case 5*2: System.out.println("TEN");break; case 5*4-5:System.out.println("FIFTEEN");break; case 60/4+5: System.out.println("TWENTY"); }
Any expression that results in a Constant can be used as a "case constant".
switch(45) { case 10: ; }
You can specify dummy statements in java using a Semicolon (;).
int points=6; switch(points) { case 6: ; case 7: System.out.println("PASS");break; case 8: ; case 9: System.out.println("Excellent");break; case 10: System.out.println("Outstanding"); break; default: System.out.println("FAIL"); }
This is the way we define ranges in a SWITCH construct.
More or Less conditions can not be checked with a SWITCH statement in Java.
SWITCH case constants must be unique.
The compiler (JIT-Just In Time) creates a JUMP-TABLE for switch case branchings. So, it does not take time during Runtime. So, the SWITCH statement is fast.
int hours = 10; switch(hours) { case 10: System.out.println("TEN");break; case 10: System.out.println("TEN AGAIN"); break; default: System.out.println("TEN AS USUAL"); }
Case constant 10 is duplicate. So, it causes compiler error.
char grade = 'B'; switch(grade) { case 'A': System.out.print("GRADE-A ");break; case 'B': System.out.print("GRADE-B "); case 'C': System.out.print("GRADE-C "); }
"char" data type is allowed inside a Java SWITCH statement. The missing break statement causes CASE-C to be executed.
static enum ANIMAL {GOAT, TIGER, CAMEL} public static void main(String args[]) { ANIMAL ani = ANIMAL.CAMEL; switch(ani) { case GOAT: System.out.println("GOAT");break; case CAMEL: System.out.println("CAMEL");break; case TIGER: System.out.println("TIGER");break; } }
A SWITCH in java works well with enum constants. CASE Constants are defined without enum type.
String phone = "APPLE"; switch(phone) { case "Apple": System.out.println("Apple");break; case "APPLE": System.out.println("APPLE");break; case "SAMSUNG": System.out.println("SAMSUNG"); }
Apple and APPLE are different strings.