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

About Thread and String. Help!

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, friends, I know whenever I have questions I can turn to you for help, because you are so good to be in this wonderful saloon.
Question 1:
Which expression will evaluate to ture if preceded by the follwing code? Select all valid answers:
<space type=horizontal size=18>String a = "hello";
<space type=horizontal size=18>String b = new String(a);
<space type=horizontal size=18>String c = a;
<space type=horizontal size=18>String char[] d = {'h', 'e', 'l', 'l', 'o'};
(a) (a == "hello")
(b) (a == b)
(c) (a == c)
(d) a.equals(b)
(e) a.equals(d)
The answer is (c) and (d). But my answer is (a), (c) and (d). I tried the code below and got true printed out. Would someone like to try it again? Thank you.
Public class Try{
<space type=horizontal size=18>public static void main (String argv[]){
<space type=horizontal size=36>String a = "hello";
<space type=horizontal size=36>boolean b = a == "hello";
<space type=horizontal size=36>System.out.println(b);
<space type=horizontal size=18>}
}

Question 2:
Give 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? Select all valid answers.
(a)
Runnable r = new Runnable(){
<space type=horizontal size=18>public void run(){
<space type=horizontal size=36>Work.doIt();
<space type=horizontal size=18>}
};
Thread t = new Thread(r);
t.start;
(b)
Thread t = new Thread();
<space type=horizontal size=18>public void start(){
<space type=horizontal size=36>Work.doIt();
<space type=horizontal size=18>}
};
t.start();
(c)
Runnable r = new Runnable(){
<space type=horizontal size=18>public void run(){
<space type=horizontal size=36>Work.doIt();
<space type=horizontal size=18>}
};
r.start();
(d)
Thread t = new Thread(new Work());
t.start();
(e)
Runnable r = new Runnable(){
<space type=horizontal size=18>public void run(){
<space type=horizontal size=36>Work.doIt();
<space type=horizontal size=18>}
};
r.run();
The answer is (a). But I think none is right. Since Runnable is interface and implicitly abstract, how can it be created an object here. Would you guys explain it?
Thanks a lot. With regards!
Frank
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frank,
I think for ques 1 -(a), (c) and (d) are right. The comparison is made from the literal string pool itself and so (a) returns true.
For ques 2, if ur query is about the new operator on an interface, then here an instance of interface Runnable is not created. Actually as I understand from the JLS, "an anonymous subclass of Object that implements the interface" Runnable is created. So it is an anonymous subclass that is instantiated and it is of type Object and it implements Runnable interface.
To get more info on this goto : http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#41147
and read subsections on Class Instance creation expression
Regards
Usha Kasi

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is (a) the answer. Is t.start valid. Shouldnt it be t.start() or was that a typo.
 
Usha Kasi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I think it must be a typo, because except for that, the code will work fine . And I don't think other choices are right too.
Frank, was the ques having start without braces ? Can I know, from which exam or book this ques is from ??
Regards
Usha
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the answer (a) has a typo, it should be t.start(). This question has been discussed here a lot of times, and I verified that it is actually a typo. You folks might want to read the related discussion here.
Have fun
Ajith
[This message has been edited by Ajith Kallambella (edited August 22, 2000).]
 
Frank Wang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, guys, first thank all of you very much, especially Usha. I am quite clear now. Second there surely is a mistake in answer (a), t.start; should have been t.start();.
Best wishes to you all!
Frank
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic