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 diehardtechy@gmail.com

Monday, June 16, 2014

OCJP practice question #21


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.

No comments: