Monday, February 22, 2010

Android

In this time, I prepare a blog about android.In this blog we will learn together android.I don't know android.So I will learn with you and I will put all my notes on my android blog : androidKillerApps

Wednesday, February 17, 2010

SCJP exercise : instanceof

Exercise :

public class Test {

public static void main(String [] args) {

Integer test = new Integer(5);
if (null instanceof Integer) {
System.out.println("null is an integer");
} else if ( test instanceof Integer) {
System.out.println("test is an integer");
} else {
System.out.println("Hello");
}
}

}


What is the result ? (choose one)

A
null is an integer
B test is an integer
C
Hello
D Compilation fails
E An exception is thrown at runtime





Solution :


The solution is B.You can write null instanceof integer with no compilation error.

Wednesday, February 10, 2010

SCJP exercise : enum

Exercise :

public class Test {

enum Animal { "CATS", "DOGS", "ELEPHANT"};
public static void main(String [] args) {

Animal myAnimal = Animal.CATS;
if ( myAnimal == Animal.CATS) {
System.out.println("It's a CATS");
} else if (myAnimal.equals(Animal.DOGS)) {
System.out.println("It's a DOGS");
} else {
System.out.println("It's an elephant");
}
}

}


What is the result ? (choose one)

A It's a CATS
B It's a DOGS
C It's an elephant
D Compilation fails
E An exception is thrown at runtime





Solution :


The solution is D.There is an compilation error because the enum syntax is wrong.
It should be : enum Animal { CATS, DOGS, ELEPHANT};

Wednesday, February 3, 2010

SCJP exercise : enum

Exercise :



Given the following :

public class Test {

enum Animal { CATS, DOGS, ELEPHANT};
public static void main(String [] args) {

Animal myAnimal = Animal.CATS;
if ( myAnimal == Animal.CATS) {
System.out.println("It's a CATS");
} else if (myAnimal.equals(Animal.DOGS)) {
System.out.println("It's a DOGS");
} else {
System.out.println("It's an elephant");
}
}

}


What is the result ? (choose one)

A It's a CATS
B It's a DOGS
C It's an elephant
D Compilation fails
E An exception is thrown at runtime






Solution :


The solution is A.There is no errors.You can use enum with == or with the method equals.

Wednesday, January 27, 2010

SCJP exercise : operators

Exercise :



Given the following :

public class Test {


public static void main(String [] args) {

int num = 1;
if ( num == 'a') {
System.out.println("Good result !");
} else {
System.out.println("Result:" + num);
}
}

}


What is the result ? (choose one)

A Good result !
B Result:1
C Compilation fails
D An exception is thrown at runtime






Solution :


The result is C.There is no compilation error.You can compare 1 with 'a' because the comparison use the unicode value of a.So it's correct to compare 1 with 'a'.

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.

Thursday, January 7, 2010

Good year

Hi !

This year we will see many things like certification exercise, Java tips, Go langage ...
So it's exiting !