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
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1Compile-time error  The access modifiers public, protected and private, can not be applied to variables declared inside methods.  
2Prints: 0  Member variables are initialized automatically. Type int variables are initialized to zero.  
3Prints: 1, 1, 0, 1  Both instances of class Red share a single copy of the static field b. Although field b is only incremented using the r1 reference, the change is visible in the r2 instance of class Red.  
4Compile-time error  A static method can not access a non-static variable.  
5a  b  e  A final method can not be overridden.  All methods declared in a final class are implicitly finalA machine-code generator can inline the body of a final method.  All methods declared in a final class are implicitly final. It is permissible--but not required--that all such methods be explicitly declared final. All private methods are implicitly final. It is permissible--but not required--that all private methods be explicitly 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 method.  
6 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 or package access, but not private. The transient modifier can not be applied to any class; because it is a field modifier.  
7c  g  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 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.  
8a  c  e  If an accessible superclass method is static, then any method with the same signature in a subclass must also be staticIf a superclass method is public, then the overriding method must also be publicIf a superclass method is protected, then the overriding method must be protected or public The signature of a method is the name of the method and the number and types of the method parameters. The return type is not part of the method signature. An instance method declared in a subclass overrides an accessible superclass method with the same signature. A static method of a subclass hides--but does not override--an accessible superclass method with the same signature. A compile-time error is generated if a subclass contains a declaration of an instance method that shares the same signature as an accessible static method of the superclass. The modifiers, synchronized, native and strictfp, specify implementation details that the programmer is free to change in a subclass. Similarly, a subclass can override a concrete implementation of a method with an abstract method declaration. A subclass method may not have weaker access than the overridden superclass method. For example, a public method may not be overridden by a private method. However, a subclass method can provide greater access than a superclass method. For example, a protected method can be overridden by a public method.  
9 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 be applied to any class; because it is a field modifier.  
10 The synchronized modifier is a method modifier and can not be applied to a field.  
11Compile-time error  The array initializer, {{1;2};{3;4;5};{6;7;8;9}} generates a compile-time error, because the commas have been replaced by semicolons. The array initializer should have been specified as follows: {{1,2},{3,4,5},{6,7,8,9}}.  
12Compile-time error  The array initializer, ((1,2),(3,4,5),(6,7,8,9)), generates a compile-time error, because the curly braces have been replaced by parentheses. The array initializer should have been specified as follows: {{1,2},{3,4,5},{6,7,8,9}}.  
13Compile-time error  The array initializer, [[1,2],[3,4,5],[6,7,8,9]], generates a compile-time error, because the curly braces have been replaced by square brackets. The array initializer should have been specified as follows: {{1,2},{3,4,5},{6,7,8,9}}.  
14Prints: 2,5,9  The line marked 1 declares three array variables, a2, a3 and a4, and each references an array with components of type int. The line marked 2 declares an array variable, a1, where each component is initialized with a reference to one of the previously created arrays. The array access expression, a1[0][1], is evaluated as follows: a1[0][1] = a1[first subarray][second component] = 2.  
15 A compile-time error occurs at the line marked 1, because the array variable declaration can not be used to specify the number of components contained in the array. Instead, the dimension expression should be contained in an array creation expression such as the following, new int[3]
16 An array variable is declared by placing brackets after the identifier or after the type name. A compile-time error occurs at the line marked 2, because the brackets appearing before the identifier for array variable a4 are not associated with the type or the identifier. 
17Prints: 147258369  The array variable a1 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 had been 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. 
18Prints: A11  The declaration A11[] a1 = new A11[1] declares a variable a1 that references an array that contains one component of type A11. The declaration A11[][] a2 = new A11[2][] declares a variable a2 that references an array that contains two components of type A11[]. In other words, the array referenced by a2 contains two reference variables, and each is able to reference a subarray. The initial value of the subarray references is null. The size of the subarrays has not been specified. The declaration A11[][][] a3 = new A11[3][][] declares a variable a3 that references an array that contains three components of type A11[][]. In other words, the array referenced by a3 contains three reference variables, and each is able to reference a subarray. The initial value of each subarray reference is null. The dimensions of the subarrays have not been specified. At line 5, a reference to the array referenced by a1 is assigned to each of the two components of the array referenced by a2, a2[0] = a2[1] = a1. At line 6, a reference to the array referenced by a2 is assigned to each of the three components of the array referenced by a3, a3[0] = a3[1] = a3[2] = a2. In other words, after line 6, each component of the array referenced by a3 is a reference to the array referenced by a2, and each element of the array referenced by a2 is a reference to the array referenced by a1, and the array referenced by a1 contains a reference to an instance of class A11. Every element of the multi-dimensional array referenced by a3 contains a reference to a single instance of class A11. The print method invokes the toString method on the instance, and produces the output, A11
19a  k  Prints: null  Run-time error  The declaration A13[] a1 = new A13[1] declares a variable a1 that references an array that contains one component of type A13. The declaration A13[][] a2 = new A13[2][1] declares a variable a2 that references an array that contains two components of type A13[]. In other words, the array referenced by a2 contains two references to two subarrays of type A13[]. The size of each subarray is 1. The declaration A13[][][] a3 = new A13[3][3][3] declares a variable a3 that references an array that contains three components of type A13[][]. In other words, the array referenced by a3 contains three references to three subarrays of type A13[][]. The dimensions of the subarrays are 3 X 3. The dimensions of the array referenced by a3 are 3 X 3 X 3. The number of elements in the array referenced by a3 is 3 * 3 * 3 = 27 elements. Each of the three subarrays contains three components, where each is a reference to a subarray of type A13[]. Each of the 27 elements of the array referenced by a3 is a reference of type A13 that has been initialized to the default value of null. At line 7, a reference to the array referenced by a2 is assigned to each of the three components of the array referenced by a3, a3[0] = a3[1] = a3[2] = a2. Since the array referenced by a2 has dimensions 2 X 1, the array referenced by a3 now has dimensions 3 X 2 X 1. The number of elements is 6. An attempt to access any element beyond those 6 results in an exception at run-time. 

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