# Martin Sergeant

Greenhorn
since May 20, 2003
Cows and Likes
Cows
0
In last 30 days
0
Total given
0
Likes
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Scavenger Hunt
Ranch Hand Scavenger Hunt
Greenhorn Scavenger Hunt

## Recent posts by Martin Sergeant

Only the method say is synchronized, so the letters thread could call the method say (obtain the lock)
and print a and then leave the method (releasing the lock). Next the numbers thread could enter the
method and obtain the lock and print 1 etc. The only guarantee is that 1-3 and a-c will be called in order
but not necessarily all at the same time.

Martin
12 years ago
This question is taken from the Sierra and Bates mock exam that comes with the SCJP 1.6 book

Given:-

Which of the following when inserted will fufill equals and hascode conrtacts for this class

(a) return ((SortOf)o).bal == this.bal
(b) return ((SortOf)o).code.length()= =this.code.length()
(c) return ((SortOf)o).code.length() *((SortOf)o).bal ==this.code.length() *this.bal
(d) return ((SortOf)o).code.length() *((SortOf)o).bal * ((SortOf)o).rate ==this.code.length() *this.bal*this.rate

I think the answer d is incorrect if you consider the following scenario

object 1 has code.length =2, bal=3 and rate=4
object 2 has code.length =4, bal=2 and rate=3

in this case their hashcodes will be different 2*3 != 4*2
but equals will return true 2*3*4 == 4*2*3
which breaks the hashcode equals contract

Therefore c is the only correct answer

Is this right or am I missing something?

Martin
12 years ago
Sorry for my previous post, answer 2 is probably correct

Martin
12 years ago
In generics you must rememeber that if you have a List<class> reference it can only ever refer
to a List<class>, not a List which is a sub-type of class or a list which has the wild card character ?
(although for backward compatibilty as Raju says, it can refer to a an old-fasioned non-generic list,
but you will get a compiler warning).In this case the return of value of the method is a List<? extends Chewable>,
which is being assigned to a List<Chewable> and List<Meat>, which is illegal

Martin.
12 years ago
Two things

(1)in your private read and write object methods, you are closing the streams- the JVM hasn't finished with them yet
(2) The class coffee needs to implement serializable, although you are serializing you coffee variable separately because
it is marked transient in OrderCoffee, it still needs to have this tag if it is to be used in any read/write object

Martin
12 years ago
Yes, three objects are eligible for GC,:-the object pointed to originally by gb1.g.g no
longer has a reference and also the objects pointed to originally by gb2.g and gb2.g.g also lose
any references pointing to them. I tend to use the same method as W. Joe Smith, although this was
a particularly nasty GC question. It seems to test your ability to solve puzzles rather than your
ability to program or your knowledge of Java.

Martin
12 years ago
This is a frequently asked question see here
Basically the java compiler has difficuty in comparing arrays of primitives . To me this seems like a bug,
but Sun probably wouldn't agree with me. I think they suggest you should not overload var args methods.
Hopefully a question on such an ambiguous topic would not be incuded in the exam

Martin
12 years ago
The answer -6 to 4 (G) is correct
The reason is because if the element is not found, a negative number corrsponding to the index
of where the element would have been is returned. Since you cannot have -0, -1 corrsponds to 0,
-2 to 1 etc. If you serached for a non existing element that would have occurred after the last
element in a 5 element array then the position it should have been would be 5 therefore
-6 would be returned. Try it for yourself

Martin
12 years ago
The problem is that although the setNumber and getNumber methods are synchronized,
the action of getting and setting the number is not atomic. Therfore it is possible
for a thead call getNumber and before it has time to call setnumber , another thread
may nip in and call getNumber, thus both threads will call setnumber using the same
number. This can be avoided by combining setNumber and getNumber in one
synchronized method

Martin
12 years ago
I don't quite understand what is going on
Calling thread.wait() will cause the main thread to pause until another thread calls thread.notify()
As no other thread does, you would expect the program to hang
This is indeed the case if wait is called on a another object
e.g.

will cause the program to hang

A clue to what is going on, however is that 999999 is always the output, so the main
thread does wait but only until the Job thread has completed.
Therefore maybe when thread has run its course it may call notify on itself?

Martin
12 years ago
Sorry, I didn't mean to confuse you, it was merely it was a turn of phrase meaning
that the API is not being consistent because for same strange reason the Boolean valueOf takes a primitive value
(as well as a String) where as the valueOf methods for all the other Wrapper classes just take a String.
I can see no logical reason for this,

Martin
12 years ago
The valueOf method is a static method of all Wrappers except Chraarcter (It wouldn't make much sense for a Character).
It take a String argument and a optional int which specifies the base of the number in the String. Obviously, the Boolean
valueOf() method does not have the optional int paramater (again this would not make sense)

The method returns the Wrapper class, so

Integer i = Integer.valueOf("23"); would be a valid use of valueOf()

However, just to confuse you, the Boolean class has another valueOf () method which takes a boolean primitive.

Hope this all makes sense

Martin
12 years ago
All variables marked final should be initialized. You can initialize them with the default value but you have to do this
explicitly i.e. final int x=0 and not final int x;. With member variables, they do not have to be initialized when they are declared, but must be
given a value in the constructor. If you have multiple constructors or if else blocks you must make sure that the
final variable is given value in all the constructors/blocks (but not necessarily the same value) else you will get a compiler error.

Martin
12 years ago
It would be a waste of memory to hold information about all java classes during a program, so I guess that classes are loaded into memory
when an instance of that class is first created or when a static member is accessed .When there are no objects of that class left
and if there are no references to static data of that class, I guess the class information will be removed from memory (garbage collection)
A potential problem with singletons is that if no object holds a a reference to the Singleton object then then the object and class
will be garbage collected. The next call to getSingleton() will cause the class to reload and a new instance of the Singleton will have to be created
, any data previously associated with it will have been lost. Hence within a program all calls to getSingleton() may not return exactly the same object.

However I shouldn't worry too much about it.
I don't think the Singleton pattern nor Class garbage collection are on the exam (Objective 5.1/5.2 and 7.4)

Martin
12 years ago
The reason for the -1 is because binary search was not called using the same comparator that was used to
sort the array. According to the Sierra and Bates book, this will give an unpredictable result, in this case -1.
I am not sure why exactly -1, i.e what algorithm went wrong etc. But I don't think you need to go in to such depth
for the exam. Just remember that to use binary search on an array that is not sorted using natural order,you need
to use the comparator that was used to sort the array, otherwise the result will be unpredictable.

Martin
12 years ago