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  There are two versions of the Math.round method. One accepts an argument of type float; the return type is int. The other accepts an argument of type double; the return type is long. A compile-time error is generated here due to the attempt to assign the long return value to the int variable, i2.  
2Compile-time error at 3  The Byte class has only two constructors: one accepts a primitive byte; the other accepts a String. The argument that appears in the class instance creation expression new Byte(1) is of type int, but the compiler will not implicitly narrow the int to a byte. At line 1, the assignment expression primitiveByte = 1 causes the compiler to implicitly narrow a literal of type int to type byte. The compiler will implicitly do a narrowing conversion for an assignment expression if the right hand operand is a compile-time constant of type byte, short, char or int and the value falls within the range of the variable on the left and the variable is of type byte, short, or char. For that reason, a constant int expression that is equal to 1 may be assigned to a byte without an explicit cast. The same is not true for the parameters of a method or constructor. The designers of the Java programming language felt that implicit narrowing conversions of method and constructor arguments would add unnecessary complexities to the process of resolving overloaded method calls.  
3Prints: 5  The Integer class has only two constructors: one accepts a primitive int, and the other accepts a String. If the argument is of type byte, short or char, then it is implicitly promoted to type int.  
4Compile-time error  Long has two constructors: one accepts an argument of type primitive long; the other accepts an argument of type String. The class instance creation expression new Long(primitiveFloat) generates a compile-time error, because the compiler will not implicitly apply a primitive narrowing conversion to the argument.  
5Prints: 4.0  The Double constructor is overloaded: one accepts an argument of type primitive double; the other accepts an argument of type String.  
6None of the above  The Float constructor is overloaded: one version accepts a primitive of type float; one accepts a primitive of type double; one accepts a String representation of a floating-point literal.  
7c  d  The boolean literals true and false are written with lower case letters; upper case letters produce a compile-time error. A String representation of a boolean literal is acceptable in both upper and lower case letters.  
8a  b  c  d  new Short(1)  new Short('1')  new Short('b' - 'a')  new Short((short)1 - (short)2)  The Short class has only two constructors: one accepts a primitive short; the other accepts a String. The argument that appears in the class instance creation expression new Short(1) is of type int, but the compiler will not implicitly narrow the int to a short. The argument that appears in the class instance creation expression new Short('1') is of type char, but the compiler will not implicitly convert the char to a short. The argument that appears in the class instance creation expression new Short('b' - 'a') is of type int, but the compiler will not implicitly narrow the int to a short. The argument that appears in the class instance creation expression new Short((short)1 - (short)2) is of type int, but the compiler will not implicitly narrow the int to a short. If both operands of a binary arithmetic expression are of type byte, char or short; then both are implicitly widened to type int, and the result of the expression is of type int.  
9Prints: 1,2  The Math.round method returns the floor of the argument value plus 0.5. If the argument is of type float, then the return type is int. If the argument is of type double, then the return type is long.  
10b  c  e  Compile-time error at 1  Compile-time error at 2  Compile-time error at 4  The Byte class has only two constructors: one accepts a primitive byte; the other accepts a String. The argument that appears in the class instance creation expression new Byte(1) is of type int, but the compiler will not implicitly narrow it to type byte. The argument that appears in the class instance creation expression new Byte('2') is of type char, but the compiler will not implicitly narrow it to type byte. At line 4, the result of the additive expression is of type int, but the variable is of type byte. The assignment expression generates a compile-time error.  
11Compile-time error  The Integer class has only two constructors: one accepts a primitive int, and the other accepts a String. The class instance creation expression new Integer(primitiveLong) generates a compile-time error, because the compiler will not apply an implicit narrowing conversion to the argument. For the same reason, the class instance creation expression new Integer(primitiveFloat) generates a compile-time error.  
12Compile-time error  Long has two constructors: one accepts an argument of type primitive long; the other accepts an argument of type String. The class instance creation expression new Long(i1) generates a compile-time error, because there is no constructor that accepts a reference to an instance of type Long.  
13Compile-time error  The Double constructor is overloaded: one accepts an argument of type primitive double; the other accepts an argument of type String. There is no constructor that accepts a reference to an instance of type Double.  
14Prints: 1.0,8.0,16.0,1.0,1.0,1.0  The Float constructor is overloaded: one version accepts a primitive of type float; one accepts a primitive of type double; one accepts a String representation of a floating-point literal. All numeric values can be promoted to type double; so all numeric values are accepted by the float constructor.  
15Prints: false,true,true  Four instances of type Boolean containing the value true are created. The equality expression b1==b2 evaluates to false, because the unique instances of type Boolean have different reference values. The instance method Boolean.booleanValue returns a copy of the primitive boolean value that is contained by the instance. Since the boolean values of b1 and b2 are the same, the result of the equality expression b1.booleanValue()==b2.booleanValue() is true. The equals method compares the values of the primitives that are wrapped by the Boolean instances. Since the wrapped primitive values are the same, the result of the method invocation expression b3.equals(b4) is true.  
16None of the above  None of the String arguments would generate a compile-time error, but two would generate a run-time error. The argument 1.0 would generate a run-time error, because a floating-point value is not acceptable. The argument 0x1 would generate a run-time error, because a hexadecimal integer literal is not acceptable. The argument must be a decimal integer literal. The leading 0 of the argument 011 is ignored and the argument is interpreted as a decimal integer literal.  
17Prints: false,false  String instances are immutable. String methods such as String.toLowerCase and String.replace create and return a new String instance. The instance on which the method has been invoked remains unchanged. In the program, the equals method is invoked on the original instances of s1 and s2--not the new instances.  
18Prints: 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.  
19Prints: BDE  The substring method returns a new String instance that is a substring of the original String instance. The single parameter form of the substring method creates a new String that begins at the index specified by the argument value. The two parameter form creates a substring that starts at the index of the first parameter and ends at the index of the second parameter. The character at the start index is included in the substring, but the character at the end index is not.  
20Prints: 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.  
 
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.