Skip to main content

Posts

Intersection of exception in class or interface hierarchy

Question? Q1. Will below program compile? public class ExceptionTestForInterface implements Type3 {   public void f()   {     System. out .println( "Hello World" );   }      public static void main(String[] args )   {     Type3 type = new ExceptionTestForInterface();     type .f();   } } interface Type1 {   void f() throws CloneNotSupportedException; } interface Type2 {      void f() throws InterruptedException;    } interface Type3 extends Type1, Type2{    } Ans => Yes, It will compile and successfully print the "Hello World" Remember The set of checked exceptions that a method can throw is the intersection of the sets of checked exceptions that it is declared to throw in all applicable types.
Recent posts

NaN Not a Number puzzle in java

Question ? What is the output of below code? public class BridgeOfLooper {   public static void main(String[] args )   {     double d = 0.0/ 0.0;     System. out .println( d - 1 == 0);   } } o/p => false Some might have thought about 'ArithmeticException :Divide by Zero' Remember that 0.0 is NaN not a Zero. Explanation :    double d = 0.0 / 0.0 Arithmetic expression evaluates to infinity    so d becomes NaN      d = NaN      TechFacts Any floating point operation evaluates to NaN if one or more    of its operands are NaN.   The rules for computing with NaN is that once it generates NaN, a computation is damaged, and no further computation can repair the damage.    d -1 = NaN which is not equal to 0

Static Block vs Instance Initialiser block

 What will be the output for below code? class A {      static {     System. out .println( "Inside static block A" );   }   {     System. out .println( "inside init A" );   } } class B  extends A{         {     System. out .println( "inside init B" );   } static {     System. out .println( "inside static block B" );   } } public class Test{   public static void main(String[] args )    {       A b = new B();   } } Answer : Inside static block A inside static block B in init A in init B In Java, first static data executed  then instance initialiser  In Inheritance parrent is initialise first, then child. Remember restriction impose on super keyword inside child class constructor.

Method overloading parameter as float, double and wrapper.

Question?  What is the output of below code ?   Other question  will it be compile ? will there be an error at runtime for ambiguity?     public   class  RedHatQuestion {    public   void  printData( float   f )   {     System. out .println( "inside float" );   }       public   void  printData( double   d )   {     System. out .println( "inside double " );   }       public   void  printData(Double  d )   {     System. out .println( "Inside object double" );   }       public   static   void  main(String[]  args ) {     RedHatQuestion  question  =  new  RedHatQuestion();      question .printData(3.1415);   } } It will compile as there is no syntax error. Also there is no runtime exception as there no runtime binding created.  See we have only compile time binding/overloading not overriding. Answer : inside double. Explanation : in java for floating value we have default data type as

return statement cascade inside the java method

Question? In given code find out the output  public int divide(int a, int b ) {       try       {           return a/b;       }      catch(Exception exception)      {           return -10;      }      finally     {         return -100;     } } => invoke above method using  statement divide(10,10) what will be the output. => output       -100 => explanation     When  execution control comes inside the divide method, then JVM create below stack frames.      one for try block      one for catch block      one for finally block. In java finally block get executed for every execution of try except System exit or System error. So you can imagine above code in stack/block at runtime like below. Divide method metadata Try block metadata  Catch block metadata Finally block metadata  I/P => divide(10,10)  execution of above statement as follows. try block   return 10/10;  will