This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About Thread and String. Help! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "About Thread and String. Help!" Watch "About Thread and String. Help!" New topic
Author

About Thread and String. Help!

Frank Wang
Greenhorn

Joined: Aug 18, 2000
Posts: 25
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
Usha Kasi
Greenhorn

Joined: Nov 18, 2000
Posts: 17
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

Lancy Mendonca
Ranch Hand

Joined: Aug 08, 2000
Posts: 54
How is (a) the answer. Is t.start valid. Shouldnt it be t.start() or was that a typo.


Sun Certified Java Programmer<BR>Oracle Certified DBA
Usha Kasi
Greenhorn

Joined: Nov 18, 2000
Posts: 17
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
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Frank Wang
Greenhorn

Joined: Aug 18, 2000
Posts: 25
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Thread and String. Help!
 
Similar Threads
Threads
questions from khalid's mock exam
Khalid Mock Exam thread
Khalid Mock Exam Garbage collection
Mock Blues 1