Dan Chisholm's
Java Programmer Certification Mock Exam

Please Help Save a Tree!

The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.

Today, you can find my book at amazon.com.

Answers: Java Programmer Certification Mock Exam
No.AnswerRemark
1Compile-time error at 1  At line 1, the invocation of the Math.min method produces a return value of type int. The variable b1 is of type short; so the assignment expression results in a compile-time error. The Math class declares four versions of the min 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.  
2Compile-time error  The Byte.parseByte method returns a primitive byte. A compile-time error is generated as a result of the attempt to assign a primitive byte to a Byte reference variable.  
3Prints: true,true,false  Integer.equals overrides Object.equals. The Integer.equals method compares the data values contained in the Integer instances. If the argument is of type Integer and if the value contained in the argument is the same as the value contained in the instance on which the method is invoked, then the result is true. The equality operator, ==, does not compare data values. Instead, the equality operator compares references. Distinct instances of any two objects can not have the same reference value; so the expression new Integer(i1) == new Integer(i1) is false.  
4b  d  e  Long.parseLong("1L")  Long.parseLong("0x10")  Long.parseLong("1.0")  Long.parseLong is overloaded: one version accepts a String argument that represents an integral value; the other accepts both a String argument and an argument of type int. The int argument represents the radix (i.e. base) of the String argument. The Long.parseLong method is not able to determine the type of the String value by examing a suffix such as L. Any such suffix results in a run-time error. The Long.parseLong method is not able to determine the radix of the String value by examing a prefix such as 0 or 0x. The 0 prefix used to identify octal values is accepted, but the String is parsed as a decimal value. The prefix 0x generates a run-time error. The Long.parseLong method generates a run-time error if the String argument is not formatted as a decimal integer. A floating-point format results in a run-time error.  
5valueOf   
6Prints: A B C  Strings are immutable. No method will change the contents of a String instance. Some String methods such as concat, replace, substring and trim will return a new String instance with the desired modifications. In this case, the String instances returned by the methods trim and concat are ignored.  
7Compile-time error at 1  At line 1, the invocation of the Math.min method produces a return value of type int. The variable a1 is of type byte; so the assignment expression results in a compile-time error. The Math class declares four versions of the min 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.  
8Prints: true,false,false  Integer.equals overrides Object.equals. The Integer.equals method compares the data values contained in the Integer instances. If the argument is of type Integer and if the value contained in the argument is the same as the value contained in the instance on which the method is invoked, then the result is true. If the argument is not of type Integer then the result is false.  
9b  d  e  Long.parseLong("+1")  Long.parseLong("1L")  Long.parseLong("1.0")  Long.parseLong is overloaded: one version accepts a String argument that represents an integral value; the other accepts both a String argument and an argument of type int. The int argument represents the radix (i.e. base) of the decimal integral value represented by the String argument. The Long.parseLong method is not able to determine the type of the String value by examing a suffix such as L. Any such suffix results in a run-time error. The Long.parseLong method is not able to determine the radix of the String value by examing a prefix such as 0 or 0x. The 0 prefix used to identify octal values is accepted, but the String is parsed as a decimal value. The prefix 0x generates a run-time error. A leading minus sign (-) can be added to indicate a negative value. A leading plus sign (+) results in a run-time error. The Long.parseLong method generates a run-time error if the String argument is not formatted as a decimal integer. A floating-point format results in a run-time error.  
10e  f  parseDouble  valueOf   
11Compile-time error  The StringBuffer class has an append method, but the String class does not. A compile-time error is generated due to the attempt to invoke the append method on an instance of type String.  
12Prints: int,int,int  The Math class declares four versions of the min 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.  
13a  b  intValue  parseInt  Integer.valueOf returns an instance of the Integer wrapper class.  
14e  g  parseDouble  valueOf   
15Prints: int,int,long  The Math class declares four versions of the min 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.  
16Prints: true,false  Integer.hashCode overrides Object.hashCode. The Integer.hashCode method calculates the hash code based on the value contained in the Integer instance. The StringBuffer.hashCode method does not override Object.hashCode. 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.  
17Run-time error  An argument of type String must represent a floating-point value. The argument "0x10" represents a hexadecimal integer literal; so a NumberFormatException would be thrown at run-time.  
18Compile-time error  The StringBuffer class has an insert method, but the String class does not. A compile-time error is generated due to the attempt to invoke the insert method on an instance of type String.  
19Prints: false,false,true  At run-time, the expression a+b is evaluated four times. Each evaluation produces a new String instance containing the value "AB". Each of the four instances has a unique reference. If any two of the four instances appear as the operands of the equality operator, then the result is always false. The left and right operands of the equality expression (a+b)==(a+b) reference unique String instances. Since the two references are not the same, the equality expression produces the value false. Similarly, the expression c==d produces the value false. Since the contents of the String instances referenced by c and d are the same, the method invocation expression c.equals(d) produces the value true.  
 
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
 

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