Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Booney's Mock exam

 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Marcela Blei
Ranch Hand
Posts: 477
  • 0
  • Mark post as helpful
  • send pies
  • 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);
 
rajsim
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • 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).]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic