Multiple choice

What is the output of the given java program?

public class HelloWorld
{
public static void main(String []args)
{
int i= (int)Math.round(Math.hypot(114,123));
switch(i) // Line 1 { default : System.out.println("Default Case");
 case 128<<6<<3 : System.out.println("Case 1");
 case 9072/54 : System.out.
println("Case 2"); case 128<<4<<2 :
 System.out.println("Case 3");
 }
 }
 }

  1. Default CaseCase 1Case 2Case 3

  2. Case 1Case 2Case 3

  3. Case 2Case 3

  4. Case 3

  5. Compilation Error

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

This is the correct choice. The hypot() method in java takes two arguments as hypot(x,y) and it evaluates the expression :- sqrt(x^2 + y^2).So this will find sqrt(5,8) which comes out to be 167.7, so now the round() function will convert it to the nearest integer value, which is 168.We have converted the “long” value 168 into integer and passed it to switch case.Now swtich() case becomes switch(168), now the case label having value 168 will be executed.So the case 2 gets executed and printed “Case 2”, but there is no break keyword after case 2, so case 3 also gets executed. So, the correct output becomes:-Case 2Case 3.