Wednesday, March 3, 2010

SCJP exercise : initialisation

Exercice :

 

Given the following :

public class Test {

    private int myvar=3;
    public Test(int myvar) {

        if (myvar == 5) {
            myvar = 2;
        } else {
            myvar =3;
        }
    }

    public void show() {
        System.out.println("myvar:" + myvar);
    }

    public static void main(String[] args) {
        Test test = new Test(4);
        test.show();
    }
}


What is the result (choose one) :
A: myvar:2
B: myvar:3
C: myvar:4
D: myvar:5
E: Compilation fails
F: An exception is thrown at runtime










Solution :



The solution is B myvar:3.

Not easy ....

No comments:

Post a Comment