Friday, October 23, 2009

Exercice certification SCJP

Here is a small SCJP exercice to practice :


Given the following :

public class Test {

public int myTest = 2;
private String str;
public static void main(String [] args) {

for(int i=1;i<12;i++){
int myValue = 2;
System.out.println("great test !");
if (i== 5) {
myValue++;
System.out.println("myValue=" + myValue);
}
}
myValue++;
System.out.println("myValue=" + myValue);
}
}

What is the result ? (choose one)


A myValue=3 myValue=4

B myValue=3 myValue=3

C Compilation fails

D An exception is thrown at runtime









Solution

The solution is C because myValue exist only in for statement.