SCJP 5.0 Questions
01. MockExam - 01
02. MockExam - 02
03. MockExam - 03
04. AutoBoxing - 1 (10 questions)
05. AutoBoxing - 2 (10 questions)
06. Generics 1 - 6 Questions
07. Generics 2 - 7 Questions
08. Generics 3 - 10 Questions
09. Generics 4 - 10 Questions
10. Generics 5 - 10 Questions
11. Generics 6 - 10 Questions
12. Enum 1 - 10 Questions
13. Enums 2 - 10 Questions
14. Enums 3 - 15 Questions
15. Var Args 1 - 10 Questions
16. Var Args 2 - 10 Questions
17. SCJP 5.0 Mock Questions -1
18. Declaration,Access Control-1
19.Java File IO Package - 1
20.Java File IO Package - 2
21.Language Features - 1
22.New Api's - 1 (10 questions)
23.New Api's - 2 (12 questions)
24.Objective - 1 (20 questions)
25.Objective - 2 (10 questions)
26.Objective - 3 (10 questions)
27.Objective - 4 (10 questions)
28.Objective - 5 (10 questions)
29.Objective - 6 (10 questions)
30.Objective - 7 (10 questions)
350 Mock Questions on SCJP 1.5 - JUST Rs.250 or 7 USD
Send us mail to sales@javabeat.net
more details

SCJP 5.0 Mock Exam -1

Question 5 :Let's have a look at the typeless baskets and the ones where the type is an unbounded wildcards.


// Source A

Basket<?> b5 = new Basket<Apple>();
b5.setElement(new Apple());
Apple apple = (Apple) b5.getElement();
// Source B           

Basket b = new Basket();
b.setElement(new Apple());            
Apple apple = (Apple) b.getElement();
// Source C           

Basket b1 = new Basket<Orange>();
b1.setElement(new Apple());
Apple apple = (Apple) b1.getElement();
Which of the following statements are true?
a)Source A cannot be compiled
b)Source B will be compiled with warning(s).
c)No exception will be thrown during the runtime.
d)Source C will be compiled with warning(s). A ClassCastException exception will be thrown during the runtime

Answer:

Answer5 : a,b
a) The compiler does not know the type of the element stored in b5. That is why it cannot guarantee an apple can be inserted into the basket b5. So the statement s5.setElement(new Apple()) ist not allowed. The methode b5.setElement(..) cannot be used at all.
b) The compiler does not know the type of the element stored in b. That is why it cannot guarantee apples can be inserted into the basket b. But since we did not specify the type of the element of b at all, the compiler will accept the source code and compile it as if it was a pre 1.5 source code. Since the compiler cannot assure the type safety of the compiled code, it will issue a warning.

Get more questions on SCJP 1.5 topics

350 Mock Questions on SCJP 1.5 - JUST Rs.250 or 7 USD
Send us mail to sales@javabeat.net
more details

javabeat | advertise | about us | contact | useful resources
Copyright (2004 - 2013), JavaBeat