Bonjour,

Je suis en préparation du SCJP.

Voici quatre questions dont je ne comprends pas les réponses données par le livre.

Quelqu'un peut-il me corriger ou me dire si j'ai choisi la bonne réponse ?

Question.1
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
package sun.scjp;
public enum Color { RED, GREEN, BLUE }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
package sun.beta;
// insert code here
public class Beta {
Color g = GREEN;
public static void main( String[] argv)
{ System.out.println( GREEN); }
}
The class Beta and the enum Color are in different packages.
Which two code fragments, inserted individually at line 2 of the Beta declaration, will allow this code to compile? (Choose two.)
A. import sun.scjp.Color.*;
B. import static sun.scjp.Color.*;
C. import sun.scjp.Color; import static sun.scjp.Color.*;
D. import sun.scjp.*; import static sun.scjp.Color.*;
E. import sun.scjp.Color; import static sun.scjp.Color.GREEN;
Answer: CE
Pour quoi pas D ?

___________________________________________________

Question.2
Given:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
public String makinStrings() {
String s = “Fred”;
s = s + “47”;
s = s.substring(2, 5);
s = s.toUpperCase();
return s.toString();
}
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
Answer: C
Pour moi la réponse est D, car 4 objets String sont créés à la ligne 2, 3, 4 et 5 ?

___________________________________________________

Question.3
Given classes defined in two different files:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
package packageA;
public class Message {
String getText() { return “text”; }
}
and:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
package packageB;
public class XMLMessage extends packageA.Message {
String getText() { return “<msg>text</msg>”; }
public static void main(String[] args) {
System.out.println(new XMLMessage().getText());
}
}
What is the result of executing XMLMessage.main?
A. text
B. <msg>text</msg>
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 2 of XMLMessage.
E. Compilation fails because of an error in line 3 of XMLMessage.
Answer: E
Moi je dirais plutôt la réponse B !?

___________________________________________________

Question.4
Given the command line java Pass2 and:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
public class Pass2 {
public void main(String [] args) {
int x=6;
Pass2 p = new Pass2();
p.doStuff(x);
System.out.print(” main x = “+ x);
}
 
void doStuff(int x) {
 System.out.print(” doStuffx = “+ x++);
 }
 }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuffx = 6 main x = 6
D. doStuffx = 6 main x = 7
E. doStuffx = 7 main x = 6
F. doStuffx = 7 main x = 7
Answer: B
Moi j'aurai dit C !?

Merci d'avance pour votre aide.