aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ID:1    SCJP Question of the day : Wednesday June 9 2010 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ID:1    SCJP Question of the day : Wednesday June 9 2010" Watch "ID:1    SCJP Question of the day : Wednesday June 9 2010" New topic
Author

ID:1 SCJP Question of the day : Wednesday June 9 2010

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
After discussion with some savvy guys , i am taking an Initiative to start a thread which has following features :-

1) A question would be asked by an Author of the thread.

2) You are required to answer it with reasoning, and without actually executing it in your compiler because it is for your help.

3) At the end of the day , i request SCJP cleared and other savvy java guys to answer it in their words so that it could help SCJP preparing guys like me.

NOTE:- If you ask a question, i request you to follow the following :-

1) Start New topic and do not post your question here.

2) Please use the format for the Subject line of the Topic or thread as follows:-

i.e. ID: <Your Question number> SCJP Question of the day : <TIME>
like , it is my first question and my name is Sahil Rally so in accordance with the above format i used the following Subject line for my topic.

ID:1 SCJP Question of the day : Wednesday June 9 2010


NOTE 2: - Make as challenging Questions as you can while reading the topics and i really hope that this Idea would Rock !!!

Thanks !!!

So here goes my Question :-


SCJP 6.0 96%

(Connecting the Dots ....)
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Question ID 1:



What is the output of the above code ???
Give your reasoning ???

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Your idea rocks and as a fellow aspiring SCJP, would love to participate.

I will try to answer your question, without using compiler. Please let me know how I scored. My answers are commented out in the code below:




Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40



Question ID 1: hint... the Integer.valueOf(int) method is the method, used internally, when the compiler generates code for autoboxing. So... the answers here will be the same, as if autoboxing was used.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

I think the answer is
true
false

If magicSunOfficialSecretNumber value is less than 128 then answer will be

true
true


Life is easy because we write the source code.....
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Please do not tell SUN peple that i know their "magicSunOfficialSecretNumber" .

Pramod P Deore : Yours Answer is Partially True.

Sandra Bachan : Your Answer is Wrong.

Henry Wong : I request you sir to devote some time in explaining the reason of the answers as i love your explanations and like me many other SCJP aspirants too must.


To all people : Please do post questions like me whenever you encounter any good question and please do comply with the Format as i explained above in order to preserve consistency. Thanks !!!



Keep trying !!!
Cheers!!!
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3267
    
  52

Sahil Rally wrote:Please do post questions like me whenever you encounter any good question....

+1 But don't forget to QuoteYourSources if a question is not yours.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Yess please do comply to java ranch rules ofcourse.

and by default if no source is mentioned it means " Its our own creation ". Thanks !!!

Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Something similar was discused in another thread not long ago. Pramod P Deore is right.

Pramod P Deore wrote:I think the answer is
true
false

If magicSunOfficialSecretNumber value is less than 128 then answer will be

true
true


I would just add, that (k == l) returns true if magicSunOfficialSecretNumber is within [-128, 127]


p.s. can there be more than one question of the day?


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34

Martin Vanyavchich

I would just add, that (k == l) returns true if magicSunOfficialSecretNumber is within [-128, 127]


I understood the ans to the 1st part i.e i==j is true but since value of SUNSECRETNO=143 which is greater than 128 then how does this helps in making a faulty comparison,and show (k==l) is false but answer should be true as both k and l hold the value 143.


No Dream Too Big
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
I would just add, that (k == l) returns true if magicSunOfficialSecretNumber is within [-128, 127]


That what was missing in Pramod's answer.

p.s. can there be more than one question of the day?


I would really appreciate if someone comes up with his own questions. Ya We can have many on one day .
But please do comply the format rules and also please increment your Question ID by 1.



None has given the reason of answers yet....Though answer is Completely correct by "Martin Vanyavchich" .

Thanks Cheers !!!
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
I can't find the thread that had the explanation I think it was an older thread referenced in one of the newer ones. Is the answer somehow related to byte range?
Rajesh Lohani
Greenhorn

Joined: May 14, 2010
Posts: 8
I think the answer will be
true
true


" I don't believe in failure. It is not failure if you enjoyed the process. " - Rajesh Lohani
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

Martin Vanyavchich wrote:I can't find the thread that had the explanation I think it was an older thread referenced in one of the newer ones. Is the answer somehow related to byte range?


I agree ... there is not enough explanation given ... although the answer is true, false

it is somehow related to byte i know as one of my friends stated above... but i can't figure it out ...


http://plainoldjavaobject.blogspot.in
Rajesh Lohani
Greenhorn

Joined: May 14, 2010
Posts: 8
I also don't understand the explanation of bytes...
please somebody elaborate it...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

Lalit Mehra wrote:
Martin Vanyavchich wrote:I can't find the thread that had the explanation I think it was an older thread referenced in one of the newer ones. Is the answer somehow related to byte range?


I agree ... there is not enough explanation given ... although the answer is true, false

it is somehow related to byte i know as one of my friends stated above... but i can't figure it out ...


Do a search in the forums on "integer cache". That is the feature which effects this, with autoboxing, and of course with the valueOf(int) method.

And the answer is ... true, followed by either true or false. Basically, the specification defines the range where the integer cache must take place. It doesn't define what will happen if it is outside the range. This means that it is theoretically possible to cache more than specification. In fact, that is what happens with the Sun JVM -- it caches the Long object which is not required by the spec. And it has a -XX switch to allow you to increase the range to cache.

Henry

Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34
Whenever boxing is applied , only one wrapper object exists in the program for primitive values of( boolean,byte,char and int or short):
1).boolean values true or false,
2).a byte,
3).a char in range \u0000 to \u007f,
4),and an int or short value in the range -128 and 127.

so for eg if a and b refer to two wrapper objects that box same value which is among one of those mentioned above ,then a==b is always true,i.e object and reference equality will give same result..
Let us take an example,

but in case if we violate one of the above 4 points ,for eg here let us take point 4,
and do

Thus this proves the result.
Rajesh Lohani
Greenhorn

Joined: May 14, 2010
Posts: 8
Thanks Abhinav.
Now I got it .....
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

Well Thanks, but don't you think since both k and l have the same value they will be truncated to same value after autoboxing too.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
See http://faq.javaranch.com/java/JavaIntermediateFaq#integerAutoBoxing for an explanation.
Rajesh Lohani
Greenhorn

Joined: May 14, 2010
Posts: 8
Both have the same values but both are treated as different object(Because range is outside -128 to 127) as Abhinav stated above.
== compare only for the objects so return false and
in case of equals, it checks for only value and both having same value so it returns true...
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

thanks ulf ... you solved my confusion ...
Abhinav Yadav
Ranch Hand

Joined: Nov 08, 2009
Posts: 34
although it was a good question to make our concepts solid
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Did anyone liked my variable name "magicSunOfficialSecretNumber" ??? or it was silly ???
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

Sahil Rally wrote:Did anyone liked my variable name "magicSunOfficialSecretNumber" ??? or it was silly ???




it is a good one until the sun officials do not complain
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Martin Vanyavchich wrote:Something similar was discused in another thread not long ago. Pramod P Deore is right.

Pramod P Deore wrote:I think the answer is
true
false

If magicSunOfficialSecretNumber value is less than 128 then answer will be

true
true


I would just add, that (k == l) returns true if magicSunOfficialSecretNumber is within [-128, 127]


p.s. can there be more than one question of the day?



OK, now this makes sense. I didn't realize Integer.valueOf() was a method that returned an int, not an Integer object......
suresh dasari
Ranch Hand

Joined: Oct 05, 2009
Posts: 121

This is really a good question, I am confused alot,
finally I got it.
as per my understanding if the value exceeds integer range value, it is stored in a different object, hence the weird result


Sun Certified Java Programmer with 93 percent
pete reisinger
Ranch Hand

Joined: Dec 30, 2009
Posts: 46
Sandra Bachan wrote:
Martin Vanyavchich wrote:Something similar was discused in another thread not long ago. Pramod P Deore is right.

Pramod P Deore wrote:I think the answer is
true
false

If magicSunOfficialSecretNumber value is less than 128 then answer will be

true
true


I would just add, that (k == l) returns true if magicSunOfficialSecretNumber is within [-128, 127]


p.s. can there be more than one question of the day?



OK, now this makes sense. I didn't realize Integer.valueOf() was a method that returned an int, not an Integer object......


Integer.valueOf(int) returns Integer obect, but it caches frequently requested values (-128, 127), so numbers in this range (if they are equal) will return the same object.

java Integer 1.5.0 api

If I understand it correctly.
Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
The answer is:
true
false

Basically, the behavior of the == or equals(Object o) is absolutely depended on the programmer who has written the class.

The equals(Object o) method in the boxed primitive is implemented as that it will return true if the two boxed primitive objects are logically equal.

And the == operator of the boxed primitive is implemented as usual, that is, if two references are refer to the same boxed primitive object on the heap then it returns true, otherwise it returns false. But the implementers of the boxed primitive classes have decided that, the following boxed primitive types: Integer, Short, Character, Byte and Boolean will behave slightly differently if their values are within the certain ranges. For example, for two Integer objects, == operator returns true if the values of these two object within the following range: -128 to 127. This is happened absolutely for using memory efficiently.

M.Sc. in CS, OCPJP6 93%
Mike Peters
Ranch Hand

Joined: Oct 10, 2009
Posts: 67

The answer is:
true
unknown

My motivation for the second answer is that the language specification allows the interning of a wider range than the minimum range specified in the java language specification. So the SUN JVM may return false, but another JVM is allowed to return true for the second example.


Mike Peters
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31057
    
232

Hi Mike, Umesh,
Welcome to JavaRanch!

I noticed that both of you posted in this thread today and have a low post count. Just wondering if you found out about this excellent question of the day series from the Journal or just stumbled upon it.

Thanks,
Jeanne


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mike Peters
Ranch Hand

Joined: Oct 10, 2009
Posts: 67

Hello Jeanne,

I got it from the journal e-mail.

Sincerely,
Mike
Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
Hi Jeanne,
Do you mean Unmesh? Is my reasoning is totally ridiculous? Sorry, this question is not read from any journal. This question is seen here first. My reasoning is reflected from the K&B book. And, what I’ve written is absolutely true for Sun JVM.

Thanks,
u.c.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Unmesh Chowdhury wrote:Do you mean Unmesh?

I'm quite sure Jeanne made a typo in your name so she means Unmesh.
Is my reasoning is totally ridiculous? Sorry, this question is not read from any journal. This question is seen here first.

Jeanne was not questioning your reasoning, she just wanted to know if you came to know about this topic from the journal or not...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
Yes, this question is known to me from the June JavaRanch Journal email. Thanks to all.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31057
    
232

Unmesh,
Sorry about the typo in your name. I knew someone named Umesh and never anyone named Unmesh so my fingers typed it subconciously.

It is good to know you and Mike found out about this from the Journal. Since it is the first Journal in this format, I'm trying to do some analysis on what worked.
Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
That's alright. It's a great forum. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ID:1 SCJP Question of the day : Wednesday June 9 2010