Thursday 21 February 2013

19. What will be the result of executing the following code?

19.  What will be the result of executing the following code?
Given that Test1 is a class.



Choices:
a. The code will not compile because the array t2 is not initialized in an unconditional statement before use.
b. The code will compile but a runtime exception will be thrown at line 12.
c. The code will compile but a runtime exception will be thrown at line 11.
d. None of these.


Answer:-
D is correct.
Though we cannot use local variables without initializing them (compilation error), there is an exception to it. In case of arrays initialization is supposed to be complete when we specify the leftmost dimension of the array. The problem occurs at runtime if we try to access an element of the array which has not been initialized (specification of size). In the question above the array t2 is initialized before use, therefore there will be no problem at runtime also and the lines 11 and 12 will both print null.

No comments:

Post a Comment