Win a copy of Rust Web Development this week in the Other Languages forum!

Ramesh Mangam

Greenhorn
+ Follow
since May 03, 2005
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 Ramesh Mangam

I tried the code that you gave (although I made some changes like wrapping the sleep statement with a try-catch block, etc) and it did print Finalized 11. But, I am confused because I would think answer '10' is correct. Read the question carefully. The question says '

When the program reaches line 8, how many of the String objects created in line 5 are eligible for garbage collection?



As the string objects are created in the loop, they keep losing their references when the next iteration runs and are eligible for GC. But, just after the end of the loop, the last reference to the String object still exists which means I can use tmp (if it is declared before the loop instead of within the loop) reference after the for loop and it will be poiting to a live object which was the last object created in the loop. This object is not eligible for GC at line 8 as it can be accessible by a live thread. Only after the METHOD (countDown()) completes, will it be eligible for GC. But, I am not able to explain why it is priting Finalized 11 and then BOOM. I would think it should print Finalized1 ... Finalized10, BOOM and then Finalized 11. Actually, can you check if the tmp was declared within the loop or before the loop?

[ October 24, 2005: Message edited by: Ramesh Mangam ]
[ October 24, 2005: Message edited by: Ramesh Mangam ]
Srikanth,

When you extend a abstract class and instantiate an object of subclass, it not only calls its own constructor but also all the superclass constructors in the inheritance hierarchy up to java.lang.Object. There might be some other reasons too. Can someone throw some light on this?
Congrats Bob and Chaubey! Yes. I am looking forward to SCWCD. As always, I will be looking for help from javaranch.
16 years ago
Hi All,

I cleared SCJP just a few hours ago with 86%. It goes without saying that K&B and javaranch were solid resources. I would like to thank the authors and all you guys. Dan's exam were also of great help. All the best for the aspirants.

Ramesh
16 years ago

[ October 20, 2005: Message edited by: Ramesh Mangam ]
Yes. I should have been more specific. Thanks Barry.
Hi Jenny,

Class declaration has nothing to do with method declaration. You have to expicitly give access modifiers to constructors or methods. However, there is one exception. If we do not write any constructors ourselves, in that case the compiler provides us with default constructor and the access modifier for that constructor will be same as the its class's access modifier. Also, a class can have only public or default access levels.
Hi Srikanth,



In the first code above, the flow goes as follows: s1 is pointing to a Sub object, s1 reference is assigned to Base reference base at line 3 which means that base reference is also pointing to the same Sub object as s1. So, line 4 is legal as we can cast a superclass reference to subclass if the superclass reference is actually poiting to a subclass object.

Now, watch the secod code:



Here base refers to Base object. So, when you cast a superclass reference to a subclass reference you will get a runtime error. Hope this helps. But, I am still not sure how there is compile error in the first example. I compiled it and did not get any error. Are you sure there is no typo?
Oh! That's great. Thank you very much.
NOTE: For those who have not taken the SELF TEST on pg 447, this discussion will reveal the answer.
___________________________________________
K&B Pg 447, Ques 2:

Given the following:
class Test1 {
public int value;
public int hashCode() {return 42;}
}
class Test2 {
public int value;
public int hashCode() {return (int) (value^5);}
}

which statement is true?
A. class Test1 will not compile
B. The Test1 hashCode() method is more efficient than Test2 hashCode() method.
C. The Test1 hashCode() method is less efficient than Test2 hashCode() method.
D. class Test2 will not compile
E. The two hashCode() methods will have the same efficiency.

The correct answer, according to K&B, is C as Test1 hashCode() method always returns the same value. I feel the correct answer is E because even the Test2 hashCode() method will always return the same value. The instance variable 'value' of Test2 will be given the default value '0' for all instances and there are no other setter methods that could change its value. So, (int) (value^5) will always return the same value.

Where am I going wrong?

mathmetically integer closest to -7.7 in -8 not -7.so the algorithm is quite right



Then how come the following is NOT working out:
round(8.2) = Nearest integer to (8.2 + 0.5 = 8.7) = 8

Nearest integer to 8.7 is 9. But, the correct answer is 8.
Hi All,

In K&B book, page 371 following definition is given for Math.round():


The round() method returns the integer closest to the argument. The algorithm is to add 0.5 to the argument and truncate to the nearest integer equivalent.



Going by the above definition:
round(8.2) = Nearest integer to (8.2 + 0.5 = 8.7) = 8
round(8.7) = Nearest integer to (8.7 + 0.5 = 9.2) = 9
round(-8.2) = Nearest integer to (-8.2 + 0.5 = -7.7) = -7 //?? (Ans: -8)
round(-8.7) = Nearest integer to (-8.7 + 0.5 = -8.2) = -8 //?? (Ans: -9)

But, for negative numbers, if I subtract 0.5 instead of adding, we get the right answers. So, I think it should be add 0.5 to +ve numbers and subtract 0.5 from negative numbers. Also, I don't think 'nearest integer' are the right words to use. I think it should be add 0.5 to +ve numbers or subtract 0.5 from negative numbers and 'discard decimal portion'. Does anyone have a different opinion on this?

Ramesh


The answer given is 1 and 5. 2 can't be the answer as this can only be called from a constructor.



You could also think of a different reason: Constructors do not have a return type. They are not methods. So, they cannot be used to return 'to' anything.