• 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

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

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Costa lamona
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B have mentioned the method declaration as erronous in their errata at the link below--
https://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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by xitiz bhatia:
K&B have mentioned the method declaration as erronous in their errata at the link below--
https://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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic