Java OCA OCP Practice Question 1907

Question

What will be printed by the following code if it is run with command line: java Main -0.50 ?.

public class Main {
   public static double getSwitch(String str) {
      return Double.parseDouble(str.substring(1, str.length() - 1));
   }//from  w  w w. j a  va 2  s  .  c  om

   public static void main(String args[]) {
      switch (getSwitch(args[0])) {
      case 0.0:
         System.out.println("Hello");
      case 1.0:
         System.out.println("World");
         break;
      default:
         System.out.println("Good Bye");
      }
   }
}

Select 1 option

  • A. Hello
  • B. World
  • C. Hello World
  • D. Hello World Good Bye
  • E. None of the above.


Correct Option is  : E

Note

Observe that the method getSwitch() has been declared to return a double.

Its return value is being used in the switch() statement.

The program will not even compile because double/float/long/boolean cannot be used in switch(...) statement.




PreviousNext

Related