Tuesday, November 26, 2013

Question: Variable Decleration and Scope

Given:

public static void main(String[] args) {
   for (int i = 0; i <= 10; i++) {
      if (i > 6) 

           break;
   }
    System.out.println(i);
 }


What is the result?

A. 6
B. 7
C. 10
D. 11
E. Compilation fails.
F. An exception is thrown at runtime

Execution:
The variable i have scope within for loop only and it is printed out of scope will give Compile Time Exception. So it says can't find symbol 'i'

Answer:
E. Compilation fails.


No comments:

Post a Comment