According to the JLS it is mentioned that :
-It is a compile time error to use a wildcard (?) as an explicit type argument to a generic method or to a constructor.
-It is a compile time error to declare an array whose component type is a parameterized type whose actual arguments include a bounded wildcard.
can anybody show examples of these assumptions? i tried some samples, but i got negative results.
I am sure that there is some folks among us who are already ready for the exam(at least cover entire topics like generics), i'd like to ask them to share with us some useful tricks(or rules)and post some questions along the discussion and try to help each other to overcome this exam. I did not yet take the exam but i think i ll hang on some time before going on.
Herein my contribution (i didnt yet read the objectives):
Q1: What is the output of this program ?
1: Integer i1 = 1;
2: Integer i2 = 1;
3: Integer i3 = 128;
4: Integer i4 = 128;
5: Long l3 = 129l;
6: Long l4 = 129l;
7: Long l1 = 258l;
8: Long l2 = 258l;
9: System.out.print(i1==i2+" "+i3==i4);
10: System.out.print(" ");
11: System.out.print(l1==l2+" "+l3==l4);
(Choose one)
a) true false true false
b) false true false true
c) true true true true
d) false false false false
e) true false false true
f) true false true false
g) false true true false
Q2:
1: Integer i = null;
2: int myInt = i;
3: System.out.println(myInt);
Choose one :
a) NullPointerException
b) 0
c) The program won't compile
d) There is no output
e) NumberFormatException
Q3 :
1: Vector<
String> x = new Vector<String>();
2: Vector<Integer> y = new Vector<Integer>();
3: System.out.println(x==y+" "+x.getClass()== y.getClass());
(Choose one)
a) true false
b) false true
c) true true
d) false false
Q4 :
public class MyCCass {
public final enum RGBColor {
RED, GREEN, BLUE;
}
public static void main(String[] args){
System.out.println(Enum.valueOf(RGBColor.class, "RED"));
}
}
Choose one :
a) NullPointerException
b) RED
c) RGBColor.RED
d) The program won't compile
Q5: Choose the right statement
interface L<T> {
//...
}
class A implements L<Enum> {
//...
}
class B extends A implements L<Long> {
//...
}
Choose one :
a) the code compile without errors
b) the code will not compile because L cannot be inherited with different arguments
c) the code will not compile because L<Enum> is not a legal parameterized type
Q6: Choose the right statement below
public enum SCJPOneDotFour {MEDIUM, EASY, HARD;}
public enum SCJPOneDotFive {MEDIUM, EASY, HARD;}
interface L<T> {
public abstract T getToughness(int ordinal);
}
class A implements L<java.lang.Enum> {
public SCJPOneDotFour getToughness(int ordinal){
SCJPOneDotFour myEnum;
switch(ordinal) {
case 0: myEnum = Enum.valueOf(SCJPOneDotFour.class, "MEDIUM");
break;
case 1: myEnum = Enum.valueOf(SCJPOneDotFour.class, "EASY");
break;
case 2: myEnum = Enum.valueOf(SCJPOneDotFour.class, "HARD");
break;
default : myEnum=null;
}
return myEnum;
}
}
class B extends A implements L<java.lang.Enum > {
public SCJPOneDotFive getToughness(int ordinal){
SCJPOneDotFive myEnum;
switch(ordinal) {
case 0: myEnum = Enum.valueOf(SCJPOneDotFive.class, "MEDIUM");
case 1: myEnum = Enum.valueOf(SCJPOneDotFive.class, "EASY");
case 2: myEnum = Enum.valueOf(SCJPOneDotFive.class, "HARD");
default : myEnum=null;
}
return myEnum;
}
}
Choose one :
a) the code compile without errors
b) the code will not compile because B cannot override getToughness(int) in A ; attemping to use incompatible return types.
c) the code will not compile because L<java.lang.Enum> is not a legal parameterized type
d) the code would compile if B doesnt implement L<java.lang.Enum>
Q7: assume that the following code was compiled with "javac -source 1.4"
public class
Test {
public static void main(String[] args){
B myClass = new B();
System.out.println(myClass.getNumber());
}
private static class A {
public Number getNumber(){
return 1;
}
}
private static class B extends A{
public Integer getNumber(){
return 11;
}
}
}
Choose two :
a) the code compile without errors and prints 11
b) the code compile without errors and prints 1
c) the code will not compile because B cannot override getNumber() in A ; attemping to use incompatible return types.
d) the code will compile if you replace java.lang.Integer by java.lang.Object in the class B
e) RuntimeException
f) the code will compile
g) the code would compile without errors and prints 11 if it is compiled with the "-source 1.5" switch
Q8: Choose the best statements to make the following code compiles without errors
class A{
ArrayList<Color> list;// Line 2
A(){
list = new LinkedList<? extends Color>();// Line 4
list.add(Color.RED);
list.add(Color.GREEN);
list.add(Color.BLUE);
}
public int getSize(){
return list.size();
}
}
Choose two :
a) List<Color> list;//Line 2
list = new ArrayList<Color>(); //Line 4
b)LinkedList<Enum> list;//Line 2
list = new LinkedList<Color>();//Line 4
b)List<Color> list//Line 2
list = new LinkedList<Enum>();//Line 4
d)List<Enum<Color>>//Line 2
list = new LinkedList<Enum<Color>>(); //Line 4
e) None of above
f) only (e) is correct
Q9 :
public class MyCCass {
public abstract enum RGBColor {
RED, GREEN, BLUE;
}
public static void main(String[] args){
System.out.println(Enum.valueOf(RGBColor.class, "RED"));
}
}
Choose one :
a) NullPointerException
b) RED
c) RGBColor.RED
d) The program won't compile
Q10: What is the output of this program ?
Integer i1 = 1;
byte i2 = 1;
short i3 = 200;
Integer i4 = 200;
Long l3 = 256l;
Long l4 = 256l;
long l1 = 258l;
Long l2 = 258l;
Boolean b1 = i1==i2;
Boolean b2 = i4==i3;
Boolean b3 = l2==l1;
Boolean b4 = l3==l4;
System.out.print(b1==b2);
System.out.print(" ");
System.out.print(b3==b4);
(Choose one)
a) true false
b) false true
c) true true
d) false false
e) compile time error
Q11: What is the output of this program ? (Assume the -ea switch turned on at runtime)
Float scjp1_4 = 1.4f;
String tiger = "1.5";
Boolean take = true;
try{
assert !take : Double.valueOf(tiger)>scjp1_4?tiger:scjp1_4;
}catch(AssertionError e){
System.err.println(e.getMessage());
}
Choose one :
a) 1.5
b) it won't compile because Float and String are incompatible types for :?
c) 1.4f
d) it would compile if you replace String by StringBuilder
e) 1.4
Q12: choose one correct statement?
class Instrument {
public Instrument(String name){
}
// methods
}
public class Guitar<T> extends Instrument{
private String model;
private List<String> features;
private float[] stringHeights;
public Guitar(String model, String... features, float... stringHeights){
this.model = model;
this.features = java.util.Arrays.asList(features);
this.stringHeights = stringHeights;
}
// methods
}
a) it will compile if the line 2 is replaced by
public Guitar(String model, String... features, float[] stringHeights)
b) it will compile if the line 2 is replaced by
public Guitar(String... features, float[] stringHeights, String model)
c) it will compile if the line 2 is replaced by
public Guitar<T>(String model, String[] features, float... stringHeights)
d) it will compile successfully if you provide a no-argument constructor in Instrument
e) it will compile successfully.
f) none of the above
Q13: choose the correct statement?
public class Test {
private static void getTotal(String client, Float... cart){
Float[] prices = cart;
Float sum = 0.0f;
for(Float f : prices){
sum+= f;
}
System.out.println("Total for "+client+" :"+sum);
}
public static void main(String[] args){
getTotal("client1", new Float[]{14F, new Float(20)});
getTotal("client2");
getTotal("client3", 41f, 5.0f);
}
}
a) it wont compile because float is not assignable to Float[].
b) it wont compile because zero-length argument lists aren't permitted.
c) IllegalArgumentException
d) none of the above
Q14: choose the correct statement?
public class Test {
private static void print(Object... items){
for(Object f : price){
System.out.println(f);
}
}
public static void main(String[] args){
print(new Float[]{14F, new Float(20)});
print(41f, "Hello world", 'c', null);
}
}
a) it compile successfully but ClassCastException at runtime.
b) NullPointerException
c) It will compile and run successfully.
d) none of the above
Q15: what is the output of this code? (Choose one)
String[] hello = new String[]{"Hello ", "World", "! "};
System.out.printf("%s %c", hello);
a) the code won't compile because printf is not part of PrintStream
b) Hello World!
c) Hello Hello
d) Hello World! Hello World!
e) IllegalFormatStringConversion
f) none of the above
Which that helps a bit
(
SCJP 5: added to topic title)
[ January 12, 2005: Message edited by: Barry Gaunt ]