Forums Register Login

abhilash's quiz problems

+Pie Number of slices to send: Send
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!
1.)
Read this piece of code carefully

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

Answers
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.....
2.)
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);
}
}
Answers

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???
+Pie Number of slices to send: Send
Anshuman,
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!
Regards,
Manfred.
+Pie Number of slices to send: Send
thanx manfred...beautiful answers!
+Pie Number of slices to send: Send
Anshuman,
Can you post the web site address abhilash's quiz problems.
Thanks
I found some pretty shells, some sea glass and this lovely tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 839 times.
Similar Threads
Access Modifiers
Abhilash Java Quiz
order of loading??????
Angelfire.com....quiz 2
Angelfire.com....quiz 2
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:11:43.