Sunday, 5 August 2012

Question 161 : Statements

Given:
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.

2 comments:

  1. Replies
    1. The 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.

      For more information regarding variable scope click here

      Delete