Given a string constructed by calling s = new String(“xyzzy” ) , which of the calls listed below modify the string? (Choose all that apply.) A. s.append(“aaa” ) ; B. s.trim(); C. s.substring(3); D. s.replace(‘z’, ‘a’ ) ; E. s.concat(s); F. None of the above why select "f". can anyone tell me? thx. Edited to try to get rid of winkies - Barry [ April 18, 2003: Message edited by: Barry Gaunt ]
That was a bit of a trick question though don't you think?
None of the methods modify the original string, they all create new strings from the original string.
If I put a CD palyer (in addition to the stock AM radio) in my otherwise 'stock' 1957 Chevy; did I 'modify' or 'change' it by creating something different from the original, than the original? Semantics I know, but it's those type of questions (on tests especially ) that seemingly are there merely to trip you up. doco
... that seemingly are there merely to trip you up
I don't think that test designers are the evil deviants that many of us feel they are. Questions such as this one are designed to test your knowledge of immutability and understand its consequenses. Immutability for objects like Strings and the value wrappers such as Integer, Long etc. makes a lot of sense. Object identity in these cases makes little sense, so it is easy to cache those objects or implement the Flyweight pattern using them.
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher