• 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

questions from khalid's mock exam

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Happily living in the valley of the dried frogs with a few tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic