Tuesday, August 10, 2010

Strings: Theory.java


Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 6, Question 9













Which are true? (Choose all that apply.)




A. Compilation fails

 
Incorrect
  

B. The first line of output is abc abc true


Incorrect. Value "d" is appended to s1 hence s1 == "abcd", while s2 == "abc"




C. The first line of output is abc abc false

Incorrect. Value "d" is appended to s1 hence s1 == "abcd", while s2 == "abc"




D. The first line of output is abcd abc false


Correct. While String objects are immutable, references to Strings are mutable. The code s1 += "d"; creates a new String object.




E. The second line of output is abcd abc false


Incorrect. sb1 is equal to sb2.




F. The second line of output is abcd abcd true

Correct. StringBuffer objects are mutable, so the append() is changing the single StringBuffer object to which both StringBuffer references refer.

 
G. The second line of output is abcd abcd false
 
Incorrect. sb1 is equal to sb2.

No comments:

Post a Comment