1. You have a Employee class with a public String field departmentName. In you program’s main method,
you create a Employee instance e1. What is output of the below code ?

String department = "MECH";
switch(department ){
  case "CSE":
   e1.departmentName = "CSE";
  case "EEE":
   e1.departmentName = "EEE";
  case "ECE":
  e1.departmentName = "ECE";
    break;
  default:
    e1.departmentName = "CSE";
}
System.out.println(e1.departmentName);
  1. CSE
  2. EEE
  3. ECE
  4. MECH

Answer: 3 – ECE. The case statement “CSE”, “EEE” must followed with break statements to exit from switch. If there is no break then it will execute all case statements.

2. Which of the following switch case statements are correct?

int measurement = 1
A) switch(measurement){
    case 1, case 2, case 3,
        System.out.println("Success");
        break;
}

B) switch(measurement){
    case 1 || 2 || case 3:
          System.out.println("Success");
        break;
}

C) switch(measurement){
    case 1, 2, 3:
         System.out.println("Success");
        break;
}

D) Switch(measurement){
    case 1: case 2: case 3:
         System.out.println("Success");
        break;
}
Answer: D