Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link

Stephen O'Kane

Greenhorn
+ Follow
since Aug 17, 2005
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stephen O'Kane

"prints 11 or 12" as you can't be 100% sure whether the second print will be executed before or after i is incremented in the separate thread

Sok
Given



What is the result

a.Prints hello 3 hi 2 bye 1
b.Prints hello 2 hi 1 bye 0
c.Prints bye 3 hello 2 hi 1
d.Prints bye 2 hello 1 hi 0
e.Prints bye 2 followed by an Exception.
f.None of the above.






Answer

e. bye 2 followed by ConcurrentModificationException. When the loop is iterating through the queue, you are not allowed to poll it since it changes the contents of the data structure. The same goes for offer(). Both these methods will appear on the exam.


The only question I have about this answer is, does it apply to all collections, or just those that extend Collection or just those that extend Queue, or just PriorityQueue.

Thanks in advance
Sok
maybe if you had a look around the forum (hint, search function near the top of the page), you might find another topic discussing the exact same problem, i.e.

https://coderanch.com/t/258350/java-programmer-SCJP/certification/comparator-someone-answer

oh, and feel free to try and format your code as well. It makes it much easier for anyone trying to help you. You help us, we help you.

Sok
How else do you think you'd get something from a List?
only right Barry.

I find too many people on this board are posting for things they can easily find out themselves, if they weren't the lazy ones. So many don't seem top even know how to find simple things in the API, or put effort into posting etc.

anyway enough of a rant.
not being a complete expert, I can only suggest you take my opinion with a pinch of salt.

I believe that when you pass in a Float or Double it won't cause a problem, but when you try to return that value you passed in, it will cause a comiler failure, as Float or Double ain't an Integer.

if you pass in a Float, you should imagine the method like this



you cannot return a Float when an Integer is expected

Sok
I recommend looking for a bit more info about them. here's a decent set of questions with a few interesting points to learn

http://www.ejavaguru.com/scjp5freemockexam.php

Sok
It's all in the API if you care to look at it:

PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

The static class PQsort will take two Integer objects and compare them, not according to natural order (which is default for a PriorityQueue), but to reverse natural order (hence return two - one; if it was return one - two; then it would still be natural order).

When a PriorityQueue is created with a Comparator, then it will use the comparator to decide where in the Queue the value should go. If it is created with no Comparator, it uses natural order
The important thing to remember here Reena is that Level1Exception IS-NOT-A Level2Exception, so the catch block for Level2Exception will not be run. BUT, the finally blocks will always run, no matter what.

If the catch block for Level1Exception was not there, then the catch block for Exception would have been run, as Level1Exception IS-A Exception. And, as before, the finally block always runs.
you can widen from short to int, but not from Short to Integer, so your reckoning is correct
If you take the semi colon out after the call to start() things begin to look more obvious. You cannot use a semi colon within the method call println(). When you remove the semi colon you will see that the println() method cannot be called with a void (the return type from start()).

Sok
yep, generally speaking when you buy the book it comes with all the chapters

Sok
I think both options are possible as well, can anyone confirm this, or can anyone explain why ("dir3", file) would be wrong

Sok
Ok, my brain understands it now, thanks Amitabha. After having a look at the API for InterruptedException;

Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.



It seems that the only way for the piece of code where sleep() is called to throw an exception is if interrupt() is called on the thread that was executing when sleep() was called. Since no interrupt is called in the code, then we can assume no exception is thrown (I hope).

Thanks everyone
Sok