Anna Chu

Ranch Hand
+ Follow
since May 18, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Anna Chu

Where is the voucher available, I could not find it?
The 17th question in chapter 9 self test:
public class TwoThreads {
static Thread laurel, hardy;
public static void main(String[] args) {
laurel = new Thread() {
public void run() {
System.out.println("A");
try {
hardy.sleep(1000);
} catch (Exception e) {
System.out.println("B");
}
System.out.println("C");
}
};
hardy = new Thread() {
public void run() {
System.out.println("D");
try {
laurel.wait();
} catch (Exception e) {
System.out.println("E");
}
System.out.println("F");
}
};
laurel.start();
hardy.start();
}
}

I think the code cannot compile because "hardy.sleep(1000)" is not correct since sleep() is a static function of Thread class, it should not be invoked by an instance.

Originally posted by Naseem Khan:
Well the answer for this question is one. See the K&B errata section and modify this question. When you modify your question, then objects eligible for GC will be two.



Right from start to end of your program, is c3 pointing to any object? It is always null. So there is no question about gc of c3 object.

cb is set to null. Fine but you must see that both cb and c2 are pointing to same object, so even cb is set to null, still this will not allow CardBoard object for GC as it is still referenced by c2.

Naseem




When excute "cb=null" in go method, cb is assigned to null, but c2 will not assign to null. That's why I think cb should be eligible to GC.
The 10th question in chapter 9 self test:
Assume the following method is properly synchronized and called from a thread A on an object B:
wait(2000);
After calling this method, when will the thread A become a candidate to get another turn at the CPU?
Answer: After object B is notified, or after two seconds.

I have difficulty to understand the question.Can someone explain the question to me? Thanks a lot!
The 9th question in chapter 9 self test:
1. public class WaitTest {
2. public static void main(String [] args) {
3. System.out.print("1 ");
4. synchronized(args){
5. System.out.print("2 ");
6. try {
7. args.wait();
8. }
9. catch(InterruptedException e){}
10. }
11. System.out.print("3 ");
12. } }
Question: what is the result of trying to compile and run this program?
Answer: 1 2; because there will be no return from the wait call since no other thread will notify the main thread, so "3" will never be printed.

My question is: "3" will not be printed because no thread in this program will notify the main thread. Or it is because generally the main thread can not be notified by other thread.
Thanks a lot!
The second question in chapter 3:
class CardBoard{
Short story = 5;
CardBoard go(CardBoard cb){
cb = null;
return cb;
}
public static void main(String[] args){
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
//dostuff
}
The question is: how many objects are eligible for GC when //dostuff is reached.
The answer is: 2; only object (c1) is eligible and its associated Short wrapper object that is also eligible.

My question is: why c3 and cb are not eligible for GC since both of them are null?
Can some one help me with this? Thanks a lot.
I wrote the code below:
public interface Router{
public synchronized void enqueue(Packet p, LinkedList q);
}

The compile error is like this: modifier synchronized not allowed here.
Can someone explain this to me?
Thanks a lot!
Paul, I have assign the download form and accept the successful information yesterday, but the download information didn't send to my email box yet. Do you know how many days will it take to send out the download information.


Thanks!
Congratulations!

How do you think about the real test, is it as difficult as the questions in K&B book or is it easier? I read the book the second time, but there are still some questions I didn't answer correct.

Thanks for your information!
15 years ago
Oh...yes, there is no valid main method. Thank you much to mention that.
Given:
1. public class Foo {
2. public void main( String[] args ) {
3. System.out.println( �Hello� + args[0] );
4. }
5. }
What is the result if this code is executed with the command line?
java Foo world

The result is: The code does not run.
I think this method is inherited from Object. And in Object, it is defined as protected and if any class want to override it, should be at least put the access modifier "protected".
Another problem assignment is listed below:

int i=0;
int a[]={3,6};
a[i]=i=9;
System.out.println(i+" "+a[0]+" "+a[1]);

The result printed out is: 9 9 6.
I am wondering what is the order of the assignment in line 3? I will appreciate if anyone can give some clue about this.
Thanks a lot!
I get a very wierd problem about assigment operator about the code below:

int i=0;
i=i++;
System.out.println(i);

The result printed out is 0.

I don't know why it is not 1. Can someone explain how this happens?
Thanks a lot!
Yes, I think I just confused this situation with the rule that static methods can only access static members.

Thanks a lot!