neha verma

Ranch Hand
+ Follow
since Jun 17, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by neha verma

Hi Raghu,
Can you please let me know how much you scored in mock tests. how did you find the difference between mock exams and actual test.
Can you give me some advice what you felt after the exam.
Thanks,
Neha
17 years ago
Hi Ravi,
Congrats..........
did you appear for the mock tests , how much did you score in the mocks.Was the actual test different from mocks?
Were there things outsise K&B book.
Thanks,
Neha.
17 years ago
congrats Satya.
Are the thread questions on exam difficult than Whizlabs or of the same standard?
17 years ago
Annette Sovereign -Yes there is a typo error in the code above- its hasCode() method not hashcode(). It is my mistake. This is a question from K&B mock exam.
Krithika -I am still not clear about the fact that why answer 1 is also correct. As we can prove that when are equals method returns true the hashCode() method may fail. So does'nt it violate the hashCode() contract.
Hi,
Here is another question-

The output is:
Parent's method2()
Parent's method1()
Now in this example Parent s method1 is called not Child 's method1 as compared to previous example. I am confused.


--- indenting repaired, Bu.
[ September 16, 2007: Message edited by: Burkhard Hassel ]
Hi,
Can you please tell which all objects are lost?In such kind of question should we answer the total number of objects created or the objects created minus the objects lost.
Another thing I guess you are not counting "47" as a String object.
Please clarify.
Hi Marc,
What I am able to understand is -
As object of Derived class is used to invoke the Base class constructor and further upon amethod(), therefore Derived class overridden amethod is called.
In case the amethod is static then the method is not overridden and so Base class amethod will be invoked.
Please correct me if I am wrong.
Thanks Marc.
hi chand,
As per my understanding 5 objects are created and only one of the objects remains rest are all lost . So when the questions says how many objects are created when this method is invoked, which all objects are you counting. Can you please list them.
Thanks.

As per my understanding 5 objects should be created-
"Fred"
"47"
"Fred47"
"ed4"
"ED4"
Please verify if I am correct.
Thanks.

The answer is
Derived.amethod()
99
Derived.amethod().
I did not understand why did the base constructor call the method amethod of Derived class instead of Base class.
Hi ,
This is a example given in K&B book-

Remember that when you use a class that implements Map, any classes that you
use as a part of the keys for that map must override the hashCode() and equals()
methods.


We get null when we try to get the Cat object as Cat class does not implements hashCode and equals.
But just look at the example below-
import java.util.*;
class Dog {
public Dog(String n) { name = n; }
public String name;
public boolean equals(Object o) {
if((o instanceof Dog) &&
(((Dog)o).name == name)) {
return true;
} else {
return false;
}
}
public int hashCode() {return name.length(); }
}
class Cat { }
enum Pets {DOG, CAT, HORSE }
class MapTest {
public static void main(String[] args) {
Map<Object, Object> m = new HashMap<Object, Object>();
m.put("k1", new Dog("aiko")); // add some key/value pairs
m.put("k2", Pets.DOG);
m.put(Pets.CAT, "CAT key");
Dog d1 = new Dog("clover"); // let's keep this reference
m.put(d1, "Dog key");
Cat c= new Cat();
m.put(c, "Cat key");
System.out.println(m.get("k1")); // #1
String k2 = "k2";
System.out.println(m.get(k2)); // #2
Pets p = Pets.CAT;
System.out.println(m.get(p)); // #3
System.out.println(m.get(d1)); // #4
System.out.println(m.get(c)); // #5
System.out.println(m.size()); // #6
}
}
Now when we try to add a Cat oject c then we are able to get the answer Cat Key. Does it mean that when we say m.get(new Cat()) we pass a new Cat object as the key not the the same key which we had used to add


m.put(new Cat(), "Cat key");.


Can somebody please explain-when Cat does not implement hashCode() then how are we able to get when we use m.get(c).
Thanks.
Thanks for the clarification. So this is a wrong answer given in mock Master exam that comes alongwith K&B book. Correct.
Hi Bukhard,
I dont agree with you, what example Katrin has given means that two objects are equal but still their hashcodes are not equal.
So how does 1 option fulfill the hashcode contract.Can you please explain.

What code can be inserted in equals method so that it fulfills the hashcode contract?
Two correct answers are-
1. return((SortOf)o).code.length()*((SortOf)o.bal)*((SortOf)o.rate==this.code.length()*this.bal*this.rate;

2. return((SortOf)o).code.length()*((SortOf)o.bal))==this.code.length() *this.bal.

I am not able to understand how 1 option is also correct.
It is possible that equals method can be return true while hashcode is still not equal as we can have such combination of length*rate.
Please clarify.



--- indenting repaired, Bu.
[ September 13, 2007: Message edited by: Burkhard Hassel ]