Wednesday, January 20, 2010

SCJP exercise : operators

An other exercise ! The goal is practice and learn commons java certification SCJP questions.

Exercise :



Given the following :

public class Test {

public static void main(String [] args) {


int num = 1;

if ( num = 2) {

System.out.println("Result:" + num);

}

System.out.println("Result:" + num);

}

}



What is the result ? (choose one)


A Result:1

B Result:2

C Compilation fails

D An exception is thrown at runtime






Solution :


The solution is C.In fact we set num to 2 with num=2.
So the result of the setting is an int.Or the if is waiting a boolean so the compilation fails.