Sei sulla pagina 1di 6

Dan Chisholm Exam 8 Answers

Answers: Certified Java Programmer Mock Exam


No. Answer Remark
The access modifiers public, protected and private,
1 e Compile-time error can not be applied to variables declared inside
methods.
The data members can not be
directly manipulated by
a If a class A is not tightly encapsulated, then no
2 external code. The
d subclass of A is tightly encapsulated.
superclass is tightly
encapsulated.
a
3 f join sleep wait
k
The Math.floor method name is not overloaded. The
Math.floor method accepts an argument of type
4 a Prints: 0.0,1.0 double and returns a double. The returned value is
the largest whole number that is smaller than or
equal to the argument.
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
5 d Prints: true,true
expression a==b compares the hash codes of two
instances of Byte. The result is true, because the two
instances contain the same value.
The Integer.parseInt method returns a primitive of
6 a Prints: int primitive
type int.
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
7 g Run-time error
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.
e
parseDouble
8 f
toString(double) valueOf
g
9 c Prints: 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

Page: 1
Dan Chisholm Exam 8 Answers

Answers: Certified Java Programmer Mock Exam


No. Answer Remark
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.
The Float.floatValue method is not static; it must be
10 i Compile-time error
invoked on an instance of type Float.
The Short.shortValue method and the
Short.parseShort method both return primitives of
11 b Prints: p,p,S
type short. The Short.valueOf method returns an
instance of type Short.
The requirement to store key/value pairs is directly
satisfied by a concrete implementation of the Map
interface. The List and Set interfaces recognize
12 b TreeMap objects, but do not recognize keys and values.
TreeMap and TreeSet store elements in a sorted
order based on the key, but the TreeSet does not
support key/value pairs.
The 1.2 version of Java introduced the updated
13 h Prints: true,true,true
Vector class that implements the List interface.
If two objects are equal according to the equals
method, then the hashcodes must also be equal. If
two objects are not equal according to the equals
a (a.hashCode() == method, then the hashcodes may or may not be
14
d b.hashCode()) (!g.equals(h)) equal. If two objects have the same hashcode, then
the objects may or may not be equal. If two objects
have different hashcodes, then the objects must not
be equal.
15 a A final method can not be All methods declared in a final class are implicitly
b overridden. All methods final. It is permissible--but not required--that all such
e declared in a final class are methods be explicitly declared final. All private
implicitly final. A machine- methods are implicitly final. It is permissible--but
code generator can inline the not required--that all private methods be explicitly
body of a final method. declared final. The body of an inline method is
inserted directly into the code at the point where the
method is invoked. If the method is invoked at 10
different points in the code, then the body can be
copied to all 10 points. Inline code runs very quickly,
because it removes the need to do the work
associated with a method invocation. If the method
is executed repeatedly in a loop, then inlining can
improve performance significantly. The machine-
code generator has the option to inline a final

Page: 2
Dan Chisholm Exam 8 Answers

Answers: Certified Java Programmer Mock Exam


No. Answer Remark
method.
The modifiers, private and static, can be applied to a
nested class, but can not be applied to a class that is
not nested. A class that is not nested can have public
16 a 1
or package access, but not private. The transient
modifier can not be applied to any class; because it is
a field modifier.
The following modifiers can be applied to a member
17 f None of the above class: abstract, private, protected, public, static and
final.
The nested catch clause is able to catch a
Level2Exception or any subclass of it. The switch
statement throws an Exception that can not be
caught by the nested catch clause; so the nested
18 d Prints: 0,0,1,0,1,1
finally block is executed as control passes to the
second of the two outer catch clauses. The outer
finally block is executed as control passes out of the
try statement.
Base is the superclass of type Sub, so a reference to
an actual instance of type Base can not be cast to
type Sub. Therefore, a reference to an array instance
of type Base[] can not be cast to type Sub[]. The type
of the reference, obj, is Object. Type Sub[] is a
19 f Run-time error at line 3
subclass of Object. The compiler accepts the cast,
because the actual instance referenced at run-time
might be of type Sub[]. In this case, the actual type
of the instance referenced by obj at run-time is found
to be type Base[], so the result is a run-time error.
An array of primitive type can not be cast to an array
20 d 4
of a different primitive type.
21 d Compile-time error The type of the argument is null and could be
converted to any of the types Object, String or
GFC200, by method invocation conversion. All three
methods are applicable, but none of the three is more
specific than both of the other two. The ambiguity
results in a compile-time type error. If type GFC200
were a subclass of type String; then any argument
that could be pass to m(GFC200 x) could also be
passed to m(String x) without causing a compile-
time type error, and we could say that m(GFC200 x)
is more specific than m(String x). Since GFC200 is
not a subclass of type String, a method invocation

Page: 3
Dan Chisholm Exam 8 Answers

Answers: Certified Java Programmer Mock Exam


No. Answer Remark
conversion is not able to widen an argument of type
GFC200 to match a method parameter of type
String, so m(GFC200 x) is not more specific than
m(String x).
A private method of a superclass is not inherited by a
subclass. Even if a subclass method has the same
signature as a superclass method, the subclass
method does not override the superclass method.
Suppose a non-static method m1 is invoked using
the method invocation expression m1(). If m1 is a
private member of the class T where the invocation
expression occurs, then the implementation in class
T is selected at run-time regardless of the run-time
type of the object. If the non-static method m1 is not
private, then the selected implementation is
determined at run-time based on the run-time type of
the object. The program invokes the non-static
22 b Prints: R.printS1 S.printS2
method printS1S2 on an instance of class S, so the
run-time type is S. The body of method R.printS1S2
contains two method invocation expressions,
printS1() and printS2(). Since class R contains a
private implementation of the instance method
printS1, it is the implementation that is selected
regardless of the run-time type of the object. Since
printS2 is not private and not static, the selected
implementation of printS2 depends on the run-time
type of the object. The method printS1S2 is invoked
on an instance of class S; so the run-time type of the
object is S, and the implementation of printS2
declared in class S is selected.
Class Z is a static member class of class E. Static
member classes are similar to ordinary top-level
classes with the added advantage that all of the static
fields and methods of the enclosing class (including
those that are private) are available to the member
23 b Prints: Z class. The class instance creation expression new
E.Z() creates an instance of the static nested class
E.Z. The instance of the static nested class is not
associated with any instance of the enclosing class,
and no instance of the enclosing class is created. For
that reason, only the letter "Z" is printed.
24 a Prints: AAA The declared type of the reference variables, a1, b1
and c1, is the superclass type, A; so the three

Page: 4
Dan Chisholm Exam 8 Answers

Answers: Certified Java Programmer Mock Exam


No. Answer Remark
reference variables can be used to invoke only the
method m1(A a) that is declared in the superclass, A.
The methods that overload the method name m1 in
the subclasses, B and C, can not be invoked using a
reference variable of the superclass type, A. A
method invocation conversion promotes the
argument referenced by c4 from type C to type A,
and the method declared in class A is executed.
The expression a+b is evaluated at run-time and
produces a new instance of a String. Even though the
expression a+b produces the same result each time it
is evaluated, each evaluation produces a new
instance of the String. Since the equality operator
compares the references rather than the contents of
the objects, the equality operator returns the value
25 e Prints: true,false,false false. The declaration of variables c and d with the
final modifier does not transform the expressions
into compile-time constants. Instead, it just prevents
variables c and d from being changed after
initialization at run-time. However, if variables a and
b had been declared final then the results would be
different, because the compiler could evaluate the
expressions at compile time.
On the first pass through the loop, the value of x is 6,
so 5 is subtracted from x. On the second pass, the
value of x is 1, so 3 is added to x. On the third pass,
the value of x is 4, so 1 is subtracted from x. On the
fourth pass, the value of x is 3, so the variable,
26 c Prints: 61433
success, is incremented from zero to one. On the
final pass, the value of x is 3 and the variable,
success, is incremented to the value, 2. The boolean
expression of the do loop is now false, so control
passes out of the loop.
With assertions enabled it
If the default label of a switch statement should not
prints ABC followed by an
a be reached under normal operating circumstances,
27 AssertionError message.
d then the default label might be a good candidate for
With assertions disabled it
the use of an assert statement.
prints ABCE
Although the reference parameter i1 is reassigned
inside of m1, the change has no impact outside of
28 a Prints: 1,1
m1. Array references are passed by value: the
invoked method gets a copy of the array reference.

Page: 5
Dan Chisholm Exam 8 Answers

Answers: Certified Java Programmer Mock Exam


No. Answer Remark
The array is declared with the initializer,
{{1,2,3},{4,5,6},{7,8,9}}. The array access
expression, a1[0][1] = a1[first subarray][second
element] = 2. If the argument of the print statement
29 b Prints: 147258369 were a1[i][j] then the output would have been
123456789. The tricky feature of this question is the
reversal of i and j to produce the deceptive array
access expression, a1[j][i]. The output is
147258369.
The arguments that appear in the class instance
30 b Prints: 3122 creation expression of an anonymous class are
passed to a constructor of the superclass.

Page: 6

Potrebbero piacerti anche