Hi ,
I just came across a question in JCHQ.
What will happen when you attempt to compile and run this code?
public class MySwitch{
public static void main(
String argv[]){
MySwitch ms= new MySwitch();
ms.amethod();
}
public void amethod(){
char k=10;
switch(k){
default:
System.out.println("This is the default output");
break;
case 10:
System.out.println("ten");
break;
case 20:
System.out.println("twenty");
break;
}
}
}
1) None of these options
2) Compile time errror target of switch must be an integral type
3) Compile and run with output "This is the default output"
4) Compile and run with output "ten"
The answer was 4.
I can understand it but will it not cause a problem when the variable k is being assigned with a value not enclosed in single quotes ?
I was thinking that the answer might be 3 because of that.
Can someone please explain ?
Thanks,
Srini