GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Done with SCJP; Advices and Error in K&B 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 "Done with SCJP; Advices and Error in K&B" Watch "Done with SCJP; Advices and Error in K&B" New topic
Author

Done with SCJP; Advices and Error in K&B

Imran Jack
Ranch Hand

Joined: May 05, 2008
Posts: 31
Hi all,
Cleared SCJP last week with 82%.... Thanks Kathy and Bert for such an excellent book, you have done amazing job by weaving entire SCJP in one text.

Also, Thanks to Whizlabs for such good mock exams, their questions are really conceptual and simulates the mind to further think of possibilities....

PROBLEM with whizlabs is that there are some question irrelevant to SCJP Exam. People are likely to get 10% in actually exam than wht they get in whizlabs.

Also, there is an error I found in K&B, which is not included in Bert's post on this site.... Details are as follows...


ON PAGE-567, the poll() method of PriorityQueue interface has been called in for-loop. This code will generate CurrentModificationException as poll() method modifies the queue. All mates, please be careful if you see such question in Exam.....

Best wishes to all......
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Congratulations
ON PAGE-567, the poll() method of PriorityQueue interface has been called in for-loop. This code will generate CurrentModificationException as poll() method modifies the queue. All mates, please be careful if you see such question in Exam.....

Really ? I don't have the book under hands now, but I recall that it was looping on an array used to populate the queue, not on the queue itself. Something like :


[My Blog]
All roads lead to JavaRanch
Imran Jack
Ranch Hand

Joined: May 05, 2008
Posts: 31
OK yes,
PriorityQueue< Integer> pq = new PriorityQueue<Integer>();
int[] ar = {1,3,5,7,8,95,33};
for (int x : ar)
pq.offer(x);

for (int x : pq)
System.out.println(pq.poll());

System.out.println(pq);
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Congratulations!

Interesting feedback on the exam simulator. I'd like to hear more, if you get the chance.

-Cameron McKenzie
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486


I have slightly modified the code.

output :

true
true
true
true
true
true
1
[3, 7, 5, 33, 8, 95]
3

As far a i know offer is used to add the elements in the queue.
poll()-- prints the highest element in queue
peek()-- prints the highest element in queue and removes it from the queue.
in this case highest element is 95, them how come output is 1 and 3.

please expalin me...


Thanks<br />Dinesh
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

prints the highest element in queue

Saying the highest element is confusing. You should say "The head of the queue". What is the head then ? I have highlighted some important notes from the API :

The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. The head of this queue is the least element with respect to the specified ordering.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Christophe,

you mean to say that poll() and peek() methods of queue will print the least element in the queue which is 1 and 3 in this case. Correct me if am wrong..

What about offer???
Imran Jack
Ranch Hand

Joined: May 05, 2008
Posts: 31
DINESH, peek() and poll() deal with HIGHEST-PRIORITY element, NOT with HIGHEST-VALUE element !!! in PriorityQueue, highest priority element is usually the first element (unless you implement comparator).

Therefore, output is 1 and 3.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Christope says,
The head of this queue is the least element with respect to the specified ordering.

According to you
peek() and poll() deal with HIGHEST-PRIORITY element.

What will happen we use compartor as you have mention and christope??

Can you please help me to understand this, will be very grateful to you..
Imran Jack
Ranch Hand

Joined: May 05, 2008
Posts: 31
well if you implement comparator, then priority will be set as per logic in overridden compareTo() method and NOT as natural order of integers....
Sharma Ritu
Ranch Hand

Joined: Jul 25, 2007
Posts: 261
Congratulations on your wonderful score! We are glad to have contributed to your success! We look forward to contributing to your future certification exams.

Best wishes
Ritu Sharma
--------------------
Whizlabs Software
Success, certified!
World's BEST Java Certification Preparation Solutions
SCJA|
SCJP 1.4| SCJP 5.0 | SCJP 5.0 (Training)|
SCJP 6.0|
SCBCD | SCDJWS | SCMAD
With Unconditional 150% Test PASS Guarantee!
 
GeeCON Prague 2014
 
subject: Done with SCJP; Advices and Error in K&B