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
Joined: Mar 22, 2000
Posts: 12269
1
posted
0
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
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
Joined: Jun 03, 2000
Posts: 277
posted
0
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
Joined: Jun 27, 2000
Posts: 213
posted
0
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
Joined: Jun 28, 2000
Posts: 477
posted
0
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
Joined: May 31, 2000
Posts: 116
posted
0
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
Joined: Nov 22, 2008
Posts: 18944
posted
0
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
Joined: Jan 30, 2000
Posts: 18670
posted
0
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.