my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abhilash's quiz problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abhilash Watch "abhilash New topic

abhilash's quiz problems

Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
there is this great set of 80 questions posted by abhilash...
i thot i was pretty prepared but am all rubbery knees now!
i ahve just tried the first 20 and faltered on many. there are 2 i can't resolve... please help!
Read this piece of code carefully

if(" String ".trim() == "String")
System.out.println("Not Equal");

1.the code will compile an print "Equal".
2.the code will compile an print "Not Equal".
3.the code will cause a compiler error
the answer is2. but it says for trim....
"If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned."
Please help....
the next problem is.....
public class AQuestion
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
return j;
public static void main(String args[])
System.out.println((new AQuestion()).i);

1.Compiler error complaining about access restriction of private variables of AQuestion.
2.Compiler error complaining about forward referencing.
3.No Compilation error - The output is 0;
4.No Compilation error - The output is 10;
The answer is 3. what happens???
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
In the first question the problem is not what trim returns but what '==' does with strings. Keep in mind that Strings are immutable. Regardless of the return of " String ".trim(), which by the way does equal "String", the comparison is with reference variables and not String contents (== versus equals()). To perform the code we will use three different memory locations:
(1) " String "
(2) "String" result of trim
(3) "String" literal for comparison
Then we are saying does 2 and 3 point to the same memory location. The result is NO, therefore the else statement is executed.
In your second example, the important point is that Java assigns non-static member variables as they are given from top down. Default values given by Java to primitive values are: byte, short, integer, and long = 0, float = 0f, double = 0., boolean = false, char = '\0000', and object = null. Therefore, in your example the class starts out with default values for i and j (both 0 because integers). The first assignment happens:
private int i = giveMeJ();
(1) return( 0 ) (because j has value 0 from default!)
(2) i = 0;
Then we perform the next assigment:
private int j = 10;
We now have i = 0 and j = 10. When we print out i we get i = 0!
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
thanx manfred...beautiful answers!
Sandeep Chaturvedi

Joined: Jan 19, 2001
Posts: 22
Can you post the web site address abhilash's quiz problems.
I agree. Here's the link:
subject: abhilash's quiz problems
It's not a secret anymore!