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 toner, and save money. If you prefer to work my exams from printed pages, then you can save a tree, save your printer toner, and save money if you buy my new book instead of attempting to print the pages of my web site.

Today, you can find my book on the retail web site of the company that prints it and distributes it.

Send an email to me.

Answers: Java Programmer Certification Mock Exam
No.AnswerRemark
1Prints: 0  If NaN is the argument passed to Math.round, then the return value is zero. If the argument type is float, then the return type is int. If the argument type is double, then the return type is long.  
2Compile-time error  A compile-time error is generated, because the Byte class does not have a charValue method. The Byte class extends the Number class and implements all six of the methods declared in Number.  
3Compile-time error  The Integer class has only two constructors: one accepts a primitive int, and the other accepts a String. There is no constructor that accepts an argument that is an instance of type Integer.  
4Compile-time error  Long is a subclass of the abstract class Number, and Long implements all of the methods of Number: byteValue, shortValue, intValue, longValue, floatValue and doubleValue. The attempt to invoke the charValue method on an instance of Long generates a compile-time error, because there is no charValue method.  
5Prints: -1,-1,true  Double is a subclass of the abstract class Number, and implements all of the methods of Number such as byteValue, shortValue, floatValue, etc. In this case, the Double instance contains the value 0xFFFF. When that value is converted to type byte the result is 0xFF which is also the two's complement representation of the byte value -1. Similarly, 0xFFFF is the two's complement representation of the short value -1. Please note there is no Double.charValue method.  
6b  d  f  new Float("A")  new Float("1L")  new Float("0x10")  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. The primitive char literal 'A' is converted to a float, and is accepted by the constructor that declares a parameter of type float. The String literals "NaN" and "Infinity" are accepted by the Float constructor. A sign (+ or -) is optional. The API specification states that any other String must represent a floating-point value; however, a little experimentation proves that a String is acceptable if it can be parsed as a decimal integer value. The leading 0 of an octal value is ignored, and the String is parsed as a decimal value. A String representation of a hexadecimal value is not acceptable. The String "A" does not represent a floating-point literal value; therefore, a NumberFormatException is thrown. Arguments of type String can not contain an integer type suffix, L or l. A floating-point suffix, F, f, D or d, is acceptable, but the suffix has no impact on the result.  
7Prints: true,true,true  The Boolean class contains two public static final Boolean instances: Boolean.FALSE wraps the primitive boolean value false; Boolean.TRUE wraps the primitive boolean value true. Depending on the value of the argument, the Boolean.valueOf method returns a reference to either Boolean.FALSE or Boolean.TRUE. Reference variables b1 and b2 are both initialized with a reference value returned by the method invocation expression Boolean.valueOf(true); so the equality expression b1==b2 is true. Please note that the valueOf method that accepts an argument of type primitive boolean was introduced in the 1.4 version of Java.  
8c  d  e  new Short("+1")  new Short("1.0")  new Short("0x1")  The Short class has only two constructors: one accepts a primitive short; the other accepts a String. A String argument must represent an integral primitive type. A leading minus sign can be added to indicate a negative value. A leading plus sign generates a run-time error. The constructor 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 run-time error is generated if the String argument is not formatted as a decimal integer. A floating-point format results in a run-time error.  
9a  c  d  append  delete  insert  The StringBuffer class has methods named append, delete and insert, but the String class does not. A typical trick question will attempt to invoke StringBuffer methods on a String instance.  
10Prints: 1.0,2.0  The Math.ceil method name is not overloaded. The Math.ceil method accepts an argument of type double and returns a double. The returned value is the smallest whole number that is greater than or equal to the argument.  
11Prints: false,true  The expression b1==b2 compares the references of two instances of Byte. The result is false, because the instances are distinct. The expression b1.equals(b2) compares the contents of two instances of Byte. The result is true, because the two instances contain the same value.  
12None of the above  The binary representation of 256 is one bit that is set to one followed by eight bits that are set to zero. When 256 is converted to an eight bit byte value, the bit that is set to one is lost and only the bits that are set to zero remain. When 256 is converted to a short, no information is lost; so the value remains 256.  
13Prints: long  The Long.parseLong method returns a primitive long.  
14Prints: true,true,true  NaN is the only value that is not equal to itself. The Double.isNaN method returns the result of the expression (v != v).  
15Compile-time error at 1  The Float.toString method is overloaded: one declares no parameters and returns the value wrapped by the Float instance; the other accepts a primitive of type float. The literal, 1.0, is of type double and can not be implicitly narrowed to type float.  
16Prints: true,false,false  The Boolean constructor 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 reference is null, then the new instance will contain the value false.  
17false,true,true  The equality expression s1==s2 compares the reference values of two distinct instances of type Short. Since the instance are distinct, the equality expression is false. The expression s1.equals(s2) compares the values of two instances of type Short. Since both instances contain the value 1, the returned value is true. The expression a1==b1 compares the hash codes of two instances of Short. The result is true, because the two instances contain the same value.  
18Compile-time error  A compile-time error is generated due to the attempt to access the length method of the String class as though it were a variable.  
19Prints: 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.  
20Prints: 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.  
 
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.