Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

questions from khalid's mock exam

 
vasu matt
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody please explain me the answers for this queries,i do not know the answer actually.these are from khalids mock papersWhich is the earliest line in the following code after which the object created on the line marked (0) will be a candidate for being garbage collected, assuming no compiler optimizations are done?
code/
1)
public class Q76a9 {
static String f() {
String a = "hello";
String b = "bye"; // (0)
String c = b + "!"; // (1)
String d = b;
b = a; // (2)
d = a; // (3)
return c; // (4)
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); // (5)
}
}
1)the line marked(1)
2)the line marked (2)
3)the line marked (3)
4)the line marked (4)
5)the line marked (5)
2)
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();
1)code block A
2)code block B
3)code block C
4)code block D
5)code block E
/code
 
Jenny Xia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Q1, the answer is 3).
the program created three String objects: "hello", "bye" and "bye!" when the main() method executed.
The reference varibles reference to objects in following sequence:
hello 1.a 5.b 6.d
bye 2.b 4.d 5.(b not reference bye any more) 6.(d not reference bye any more)
bye! 3.c
Since bye has no reference variable reference to it, it is ready for gc to collect it at line(3) in your code.
About Q2 you can see explation by Bill Bozeman's post.

[This message has been edited by Jenny Xia (edited December 22, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic