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


Read More...

Generate Given Output #1

Leave a Comment


Question:

 
public class Test {
     
      public static void main(String[] args) {
            System.out.println("Ram");
      }

}


Modify the above code to get below output:

Hi
Ram
Bye

You shouldn’t modify main method.

Solution:



public class Test {
           
      static
      {
            System.out.println("Hi");
            main(null);
            System.out.println("Bye");
            System.exit(0);
      }
     
      public static void main(String[] args) {
            System.out.println("Ram");
      }

}

Read More...