Given:
A. 6
B. 7
C. 10
D. 11
E. Compilation fails.
F. An exception is thrown at runtime.
11. public static void main(String[] args) {
12. for (int i = 0; i <= 10; i++) {
13. if (i > 6) break;
14. }
15. System.out.println(i);
16. }
What is the result?A. 6
B. 7
C. 10
D. 11
E. Compilation fails.
F. An exception is thrown at runtime.
why?
ReplyDeleteThe Variable "i" we are printing is out of scope.For this code to work we should put the system.out.println statement inside the for loop because the variable i(local variable) is declared inside for loop and it can be accessed only inside for loop.
DeleteFor more information regarding variable scope click here