Saturday, September 19, 2009

JAVA Certification SCJP : keyword

Question about keywords are often in SCJP exam.Be careful at C++ keyword that are different of JAVA keyword.The exam will try to make confuse about this.If you search, books and exercice,you can see that link : Certification Sun SCJP

These exercises will show common keyword question.

Exercise 1 :

Given the following :

public class Test {

public static void main(String [] args) {

System.out.println("Zest!");

}


public void assert {

System.out.println("Helllo");

}

}


What is the result ? (choose one)


A Zest!

B Helllo

C Compilation fails

D An exception is thrown at runtime



Exercice 2 :

Given the following :


public class Test {

public static void main(String [] args) {

System.out.println("Zest!");

int true = 1;

System.out.println("true : " + true);

}

}

What is the result ? (choose one)


A Zest!

B Zest true: 1

C Compilation fails

D An exception is thrown at runtime


Exercice 3 :


public class Test {
public static void main(String [] args) {

System.out.println("Zest!");

transient int value = 10;

System.out.println("true : " + true);

}

}

What is the result ? (choose one)


A Zest!

B Zest true: 1

C Compilation fails

D An exception is thrown at runtime


Solution :

Exercise 1 :

Compilation will fail because assert is a reserved keywords. Be careful at this sort of question.It's often in test and very easy to miss it

Exercise 2 :

Compilation will fail because you can't name variable with a keyword name.

Exercise 3 :

Compilation will fail.Transient keyword is an attribut of a class.It's used in serialisation.
Example :
class Writeable implements java.io.Serializable {
public transient int var1 = 4;
public int var2 = 19;
}