• 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

InterruptedException or IllegalMonitorStateException, both answers correct but only one required

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:
void waitForSignal() {
Object obj = new Object();
synchronized (Thread.currentThread()) {
obj.wait();
obj.notify();
}
}
Which statement is true?
A. This code can throw an InterruptedException.
B. This code can throw an IllegalMonitorStateException.
C. This code can throw a TimeoutException after ten minutes.
D. Reversing the order of obj.wait() and obj.notify() might cause this method to complete normally.
E. A call to notify() or notifyAll() from another thread might cause this method to complete normally.
F. This code does NOT compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".

A and B both are correct, but only one answer is required.
InterruptedException could be thrown, but IllegalMonitorStateException would definitely be thrown. Which answer should I prefer?

P.S. This is my first question, sorry if I posted it in the wrong place.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What's the source of the question?

Hint: which of A and B is checked exception.
 
Ranch Hand
Posts: 40
2
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure this is the complete question? Because I don't see the right answer at all...
 
Jeet Parekh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@K. Tsang
I found this question online while searching for SCJP practice questions. I can't remember the exact source because I saved it as a text file on my PC, but it was a blog.

RunTime exceptions are unchecked exceptions.
So, InterruptedException is a checked exception. I would have to handle it manually to compile it successfully.
But the program would compile even if I don't handle the IllegalMonitorStateException because it is unchecked.

But the question isn't about successfully compiling, and that's why I am confused about which answer I should prefer.
 
Jeet Parekh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henno Vermeulen wrote:Are you sure this is the complete question? Because I don't see the right answer at all...



Yes this is the complete question, not a single change has been made.
 
Henno Vermeulen
Ranch Hand
Posts: 40
2
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I think the blogger wasn't particularly careful in creating a high quality question. I don't even see a class declaration so this can never be compilable program. Even though I haven't taken the exam yet, I cannot imagine this question being like this on the real exam. I have done some test questions of the K&B OCA/OCP book and I haven't seen them give an incomplete code sample like this without a surrounding class.

That said, even if this code was a method in a valid class, the correct answer would be that the code doesn't compile. Object.wait() may throw a checked InterruptedException. Because of the "handle or declare" rule you must either surround the call to wait in a try/catch block or declare the waitForSignal() to throw it.

I think the author missed the "handle or declare" and thought it is an obvious assumption to make that the method is declared in a valid class.

Let's assume the latter and that waitForSignal() throws InterruptedException. The point of the exercise is probably that you should know that Object.wait() throws an IllegalMonitorStateException if the current thread does not own the lock monitor of the object on which wait is invoked. Can you now see what is the correct answer?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic