• 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

K&B Question Chapter 2

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the question from K&B book chapter 2


Which two statements are true for any concrete class implementing the
java.lang.Runnable interface? (Choose two.) ?

A. You can extend the Runnable interface as long as you override the public run() method.
B. The class must contain a method called run() from which all code for that thread will be initiated.
C. The class must contain an empty public void method named run().
D. The class must contain a public void method named runnable().
E. The class definition must include the words implements Threads and contain a method called run().
F. The mandatory method must be public, with a return type of void, must be called
run(), and cannot take any arguments.



The answer given is B & F

The explanation given in the book is as follows


When a thread�s run() method completes, the thread will die. The run()
method must be declared public void and not take any arguments.
A is incorrect because classes can never extend interfaces.
C is incorrect because the run() method is typically not empty; if it were, the thread would do nothing.
D is incorrect because the mandatory method is run().
E is incorrect because the class implements Runnable.



I understand why F is correct, however I am not understanding why B should be correct. The option B says about method run is needed, however does not specify about the access modifier.

On the contrary option C, I feel is correct because it does specify the run method to be public ( which is mandatory condition ). Option C does say about the run method being empty, which I feel should be fine because the question doesn't seem to test on the implementation part of the overridden run method.

Guys, do post your views, I might be missing here something.

TIA
[ December 18, 2004: Message edited by: Jay Pawar ]
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand your quibble with choice B; however, choice C is wrong because it reads "The class must contain an empty public void method named run()" (bold and italics added). If must must were replaced with may, choice C would be correct. The explanation given by the author gives the reason that a programmer shouldn't and not a reason that a programmer can't.

Returning to choice B ("The class must contain a method called run() from which all code for that thread will be initiated."), I read the statement as stipulating a necessary but not sufficient condition.

Reflecting upon the choices, B & F are the best two, per the question's hint, choices. If, however, choice C were rewritten, I believe C would be a better choice than B.
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the detailed explanation. Got to be watchful with those words.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic