Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Khalid Mock Exam thread

 
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
there are few questions from Khaalid Mock Exam
Since answers are not provided I would be thnakful if you give your views
Given that a static method doIt() in a class Work represents work to be done, what block of code will succeed in starting a new thread that will do the work?
CODE BLOCK A:
Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
Thread t = new Thread(r);
t.start();
CODE BLOCK B:
Thread t = new Thread() {
public void start() {
Work.doIt();
}
};
t.start();
CODE BLOCK C:
Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
r.start();
CODE BLOCK D:
Thread t = new Thread(new Work());
t.start();
CODE BLOCK E:
Runnable t = new Runnable() {
public void run() {
Work.doIt();
}
};
t.run();
a.Code block A
b.Code block B
c.Code block C
d.Code block D
e.Code block E

Only b is correct according to me as we cannot instaniate an Interface
what fo you all say ?
 
jafarali
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A & B are correct!!!

[This message has been edited by jafarali (edited July 24, 2000).]
 
pollai mandala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jafarali,
B is not correct.
coz' here, Thread Object is created but u are overriding the start() method.So if we call start() method it is executed just like other methods but it will not create a new thread.That original functionality is hidden by your new version of start() method.

C is incorrect coz' start() method is not defined and hence invoking that method is a compile time error.
D is also incorrect.
E is similar like case A.Thread object is not created here also.
according to me A is the only correct answer.
what do you all say?
regards
 
jayesh bindra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gaurav,
The correct answer according to me is choice A.
What caught my eye was the statement that you made...
...we cannot instantiate an Interface
This is completly true but I think your concepts aren't totaly clear. Consider the following statements in Code Block A

Please take note that we are not instantiating the interface Runnable and assigning it to t, instead we are assigning to t a reference to the object of the anonymous inner class that implements Runnable. This assignment is completly legal.
Hope this is of some help to you.
Please correct me if I am wrong.
Regards,
Jayesh.
[This message has been edited by jayesh bindra (edited July 24, 2000).]
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to place my vote. A is the only correct answer. I think the explanation has already been given, unless you want more.....
Herbert.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav,
You posted the same question under "Khalid Mock Exam on Garbage Collection" http://www.javaranch.com/ubb/Forum24/HTML/002666.html
and I answered it!....
This got me so because I thought I answered in this thread and my post was not here!!!.
Look at my answers there( right thread, wrong title ).
Ajith
PS : Gaurav, you have two postings with the same title "Khalid Mock Exam on Garbage Collection.". Only the newer one talks about GC, the older one talks about threads. And then you have the same question repeated!.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic