What will be the output of the following Java code?

class Output
  {
      public static void main(String args[])
      {
          Double y = new Double(257.57812);
      Double i = new Double(257.578123456789);  
          try
          {
          int x = i.compareTo(y);
              System.out.print(x);
          }
          catch(ClassCastException e)
          {
              System.out.print("Exception");
          }
  }
  }

a) 0
b) 1
c) Exception
d) None of the mentioned

1 thought on “What will be the output of the following Java code?”

  1. b
    Explanation: i.compareTo() methods two double values, if they are equal then 0 is returned and if not equal then 1 is returned, here 257.57812 and 257.578123456789 are not equal hence 1 is returned and stored in x.
    Output:
    $ javac Output.java
    $ java Output
    1

Leave a Comment