Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Done with SCJP; Advices and Error in K&B

 
Imran Jack
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :
 
Imran Jack
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations!

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

-Cameron McKenzie
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic