Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1Prints: ABABCABC  Instances of type StringBuffer are not immutable. In method m1, the method invocation expression s1.append("B") appends the String literal "B" to the StringBuffer instance referenced by variable s1. The append method returns a reference to the same StringBuffer instance on which it is invoked; so the assignment expression s1 = s1.append("B") does not assign a different reference value to variable s1. The new value, AB, is printed in method m1. In method m2, the method invocation expression s1.append("C") appends the String literal "C" to the StringBuffer instance referenced by variable s1. The new value, ABC, is printed in method m2. In the main method, a copy of the reference value contained by the reference variable s1 is passed as an argument to methods m1 and m2. Since StringBuffer instances are not immutable, methods m1 and m2 are able to change the original StringBuffer instance that is declared in the main method. The new value, ABC, is printed in the main method.  
2Prints: ABCAB  Instances of type StringBuffer are not immutable. In method m1, the method invocation expression s1.append("B") appends the String literal "B" to the StringBuffer instance referenced by the parameter variable s1. The new value, AB, is printed in method m1. The reference variable s1 declared in the main method refers to the same modified StringBuffer instance. In method m2, the class instance creation expression new StringBuffer("C") creates a new instance of type StringBuffer containing the value C. The assignment expression s1 = new StringBuffer("C") assigns a reference to the new StringBuffer instance to the method parameter variable s1. The value C is printed in method m1. The method local reference variable s1 in the main method remains unchanged by the assignment expression contained in method m2. In the main method, a copy of the reference value contained by the reference variable s1 is passed as an argument to methods m1 and m2. Since StringBuffer instances are not immutable, method m1 is able to change the original instance of the StringBuffer declared in the main method. Since references are passed by value, method m2 can not change the reference variable declared in the main method. Regardless of anything that happens in method m2, the reference variable s1 that is declared in the main method will continue to reference the original StringBuffer instance. Since the content of the original instance was modified by method m1, the new value, AB, is printed in the main method.  
3Prints: false,false  StringBuffer.equals does not override the Object.equals method; so StringBuffer.equals just compares reference values. Since the reference variables s1 and s2 refer to different objects, the equals method of the StringBuffer instance s2 returns the value false. The String.equals method does override the Object.equals, method. The String.equals method returns false anytime the argument is not an instance of type String. The method invocation expression s1.equals(s2) produces the value false, because the argument is an instance of type StringBuffer.  
4Prints: true,false  String.equals overrides Object.equals. The String.equals method compares the contents of the String instances--not the references. Since the contents of s1 and s2 are the same, the method invocation expression s1.equals(s2) produces the value true. The StringBuffer.equals method does not override Object.equals. The StringBuffer.equals method compares the reference values--not the contents of the StringBuffer instances. Since the reference values sb1 and sb2 are different, the method invocation expression sb1.equals(sb2) produces the value false.  
5Prints: false,false  StringBuffer.equals does not override Object.equals. The StringBuffer.equals method compares the reference values--not the contents of the StringBuffer instances. The expressions sb1==sb2 and sb1.equals(sb2) produce the same results.  
6Prints: true,false  The StringBuffer class does not override the equals and hashCode methods of the Object class. The Object.equals method does not return the value true unless the argument is a reference to the same object on which the method is invoked. For example, the method invocation expression obj1.equals(obj2) only produces the value true when obj1 == obj2 is also true. The Object.hashCode method tends to return distinct hashcode values for distinct objects regardless of the internal contents of the object. Suppose that the reference variables sb1 and sb2 are of type StringBuffer. The expression sb1.hashCode() == sb2.hashCode() will not produce the value true unless the expression sb1 == sb2 is also true. The String class does override the equals and hashCode methods of the Object class. The String.hashCode method returns a hashcode value that is computed based on the contents of the String object. Suppose that the reference variables s1 and s2 are of type String. The expression s1.hashCode() == s2.hashCode() must produce the value true anytime the method invocation expression s1.equals(s2) produces the value true.  

Copyright © 2002-2003, Dan Chisholm
All rights reserved.