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; int num = a + b; switch(num) { 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 is :
9
Here is how explained in code... explaination is comments...
public
 class HelloWorld {
public
 static void main(String[] args)
 {
 int a = 1;
 int b = 2;
 int c = 3;
 int num = a + b; // 1 + 2 = 3 .. num = 3
 switch (num) { // matches case num = 3
 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: // match this case.
 System.out.println(c * c); // prints 3 * 3
 break;
 }
 }
 }
Output:
9

