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
1None of the above.  Strings are immutable. No method of the of a String class will change the contents of a String instance. Some String methods such as concat, replace, substring and trim will return a new String with the desired modifications. The StringBuffer methods append, delete and insert are not members of the java.lang.String class. A typical trick question will attempt to invoke StringBuffer methods on a String instance.  
2Prints: 0.0,1.0  The Math.floor method name is not overloaded. The Math.floor method accepts an argument of type double and returns a double. The returned value is the largest whole number that is smaller than or equal to the argument.  
3Prints: 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.  
4Prints: true,true  The expression b1.equals(b2) compares the values of two instances of type Byte. Since both instances contain the value 1, the return value is true. The expression a==b compares the hash codes of two instances of Byte. The result is true, because the two instances contain the same value.  
5Prints: int primitive  The Integer.parseInt method returns a primitive of type int.  
6Run-time error  The Long.parseLong method accepts a String parameter that represents a numeric value. Long.parseLong assumes that the input String represents a decimal value unless a second parameter is provided to specify the radix. All of the characters in the String must be digits of the specified radix. The parseLong method does not determine the type of the numeric value based on a suffix such as l, L, f, F, d, or D. Use of any such suffix generates a NumberFormatException at run-time.  
7e  f  g  parseDouble  toString(double)  valueOf   
8Compile-time error  The Float.floatValue method is not static; it must be invoked on an instance of type Float.  
9Prints: false,true,false  The Boolean.valueOf method is overloaded: one version accepts a primitive boolean argument; the other accepts a String. If the String value is the word true, then the new Boolean instance will contain the value true. Both upper and lower case letters are acceptable. If the String contains any word other than true or if the String reference is null, then the new instance will contain the value false.  
10Prints: p,p,S  The Short.shortValue method and the Short.parseShort method both return primitives of type short. The Short.valueOf method returns an instance of type Short.  
11valueOf  The valueOf method is static. It returns a String representation of the argument. The StringBuffer methods append, delete and insert are not members of the java.lang.String class. A typical trick question will attempt to invoke StringBuffer methods on a String instance.  
12Prints: 10.0  The Math class declares four versions of the abs method; each declares a pair of parameters of the same type. The parameter pair can be of type int, long, float or double. The return type is the same as the argument types. At run-time, the arguments might not match the declared parameter types; so one argument or both might require an implicit conversion to an acceptable type. If both arguments are of type byte, short or char, then both will be promoted to type int. If only one argument is of type byte, short or char, then it will be promoted to the type of the other argument. If both arguments are of type int, long, float or double but the types differ, then a primitive widening conversion will be applied to one of the two arguments.  
13Prints: 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.  
14Prints: -128,127   
15Prints: true,true  An int is a 32-bit signed integral value that is stored in two's complement format. The left most bit is the sign bit. The sign bit is set to one for negative numbers and is set to zero for positive numbers.  
16Prints: true,false  Long.equals overrides Object.equals. The Long.equals method compares the data values contained in the Long instances. The StringBuffer.equals method does not override Object.equals. The StringBuffer.equals method compares the reference values of the instances. Two distinct instances of StringBuffer will not have the same reference values; so the equals method returns false. The StringBuffer.hashCode method typically returns a value that is based on the internal address of the StringBuffer instance. Two instances of StringBuffer will not have the same hash code.  
17a  b  c  d  e  doubleValue  floatValue  intValue  longValue  parseDouble   
18Prints: ffF   
19Prints: BbB  The Boolean.valueOf method returns a Boolean instance. The Boolean.booleanValue method returns a primitive. The Boolean.TRUE field is a reference to an instance of type Boolean that wraps the primitive value true.  
20Prints: A B C  The String instance referenced by s2 is passed to the m1 method by passing the value of the reference. The reference value used in method m1 is a local copy of the reference. If the local copy used in method m1 is changed, then the original reference variable in the main method remains unchanged.  

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