What will the code fragment below print Type your answer in
What will the code fragment below print? Type your answer in the box below.
int a = 1; int b = 2; int c = 3; switch(a) { case 0: System.out.println(a * a); break; case 1: System.out.println(a + a); break; case 2: System.out.println(b * c); break; case 3: System.out.println(c * c); break; }
Solution
Answer:
2
here is how? with sample program... explaination in comments
public class HelloWorld{
public static void main(String []args){
int a = 1;
int b = 2;
int c = 3;
switch (a) { // matches the case where a value...
case 0:
System.out.println(a * a);
break;
case 1: // match this condition.
System.out.println(a + a); //prints 1 + 1
break;
case 2:
System.out.println(b * c);
break;
case 3:
System.out.println(c * c);
break;
}
}
}
output:
2
