Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

string question

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found one strange question,


Options :

a) true true true true true true
b) true false true false true false
c) true true false false false false
d) true false true false false false
e) true true false true false false
f) false false false false false false
g) compile-time error

Correct answer is : D

but according to me answer should be F bcoz string is imutable so it should return new instance of string object.

???
[ July 19, 2005: Message edited by: jigar parekh ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us the URL from which you obtained that question.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
my option is true false true false false false.


Because trim() and replace create a new instance of objects only when target string was in the suitation modifiable. otherwise it returns the same target string.

so that "String".trim() == "String".trim() , "String".replace('t','t') == "String".replace('t','t')returns true.

so that "String ".trim() == "String ".trim() , "String".replace('t','T') == "String".replace('t','T')returns false.
These functions ara in the suitation value modifiable.

replace all always create a new string whether its in the suitation modifiable or not.

I hope it will helps you.

Regards
Kasimurugan R.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by jigar parekh:
I found one strange question,


Options :

a) true true true true true true
b) true false true false true false
c) true true false false false false
d) true false true true false false
e) true true false true false false
f) false false false false false false
g) compile-time error

Correct answer is : D

but according to me answer should be F bcoz string is imutable so it should return new instance of string object.

???



F is the correct answer, because the == operator is used to check whether two variables refer to the same object. In the instance of the example "String".trim(), "String".replace , etc all refer to different objects, therefore you'll get false. If you wanted to compare the contents of the String variable then use the equals() method. So "String".trim().equals("String".trim()) would give you true.


Thanks
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output will be "true" because of String pooling.

P.S. By the way, you can ask your compiler to check it for you
[ July 19, 2005: Message edited by: George Bolyuba ]
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

My understanding is as follows.


This will return true, obvious.


Here, note the space in "String ".

So, when executing, the Left Hand Side would be "String", after trim() and it will be compared, with the Right Hand Side, before the trim() is performed there.

So, It would return false. Similarly for the other statements. It is due to the precedence in execution.

Please correct me, if I am going wrong.

Cheers,
Ram
[ July 19, 2005: Message edited by: Ramaswamy Srinivasan ]
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ramaswamy Srinivasan:

So, when executing, the Left Hand Side would be "String", after trim() and it will be compared, with the Right Hand Side, before the trim() is performed there.



Why the result of first trim() should be compared with "String " rather when with "String ".trim() ??? This is wrong.

For the right answer see post by KasiMurugan Ramasamy.


trim() and replace create a new instance of objects only when target string was in the suitation modifiable.


[ July 19, 2005: Message edited by: George Bolyuba ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same here

true
false
true
false
false
false

Deepraj
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Al Morrissey:
I think your question was wrong. I compiled and run your code and I got this output and it does not match with any of the answers:

true
false
true
false
false
false



That matches with the answer provided in the original post.
We are looking for explanations. Le me try:
If you use String literals, then JVM uses the existing Object from heap if it exists there.
The trim() returns same string if there is nothing to trim. Otherwise returns a new String.
Similaraly replace and replaceAll, return same Object if resulted String an original String are same. Otherwise creates a new Object.
Think about the code in light of these 3 sentances and it will clarify the behaviour.

Cheers!
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone out there tell me from what document that question is from?
 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by George Bolyuba:
[qb]
Why the result of first trim() should be compared with "String " rather when with "String ".trim() ??? This is wrong.

For the right answer see post by KasiMurugan Ramasamy.

[qb]



George,

You are right, and I went wrong there. After doing some work on that, i came to know the internals. This was a good one, infact.

Thanks, for poiting that out...

Cheers,
Ram
[ July 19, 2005: Message edited by: Ramaswamy Srinivasan ]
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm going to take SCJP test and want to know if questions like this one will appear on the exam.

For example do I have to know the fact that replace() will return reference on original object if object will not be changed and replaceAll() will always create a new object?
[ July 20, 2005: Message edited by: George Bolyuba ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by George Bolyuba:
Hi

I'm going to take SCJP test and want to know if questions like this one will appear on the exam.

For example do I have to know the fact that replace() will return reference on original object if object will not be chenged and replaceAll() will always create a new object?

[ July 20, 2005: Message edited by: George Bolyuba ]



No, this type of question will not appear on your SCJP exam.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi



output:
false
true
false
false
[ July 20, 2005: Message edited by: George Bolyuba ]
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic