Wednesday, February 10, 2016

java interview questions with code

Can you instantiate this class? A: Not possible. Because while instantiating, constructor will be called recursively.

Is the below code written correctly? If yes, what will be the output? A: Yes, it is correct. the output is "Static Method". A a = null already declared the Class of a is A and the static method is associated with Class not object(instance), so although no instance is created, the static method still can be executed.

What will be the output of the following program? A: the output is 6.
Firstly, we need understand static block is executed before the non-static block. You can put the non-static block before static block, still the static block will be executed first.
So when constructing class B, the block sequence is
Static block of Class A
Static block of Class B
non-static block of Class A
non-static block of Class B
Secondly, we need understand the operators sequence.

i-- - --i
the left side of - is i--, it is the i value before --, so it is 1111
the right side of - is --i, now i is the value after i--, which is 1110, then run -- on 1110 is 1109
i-- - --i equals 1111 - 1109 = 2
It can be translate to i-(--(i--)).
Then i = --i - i--
i is 2 now.
Left side of - is --i, which is 1. Right side of - is i--, which is the value before --, it is 1, then the result is 1-1=0
Then i = i++ + ++i
left side of + is i++, which is the value before ++, it is 0. Right side is ++i, i is 1 and after ++ it is 2, the result is 0+2=2
Then ++i + i++
The left side is value after ++, which is 3. The right side is the value before ++, still 3. Then 3+3 = 6
The eventual result is 6.

What happens when you call methodOfA() in the below class? A: NullPointerException at run time. The method returns null, as the method return type is int. Java try to convert null to int, then throws out the NullPointerException.

Can we override method(int) as method(Integer) like in the below example? A: No. It gives compile time error. Compiler treats int and Integer as two different types while overriding. Auto-boxing doesn’t happen here.
Is this allowed.
Yes, this is allowed.

Which statements in the below class shows compile time error (Integer or String or both)? A: The statement for String shows compile error. null means it could be any object. For String, there are more than one constructor methods can accept object. Then the compiler give the ambiguous error. It does not know which constructor method to call.
For Integer it only has two constructor methods, one accepts int, and the other one accepts String. The compiler knows the second constructor could be called.
If we change the code like this, then there is no compiling error But you will get runtime error on both of them, NullPointerException.

What will be the output of the below program? A: 11. The passed in the value is the result after calculating. methodOne(11), then call methodTwo(i *= 11), it equals methodTwo(121)

No comments:

Post a Comment