vendredi 23 octobre 2009

Exercice certification SCJP

Voici un petit exercice pour s'entrainer.

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

La solution est C car la variable myValue
C because the variabme myValue exist only in the for.