• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Khalid Mock Exam thread

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A & B are correct!!!

[This message has been edited by jafarali (edited July 24, 2000).]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!.
 
It's a pleasure to see superheros taking such an interest in science. And 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