Monday, October 6, 2014

What is the output for the below piece of Code? #4

Leave a Comment

Question:


public class Test {
     
      public static void main(String[] args) {
            float a = 10;
            System.out.println(a/0);
      }

}


Output: 

 Infinity


Explanation:


For double and float, Arithmetic Exception is NOT thrown!
If "a" was an Integer (either byte, short, int or long), it would have thrown :


Exception in thread "main" java.lang.ArithmeticException: / by zero

      at Test.main(Test.java:9)
 

Similar Question:

public class Test {
     
      public static void main(String[] args) {
            float a = 10;
            float x = a/0;
            System.out.println(a/0+" = "+x);
      }

}
 
Output:

Infinity = Infinity


0 comments:

Post a Comment