This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ID:2  SCJP Question of the day : Thursday June 10 2010 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ID:2  SCJP Question of the day : Thursday June 10 2010" Watch "ID:2  SCJP Question of the day : Thursday June 10 2010" New topic
Author

ID:2 SCJP Question of the day : Thursday June 10 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> -----<Your Name>

like , it is my second question and my name is Sahil Rally so in accordance with the above format i used the following Subject line for my topic.

ID:2 SCJP Question of the day : Wednesday June 10 2010 ---Sahil Rally .


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


What is the output , tell with the reason ???
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
NO one answered yet !!!
Is it so easy !!!
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
I does not compile



That shouldn't work. You can't change object reference if it's marked final.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41147
    
  45
Please leave the name out of the subject - it's long enough as it is. What's more, the name is visible wherever the topic title is visible, so it's redundant to begin with.


Ping & DNS - my free Android networking tools app
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Martin Vanyavchich wrote:I does not compile



That shouldn't work. You can't change object reference if it's marked final.



No dear, you can always initialize once the final variables, yes after



you cannot assign anything to s13.

It would compile !!! Now try !!!

Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
First comparison should be true (s1 and s2 reference the same object).
Other comparisons should be false (s5, s8, s11, s15 refrence different objects, concatenation invokes String constructor)

I fear it's not that simple though ; - )


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
The first one is true, that is sure The second and the last one are basicaly the same (whith the exception of the trick I have fallen for , you got me there Sahil) and are both false. The third and fourth ... I'll go with final references beeing as good as string literals, therefore true. As Peter mentioned, concatenation returns a new object, therefore the fourth is false.

To sum up:
true
false
true
false
false
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384



good one ... I almost fell in the trap with final members playing it the rough way ...

anyways ... the answer as per i think is

1. true // for both are literals
2. false // concatenation invokes constructor
3. true // both are final hence can be treated as constants or literals
4. false
5. false


http://plainoldjavaobject.blogspot.in
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Lalit Mehra wrote:

good one ... I almost fell in the trap with final members playing it the rough way ...

anyways ... the answer as per i think is

1. true // for both are literals
2. false // concatenation invokes constructor
3. true // both are final hence can be treated as constants or literals
4. false
5. false



Please elaborate on the explanation for number 3. Why would s8 point to the same object as s1 if s6 and s7 are both final?


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Its all about "Compile time constants"

Since s6 and s7 are both final, hence both s6 and s7 are compile time constants. The sum of two compile time constants is also compile time constant.

Hence s1 and s8 would point to same String literal.

Cheers!!!
Moguluri Ravi Kiran
Ranch Hand

Joined: Apr 16, 2010
Posts: 62

Hi All,

According to me s1 & s2 pointing to the same String objects in String pool.
as Strings are immutable for s1 & s2 , there may not be two String objects that containing "SCJP".
s1 & s2 would refer a same location.

so s1==s2 is true.

in other cases it may not be true.

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

answer is as follows and with the following reasons
1. true // both strings refer the to String literal pool object "SCJP"
2. false// this is because when one of the operand is non final, then concatenation is done at run time and hence the + operator creates a new string and hence s1 and s5 refer to different objects
3.true // here both are final hence compile time constants hence concatenation is done at compile time and hence the variable s8 refers to the string in the constant pool referred by s1
4.false// here both the strings are non final and hence not the compile time constants hence the concatenation is done at the run time and hence the "+" operator creates the new string
5.false // the same reason that is for the third S.O.P.


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Tumi Mathibedi
Greenhorn

Joined: Jun 25, 2010
Posts: 1
1 - 5. false // Strings are immutable in order to compare their values you need to use a.equals(b).


SCJP 1.4, SCWCD 1.4
Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
I comply with the result and reasoning which is submitted by Prasad Kharkar for this excellent question. More Details


M.Sc. in CS, OCPJP6 93%
 
Don't get me started about those stupid light bulbs.
 
subject: ID:2 SCJP Question of the day : Thursday June 10 2010
 
Similar Threads
Question of the day
How I got 94% in the SCJP exam...
ID:1 SCJP Question of the day : Wednesday June 9 2010
ID:5 SCJP Question of the day : Friday June 11 2010
ID:6 SCJP Question of the day : Saturday June 12 2010