Dan Chisholm
Exam 10
Answers
Answers: Certified Java Programmer Mock Exam Answer Remark
No.
b e 1 g goto new finally const do h k The System.out.print method prints the word null if the argument is a String reference that is null. The abstract modifier can be applied to a class and a method but not a field. A field is sometimes shared between threads. The volatile modifier is used to force threads to reconcile their own working copy of a field with the master 3 7 copy in main memory. If a field is declared final then its value does not change and there is no need for threads to reconcile their own working copies of the variable with the master copy in main memory. The modifiers, synchronized, native and strictfp, specify implementation details that the programmer is free to change in a subclass. Similarly, a subclass can If an accessible superclass method is override a concrete implementation of a static, then any method with the same method with an abstract method signature in a subclass must also be declaration. A subclass method may not static. If a superclass method is public, have weaker access than the overridden then the overriding method must also be superclass method. For example, a public public. If a superclass method is method may not be overridden by a protected, then the overriding method private method. However, a subclass must be protected or public. method can provide greater access than a superclass method. For example, a protected method can be overridden by a public method. 4 The modifier, protected, can not be applied to a top level class, but can be applied to a nested class. The synchronized modifier can not be applied to any class, because it is a method modifier. The modifier, volatile, can not
2 b Prints: null
3
c g
a 4 c e
5 d
Page: 1
Dan Chisholm
No.
6 a
7 b
8
a b
b 9 d f 10 d 11 c d
Exam 10
Answers
Answers: Certified Java Programmer Mock Exam Answer Remark be applied to any class; because it is a field modifier. The following modifiers can be applied to a member class: abstract, private, protected, public, static and final. The synchronized modifier can not be applied 1 to any class, because it is a method modifier. The modifiers, transient and volatile, can not be applied to any class, because they are field modifiers. The switch statement does not throw an exception; so the switch completes normally. The subsequent statement Prints: 1,0,1,0,0,1 increments the variable, a; and the try block completes normally. Both of the finally blocks are then executed. The modifier, abstract, is applicable to an interface declaration, but its use is strongly discouraged; because every interface is implicitly abstract. The modifier, static, is applicable to a abstract public member interface, but not to an interface that is not nested. The modifier, synchronized, is applicable only to concrete implementations of methods. The modifiers, transient and volatile, are applicable only to variables. A char is a 16 bit unsigned value; so 0x0000 to 0xffff 0 to 0177777 0 to none of the char values are negative and 65535 the minimum value is zero. The maximum value is 216. The sign of an integral numeric type is Prints: true,true changed by inverting all of the bits and by adding one. 3 4 This question demonstrates a variety of assignment conversions. The compiler will implicitly do a narrowing conversion for an assignment statement 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 if the variable Page: 2
Dan Chisholm
No.
12 c
13 d
14 a
15 c
16 g
Exam 10
Answers
Answers: Certified Java Programmer Mock Exam Answer Remark on the left is of type byte, short, or char. In this case, variables s1 and c1 are not compile time constants so the compiler will not do an implicit narrowing conversion. However, variables s2 and c2 are compile time constants that fall within the range of the left hand operand. For more information, please see JLS section 5.2. A class is not tightly encapsulated if the The internal data model can be read and internal data model can be read and/or modified only through accessor and modified without working through mutator methods. accessor (i.e. get) and mutator (i.e. set) methods. The methods Object.wait, Thread.join and Thread.sleep name InterruptedException InterruptedException in their throws clauses. The Math.sqrt method calculates the square root of the argument. The Prints: true Math.exp method raises e to the power specified by the argument. Math.E is the value of e. The Byte.parseByte method returns a primitive byte. A compile-time error is Compile-time error generated as a result of the attempt to assign a primitive byte to a Byte reference variable. Prints: 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
Page: 3
Dan Chisholm
No.
b 17 d e
18 g
19
c e
20 b
Exam 10
Answers
Answers: Certified Java Programmer Mock Exam Answer Remark Integer(i1) is false. 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 Long.parseLong("1L") run-time error. The Long.parseLong Long.parseLong("0x10") method is not able to determine the radix Long.parseLong("1.0") 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 runtime error if the String argument is not formatted as a decimal integer. A floating-point format results in a runtime error. valueOf Local method variables and method parameters are stored on the stack and go out of scope after the method is exited. Although a local reference variable is stored on the stack, the referenced object is stored on the heap; so the object can continue to exist long after the method 3 5 runs to completion. An object that is instantiated within a method or block is not permitted to refer to a variable that is declared within the method or block unless the variable is declared final and the variable declaration precedes the creation of the object. Prints: 1433 On the first pass through the loop, the switch expression, x, has the value, -5. None of the case constants are matched, so the statement following the default
Page: 4
Dan Chisholm
No.
21
d e
22 a
23 f
24 d
Exam 10
Answers
Answers: Certified Java Programmer Mock Exam Answer Remark label is executed causing the value of x to be set to -3. On the second pass, the default case of the switch statement is executed again, and two is again added to the value of x. The new value is -1. On the third pass, the value of x is again incremented, and the new value is +1. After that, the value of x is printed on each pass through the loop. For the last two passes, the value of x is 3. If the default label of a switch statement should not be reached under normal operating circumstances, then the default case becomes a good candidate for the use of an assert statement. If a method is declared with a non-void return type and In this code example a throw statement if no return statement appears after the must be used in place of the assert switch statement, then each case of the statement. Compile-time error switch must have a return statement or a throw statement. The throw statement is used rather than an assert, because the compiler knows that the assert statement is not functional when assertions are disabled. Method m1 sets all elements of the array to null; so the objects created on lines 2, 1 3 and 4 are eligible for garbage collection when method m1 returns. Anytime a field is accessed from within a static context it is very important to Compile-time error at line 4. verify that the field is also static. If the field is instead an instance variable then the result is a Compile-time error. Prints: GFC206,GFC206 Type GFC207 is a subclass of types GFC206 and GFC205, so any of the four methods are applicable to the method invocation expression, m(gfc207, gfc207). The most specific of the four, m(GFC206 x, GFC206 y), is chosen. Type GFC206 is a subclass of type GFC205, and method m(GFC206 x, GFC206 y) is more specific than the
Page: 5
Dan Chisholm
No.
25 a
26 d
27 d
28 b
Exam 10
Answers
Answers: Certified Java Programmer Mock Exam Answer Remark other three, because any invocation of m(GFC206 x, GFC206 y) could also be handled by any of the other three without causing a compile-time type error. Suppose that a class extends a superclass, X, or implements an interface, X. The field access expression Prints: ABCI ((X)this).hiddenField is used to access the hidden field, hiddenField, that is accessible within the superclass or interface, X. The qualified this expression ClassName.this.name can be used to G.this.s1 access shadowed variables declared within an enclosing class. The declarations of b1 and c1 cause compile-time errors, because a reference Compile-time error of a subclass type can not refer to an instance of the superclass type. At run-time, the expression a+b is evaluated three times. Each evaluation produces a new String instance containing the value "AB". Each of the three instances has a unique reference. If any two of the three instances appear as the operands of the equality operator, then the result is always false. The expression "A"+"B" is a compile-time Prints: false,true constant; the value is "AB". At run-time, the String constant "AB" is held in the String pool. When the intern method is invoked on the instance referenced by variable c, a reference to the existing String contant "AB" is returned. The left and right operands of the equality expression c.intern()==("A"+"B") refer to the same instance; so the result is true.
29 d Prints: 1,2,3,4,5,3
The expression can be simplified as follows: j = 1 + ((2 * 3) % 4) + 5 = 8. The original expression is as follows: j = ++i + ++i * ++i % ++i + ++i. Step one.
Page: 6
Dan Chisholm
Exam 10
Answers
Answers: Certified Java Programmer Mock Exam No. Answer Remark Evaluate the unary expressions from left to right: j = 1 + 2 * 3 % 4 + 5. Step two. Add parentheses to indicate operator precedence: j = 1 + ((2 * 3) % 4) + 5. Step three. Evaluate the inner most parentheses: j = 1 + (6 % 4) + 5. Repeat step three: j = 1 + 2 + 5. Repeat step three: j = 8. The argument of the print expression is: j%5. The result is: 8 % 5 = 3. Inside of method m2, the local variables i1 and i2 remain unchanged while the 30 d Prints: 3,1 shadowed instance variables are changed.
Page: 7