static keyword interview questions in java

Interview questions of static keyword in java and answers

(1)

class StaticDemo {
    static int a=0;
    int b=++a;
    static int c=++a;

    public static void main(String[] args){
         System.out.print(c);
    }
}

What will be output of above program?
         
(a)0
(b)1
(c)2
(d)Compiler error






Answer: (b)

(2)

class StaticDemo {
    int a=0;
    int b=++a;
    static int c=++a;

    public static void main(String[] args) throws Exception {
         System.out.print(c^b);
    }
}

What will be output of above program?

(a)0
(b)1
(c)2
(d)Compiler error






Answer: (d)

(3)

package manish;
class StaticDemo {
    static {
         StaticDemo {
           System.out.println("raja");
         }
         catch(Exception e){

         }
    }
}

What will be output of above program?

(a)raja
(b)No main method in class exception.
(c)raja
   No main method in class exception.
(d)Compiler error






Answer: (c)

(4)

class StaticDemo {
    static {
         System.out.println("raja");
    }

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

What will be output of above program?

(a)rani
   raja
(b)raja
   rani
(c)rani
(d)Compiler error






Answer: (b)

(5)

class StaticDemo {
    static int a=0;
    static {
    System.out.println(a++);
    }

    public static void main(String[] args){
    ++a;
        System.out.println(++a);
    }
   
    static {++a;}
}

What will be output of above program?

(a)1
   3
(b)0
   4
(c)2
   2
(d)Compiler error






Answer: (b)

1 comment:

Anonymous said...

QNo:3 need to be corrected to get the given ans.

class StaticDemo {
static {
try
{
System.out.println("raja");
}
catch(Exception e){

}
}
}