• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

abhilash's quiz problems

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx manfred...beautiful answers!
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anshuman,
Can you post the web site address abhilash's quiz problems.
Thanks
 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic