• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

need you help about a Thread target!!!!!

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 46: If you supply a target object when you create a new Thread t = new Thread(targetObject);
What test of instanceof does targetObject have to pass for this to be legal?
Select the one right answer.
a) targetObject instanceof Thread
b) targetObject instanceof Object
c) targetObject instanceof Applet
d) targetObject instanceof Runnable
e) targetObject instanceof String

the given ans:BD
I look up the Thread find the description as following :
public Thread(Runnable target)
target - the object whose run method is called.
So I think :
The target object for a Thread must implement Runnable, which means it will pass the test:
targetObject instanceof Runnable
because the Thread is the suclass that implents the Runnable interface ,it is ok.while the Object is supper-class of the Runnable ,it is no ok.
above all above ,I thought the correct ans is:AD.
If I am wrong ,pls correct me.
You are always to the issue.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you, the correct answer should be A & D, but we also have to keep in mind that every instance of any class(i.e. object) is an instaceof of Object (Object Class).
--Farooq
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be BD.
targetObject is instanceof Runnable doesn't mean it is an instance of its subclass (Thread). Every object should be an instance of Object.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,
The correct answers are A & D as pointed out by James and Muhammad.
First, Thread is not a subclass of Runnable as you wrote.
Second, since the signature of the constructor is
Thread t = new Thread(Runnable r);
the targetObject has to be any instance of a class that implements the Runnable interface, i.e. which provides the run() method.
As you may already know, every class you develop is explicitely or implicitely a subclass of Object
Try writing some code with an Object as argument of the Thread constructor and you'll see that the compiler is not that happy.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO, the answer should be B & D because of the wording of the question: Which test does the targetObject HAVE TO pass for this to be legal?
A: any targetObject that passes this test IS LEGAL in the statement. However, passing this test is not a requirement (and therefore does not pass the "HAVE TO" standard of the question) for it to be legal, since an object may implement Runnable without extending Thread.
B: if the targetObject does not pass this test, the code will not compile. [The instanceof operator only takes object references on the LHS and class types (all of which inherit from Object) on the RHS.]
C: the only way this would be legal is if your class extended Applet and implemented Runnable.
D: this is a requirement since the Thread constructor takes requires a Runnable target.
E: you are definitely out of luck here since String is final (can't be subclassed) and doesn't implement Runnable.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, again and again wording sucks !!!
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the constructor will check if tartget is instance of Runnable.That mean the value b, boolean b=(target instanceof Runnable),it the b==true,everything is OK.
Concern the operator of "instance ",I look up the jdk API:
use the "A instanceof B" to descriptio as following:
if the target A the is a instance of the classs/subclass of class B or interface B or subclass of the interface of B,it return true ,else return false.
As above all ,I think the ans AD is right ,ans B is wrong .If
am wrong ,correct me without hesitation.
Regrd help or be helped!!!
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic