About Question com.enthuware.ets.scjp.v6.2.82 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
pushpull
Posts: 14
Joined: Thu May 14, 2015 3:25 pm
Contact:

About Question com.enthuware.ets.scjp.v6.2.82 :

Post by pushpull »

Code: Select all

import java.util.*;
class Request { }
class RequestCollector{
    //1 : Insert declaration here
    
    public synchronized void addRequest(Request r){
	container.add(r);
    }
    public synchronized Request getRequestToProcess(){
	return container.poll();
    }
}

What can be inserted at //1?
In the explanation there is
(...)the use of poll() method indicates that it must be a Queue. Both LinkedList and PriorityQueue classes implement Queue interface.
So why the answer

Code: Select all

Queue container = new PriorityQueue();
is wrong?
and those two are good?

Code: Select all

Queue<Request> container = new LinkedList<Request>();
LinkedList container = new LinkedList();

admin
Site Admin
Posts: 10389
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.82 :

Post by admin »

In case of Queue container = new PriorityQueue(); the container is not typed to Queue<Request>. So container.poll() will not satisfy the return type of the method.

pushpull
Posts: 14
Joined: Thu May 14, 2015 3:25 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.82 :

Post by pushpull »

But the LinkedList container = new LinkedList(); isn't typed either to LinkedList<Request>, so why there is no problem here?

admin
Site Admin
Posts: 10389
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.82 :

Post by admin »

You are right. This should also be a wrong option.
Fixed.

thank you for your feedback!
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests