• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SCJP 5: Mock questions

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Zak Guesmia
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q16: what is the output of this code? (Choose one)

String[] hello = new String[]{"Hello ", "World", "! "};
System.out.printf("%s %s", hello);

a) NullPointerException
b) Hello
c) Hello Hello
d) Hello World! Hello World!
e) none of the above
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This results in compile error. So h ) should be Compile error

Otherwise modify 9 and 10




and add the following option

true false false false
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Add "Code does not compile option"
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer i1 = 1;

Will this line compile?
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shri mon:
Integer i1 = 1;

Will this line compile?



yes in Java 5.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note that this mock exam is for Tiger, not for previous versions.

Nick
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:
Please note that this mock exam is for Tiger, not for previous versions.

Nick



Moderator ,please edit the subject to include java 5. Thanks.
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2 - a
4-d
5- a (Brilliant question dude)
6-b
7-g
8- a,e

Let me know if I am wrong.
 
Zak Guesmia
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q5 : b
Q8 : a,d

i ll post my explanations later. Plz refer to the generics tutorial and JLS for more details about type conversions and erasure.
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right with q 5. I made a mistake while typing the answer.

Regarding 8,I am confused after reading this
http://www.langer.camelot.de/GenericsFAQ/FAQSections/TechnicalDetails.html#Why%20are%20parameterized%20enum%20types%20illegal?
 
Zak Guesmia
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those questions are based on the JLS, and i have myself tested some of which, ill read this article thoroughly, but tell what goes wrong?
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enum can be parameterized. Here is the compile error that I got

type Enum does not take parameters

 
Zak Guesmia
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enum CANNOT be parameterized. What is the point of parameterizing Enums?

if all enum fields are implicitely final and static, how could you use a type parameter in a static context?
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pasted from the above site

Enum types . Enum types cannot have type parameters. Conceptually, an enum type and its enum values are static. Since type parameters cannot be used in any static context, the parameterization of an enum type would be pointless.



So for 8 , d will give compile error


[ January 12, 2005: Message edited by: Pradeep Bhat ]
 
Zak Guesmia
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i mean declaring parmaterized enums not using the abstract class Enum.

Plz refer to the javadoc, to see the signature of java.lang.Enum
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic