• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Booney's Mock exam

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,
This question is from boone�s mock exam.
Question 1: If you supply a target object when you create a new Thread, as in:
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

Given answer is �d� but I think answer should include �a� also because Thread class also implrement Runnable interface or am I missing something?
Please help me.
vivek
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only correct answer is d)
The class must implement the Runnable interface or the constructor will fail.
It would make no sense to attach another Thread to a Thread object, although it is a fairly common beginner error.
Remember to keep straight the distinction between interface and regular class.
Bill
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks bill,
i do agree with u. but just wanna know is it possible pass a thread object as target object for the thread constructor?
thanks again.
vivek
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi bill,
I just try following code and it compiles fine.

public class Test{
public static void main(String argv[]){
Thread a = new Thread(new Thread());
}
}
i know there is no sense of it. just wanna know. what is going here?
vivek
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it will compile since the Thread constructor takes a Runnable object as a parameter, and Thread itself implements the Runnable interface.
But as Bill said it doesn't make much sense to do so.
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think answer should be b) too.
All objects are instance of Object
I tried this code:
Runnable thread = new Runnable() {public void run() {}};
System.out.println(thread instanceof Object);
System.out.println(thread instanceof Runnable);
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The wording of the question is confusing.
However, only way to tell if a new Thread can
be created using an object is by using
"object instanceof Runnable" test. Other tests
listed are either not sufficient or not correct.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
We are stuck up only at a . I guess everything else is clear. Now the question is a kind of trick question
Lets look at the code below
class demo
{
public static void main(String[] args)
{
Test targetObject=new Test();
Thread t = new Thread(targetObject);
if(targetObject instanceof Thread ) System.out.println("Instance of Thread true");

if(targetObject instanceof Object ) System.out.println("Instance of Object true");

if(targetObject instanceof Runnable ) System.out.println("Instance of Runnable true");

System.out.println("Hello World!");
}
}
class Test extends Thread
{public void run(){
}
};
this works fine and prints out
Instance of thread true
Instance of object true
Instance of runnable true
now what happens when i change the Test class to implement Runnable rather than extend thread.
The compiler complains saying that it is impossible for target object to be an instance of Thread.
So what do u do??
The answer can be a , b , d if u r passing an Object that extends Thread, as a parameter to new Thread()
while the answer is b & d if u r passing an object that implements Runnable interface as a parameter to new Thread.
goto to be careful with this one!!. look at what is being said carefully.
Regds.
Rahul
[This message has been edited by rahul_mkar (edited July 26, 2000).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer a is definitely not true. Look again at the original question:

Question 1: If you supply a target object when you create a new Thread, as in:
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


If you say a is true, that means that targetObject must pass the test (targetObject instanceof Thread) in order for the code to compile. This is not true - it may pass that test, in which case the code will definitely compile, but it does not have to.
D is definitely the correct answer.
reply
    Bookmark Topic Watch Topic
  • New Topic