Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at

Monday, June 16, 2014

OCJP practice question #21


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.


The correct answer is: Compilation fails.

No comments: