Tricky Java Interview Question
1) Can you instantiate this class?
2) Is the below code written correctly? If yes, what will be the output?
5) What will be the output of the below program?
6) Can we override method(int) as method(Integer) like in the below example?
7) Which statements in the below class shows compile time error (Line 5 or Line 7 or both)?
8) What will be the output of the following program?
9) What will be the output of the below program?
10) What will be the output of the following program?
Ans) 75
public class A { A a = new A(); } Not possible. Because while instantiating, constructor will be called recursively. |
2) Is the below code written correctly? If yes, what will be the output?
class A { static void staticMethod() { System.out.println("Static Method"); } } public class MainClass { public static void main(String[] args) { A a = null; a.staticMethod(); } } Yes, the code is correct. You can call static methods through reference variable which is pointing to null. Output will be, Static Method 3) What will be the output of the following program? |
class A { static int i = 1111; static { i = i-- - --i; } { i = i++ + ++i; } } class B extends A { static { i = --i - i--; } { i = ++i + i++; } } public class MainClass { public static void main(String[] args) { B b = new B(); System.out.println(b.i); } } ANS) 6 4) What happens when you call methodOfA() in the below class? |
class A { int methodOfA() { return (true ? null : 0); } } ANS) Throws NullPointerException at run time. |
5) What will be the output of the below program?
public class MainClass { public static void main(String[] args) { Integer i1 = 127; Integer i2 = 127; System.out.println(i1 == i2); Integer i3 = 128; Integer i4 = 128; System.out.println(i3 == i4); } } Ans:
|
6) Can we override method(int) as method(Integer) like in the below example?
class A { void method(int i) { //method(int) } } class B extends A { @Override void method(Integer i) { //method(Integer) } } Ans) No. It gives compile time error. Compiler treats int and Integer as two different types while overriding. Auto-boxing doesn’t happen here. |
7) Which statements in the below class shows compile time error (Line 5 or Line 7 or both)?
public class MainClass { public static void main(String[] args) { Integer i = new Integer(null); String s = new String(null); } } Ans) Only Line 7 shows compile time error. Because, compiler will be in an ambiguous situation of which constructor to call. Because, String class has five constructors which take one argument of derived type . Where as Integer class has only one constructor which takes one argument of derived type. |
8) What will be the output of the following program?
1 2 3 4 5 6 7 8 | public class MainClass { public static void main(String[] args) { String s = "ONE"+1+2+"TWO"+"THREE"+3+4+"FOUR"+"FIVE"+5; System.out.println(s); } } Ans) ONE12TWOTHREE34FOURFIVE5 |
9) What will be the output of the below program?
public class MainClass { static int methodOne(int i) { return methodTwo(i *= 11); } static int methodTwo(int i) { return methodThree(i /= 11); } static int methodThree(int i) { return methodFour(i -= 11); } static int methodFour(int i) { return i += 11; } public static void main(String[] args) { System.out.println(methodOne(11)); } } Ans) 11 |
10) What will be the output of the following program?
public class MainClass { public static void main(String[] args) { int i = 10 + + 11 - - 12 + + 13 - - 14 + + 15; System.out.println(i); } } |
No comments:
Post a Comment