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