GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ch7, selftest q14, and q16, clarifying my questions!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Ch7, selftest q14, and q16, clarifying my questions!!" Watch "Ch7, selftest q14, and q16, clarifying my questions!!" New topic
Author

Ch7, selftest q14, and q16, clarifying my questions!!

Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
Hi
It looks like no one have the time to open the K & B book and look at what is confusing me , but it is ok, I will rewrite my questions.

Chapter 7, question 14, on page 632
read Choice (A)

<? extends CharSequence> makes it not correct because, It will return something "read only"

Is that Correct?? I can't find any other reasons.

Chapter 7, question 16, on page 634
read Choice (C)


I don't get it, the Answer on the same pages says
C is wrong because the return type evaluates to List<Integer> and
that can't be assigned to a variable of type List<Number>


But the return type is List<? super E> not List<E>
so the method can return List<Number> , isn't it ?
------------------------------------------------------
Why no one wants to play with me


SCJP 5
Dan Polak
Ranch Hand

Joined: Nov 06, 2006
Posts: 32
Originally posted by Mohammed EL-Adawi:

Chapter 7, question 14, on page 632
read Choice (A)

<? extends CharSequence> makes it not correct because, It will return something "read only"
Is that Correct?? I can't find any other reasons.


No it isn't.

The real reason is that Collection<? extends CharSequence> doen't fit to Collection<String>

________________________________________________________________________
------------------------------------------------------------------------
WARRNING in my opinion in question 16/page 364 is BIG BUG

answer should be G
________________________________________________________________________
------------------------------------------------------------------------

Given a method declared as:
public static <E extends Number> List<? super E> process(List<E> nums)
A programmer wants to use this method like this:

// INSERT DECLARATIONS HERE

output = process(input);

Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow

the code to compile? (Choose all that apply.)
A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;

B. ArrayList<Integer> input = null;
List<Integer> output = null;

C. ArrayList<Integer> input = null;
List<Number> output = null;

D. List<Number> input = null;
ArrayList<Integer> output = null;

E. List<Number> input = null;
List<Number> output = null;

F. List<Integer> input = null;
List<Integer> output = null;

G. None of the above.
[ November 14, 2006: Message edited by: Daniel Charczynski ]

<a href="http://www.dantheman.pl" target="_blank" rel="nofollow">http://www.dantheman.pl</a>
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
Originally posted by Daniel Charczynski:
[QB]

No it isn't.

The real reason is that Collection<? extends CharSequence> doen't fit to Collection<String>;
QB]


thanks but I still confused

If coment out the code inside the main, and then compile with the answer A, would it work, and why??

I can try it my self, but I have a doubt, and I want to understand

the method getLongWords returns longWords which is Collection<String>, and this should fit into Collection<? extends CharSequence>, because String is a CharSequence, ie <String> is <? extends CharSequence>

but what in the main is saying

assign <? extends CharSequence> to <String>,
where the vis-versa is ok but this is not.
Dan Polak
Ranch Hand

Joined: Nov 06, 2006
Posts: 32
For example

if you write your own class NewClass extends CharSequence ...
it couldn't fit to String

so

<? extends CharSequence> doesn't fit to <String>
xitiz bhatia
Greenhorn

Joined: Nov 15, 2006
Posts: 2
Daniel,
Regarding your reply to q16 from K&B posted in this thread..I am including the question for reference.
-----------------------
Question 16:

Given a method declared as:
public static <E extends Number> List<? super E> process(List<E> nums)

A programmer wants to use this method like this:

// INSERT DECLARATIONS HERE
output = process(input);

Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow
the code to compile? (Choose all that apply.)

A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;

B. ArrayList<Integer> input = null;
List<Integer> output = null;

C. ArrayList<Integer> input = null;
List<Number> output = null;

D. List<Number> input = null;
ArrayList<Integer> output = null;

E. List<Number> input = null;
List<Number> output = null;

F. List<Integer> input = null;
List<Integer> output = null;

G. None of the above.

-----------------------
According to book (A) and (D) are wrong because the return type of process is definitely declared as a List, not an ArrayList.
But why is (C) wrong??

why is (G) the answer according to you?

Xitiz
[ November 15, 2006: Message edited by: xitiz bhatia ]
xitiz bhatia
Greenhorn

Joined: Nov 15, 2006
Posts: 2
K&B have mentioned the method declaration as erronous in their errata at the link below--
http://www.coderanch.com/t/257589/java-programmer-SCJP/certification/SCJP-Errata-Updated

Now the method declaration becomes-
public static <E extends Number> List<E> process(List<E> nums)

(C) is wrong because of the reason given in book.
[ November 15, 2006: Message edited by: xitiz bhatia ]
Dan Polak
Ranch Hand

Joined: Nov 06, 2006
Posts: 32
Originally posted by xitiz bhatia:
K&B have mentioned the method declaration as erronous in their errata at the link below--
http://www.coderanch.com/t/257589/java-programmer-SCJP/certification/SCJP-Errata-Updated

Now the method declaration becomes-
public static <E extends Number> List<E> process(List<E> nums)


now answer is B,E,F

i missed it in errata
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
Originally posted by Daniel Charczynski:
For example

if you write your own class NewClass extends CharSequence ...
it couldn't fit to String

so

<? extends CharSequence> doesn't fit to <String>


Thanks Daniel
this example just fit to me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ch7, selftest q14, and q16, clarifying my questions!!