Monday, September 14, 2009

JAVA Certification SCJP : fundamentals main

You must know the construction of the main method.
The exam will try to make confuse about this.If you search, books and exercice,
you can see that link : Certification Sun SCJP

Note : the solutions of the exercise are at the end.


Exercice 1 :

Given the following :

public class Test {
public static void main(String args) {
System.out.println("Helllo");
}
}

What is the result ? (choose one)

A Helllo
B Hell
C Compilation fails
D An exception is thrown at runtime



Exercice 2 :

Given the following :

public class Test {
public static void main(String [] arg) {
System.out.println("Helllo");
}
}

What is the result ? (choose one)

A Helllo
B Hel
C Compilation fails
D An exception is thrown at runtime

Exercise 3 :

Given the following :

public class Test {
public void main(String [] argS) {
System.out.println("Helllo");
}
}

What is the result ? (choose one)

A Helllo
B Hel
C Compilation fails
D An exception is thrown at runtime


Exercise 4 :

Given the following :

public class Test {
public static void main(String [] test) {
System.out.println(test[0]);
}
}

and the command line evocation is
java Test X 1 t
What is the result ? (choose one)

A Test
B X
C 1
D t
C Compilation fails
D An exception is thrown at runtime

Solution :

Exercise 1 :

There is no [] that 's why, there is an error at execution.The compilator doesn't see
the main method.It see a method so when you launch your application, it thows an exception :

Exception in thread "main" java.lang.NoClassDefFoundError: Test (wrong name: packet/Test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: Test. Program will exit.

The answer is D.

Exercise 2 :

You will see Helllo on the scree.
Note : you can name the main method argument differently if you wish.


Exercise 3 :

The correct answer is D because the compilator search main method but there is not correct
main method like public static void main(String [] args) {

Exercise 4 :

The good solution is B (X).