Wednesday, January 23, 2008

Some Questions on Objects for SCJP 5

Q1 What is the result of executing the following fragment of code:
boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = !b2)
{ System.out.println("true");
else { System.out.println("false");
Select 1 correct option
(1)Compile time error
(2)It will print true
(3)It will print false
(4)Runtime error
(5)It will print nothing
Answer : 1
Explanation : Note that, boolean operators have more precedence than =. (In fact, = has least precedenace) so, in (b2 != b1 = !b2) first b2 != b1 is evaluated which returns a value 'false'. So the expression becomes false = !b2. And this is illegalbecause false is a value and not a variable!Had it been something like (b2 = b1 != b2) then its valid because it will boil down to : b2 = false. Because all an if() needs is a boolean, now b1 != b2 returns false which is a boolean andas b2 = false is an expression and every expression has a return value (which is actually the LHS of the erpression). Here it returns true which is again a boolean.Note, return value of expression (i is int) : i = 10 , is 10 (int).


Given two collection objects referenced by c1 and c2, which of these statements are true?Select 2 correct options
(1)c1.retainAll(c2) will not modify c1
(2)c1.removeAll(c2) will not modify c1
(3)c1.addAll(c2) will return a new collection object, containing elements from both c1 and c2
(4)For: c2.retainAll(c1); c1.containsAll(c2); 2nd statement will return true
(5)For: c2.addAll(c1); c1.retainAll(c2); 2nd statement will have no practical effect on c1

Answer : 4,5
Explanation : public boolean retainAll(Collection c) retains only the elts in this collection that are contained in the specified collection. In other words, removes from this collection all of its elts that are not contained in the specified collectionpublic boolean removeAll(Collection c) removes all this collection's elts that are also contained in the specified collection. After this call returns, this collection will contain no elts in common with the specifiedcollectionpublic boolean containsAll(Collection c) returns true if this collection contains all of the elts in the specified collectionpublic boolean addAll(Collection c) adds all the elts in the specified collectionto this collection. The behavior of this opern is undefined if the specified collection is modified while the opern is in progress(ie., the behavior of this call is undefined if the specified collection is this collection, and is nonempty)


Q 3
What happens when the following code gets executed:

class Sample {
public static void main(String[] args)
{ double d1 = 1.0;
double d2 = 0.0;
byte b =1;
d1 = d1/d2;
b = (byte) d1;
(1)It results in the throwing of an ArithmeticExcepiton
(2)It results in the throwing of a DivedeByZeroException
(3)It displays the value 1.5
(4)It displays the value –1
Answer : 4
Explanation : 1.0/0.0 results in Double.POSITIVE_INFINITY. Double.POSITIVE_INFINITY is converted to Integer.MAX_VALUE ('0' followed by 31 '1's). Integer.MAX_VALUE is then cast to byte value, which simply takes the last 8 bits(11111111) and is -1.


Class finalization can be done by implementing the following method:static void classFinalize() throws Throwable;True Or False?
Answer : 2
Explanation : PREVIOUSLY: If a class declares a class method classFinalize that takes no arguments and returns no result: static void classFinalize() throws Throwable { . . . } then this method will be invoked before the class is unloaded . Like thefinalize method for objects, this method will be automatically invoked only once. This method may optionally be declared private, protected, or public. NOW: Class finalization has been removed from the Java language. Thefunctionality of JLS 12.7 is subsumed by instance finalization (JLS 12.6).Here is a rationale for this decision. thing has happend toclass unloading: A class or interface may be unloaded if and only if its class loader is unreachable (the definition of unreachable is given in JLS 12.6.1). Classes loaded by the bootstrap loader may not be unloaded.


Consider the following method:

public void getLocks(Object a, Object b)
{ synchronized(a)
{ synchronized(b) { //do something } } }
and the following instantiations:
Object obj1 = new Object();
Object obj2 = new Object();
obj1 and obj2 are accesible to two different threads and the threads are about to call the getLocks() method.Assume the first thread calls the method getLocks(obj1, obj2).
Which of the following is true? Options Select 1 correct option
(1)The second thread should call getLocks(obj2, obj1)
(2)The second thread should call getLocks(obj1, obj2)
(3)The second thread should call getLocks() only after first thread exits out of it
(4)The second thread may call getLocks() any time and passing parameters in any order
(5)None of the above

Answer : 2
Explanation : (1) This may result in a deadlock (3) The is not necessary. Option 2 works just fine.


Search Amazon for Best Books on Java J2EE


blogarama - the blog directory

Search your favourite topics