Monday, June 16, 2014

OCJP practice question #21

7:39 PM


OCJP PRACTICE QUESTION: 

What will be the output of given OCJP question below.







Explanation :
Option E is the correct answer.

Option E is correct as the code fails to compile. In the main method signature, we have declared the argument as “String[] a” and also we have tried to create for loop with int variable named as "a" but it is illegal because we have already declared variable with name "a" (as main method argument).

This will cause a compile time error since we can't declare two variables with same name inside a same scope.
Here both method argument and the variable declared inside the for loop belong to the same local scope.

Option A, B, C and D are incorrect as code fails to compile. However option B could be correct if we changed the variable name of argument variable or for loop variable.

REFERENCE:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

The correct answer is: Compilation fails.

0 comments :

Post a Comment

 

© 2014 DieHardTechy . All rights resevered. Designed by Diehardtechy