| No. | Answer | Remark | |
|---|---|---|---|
| 1 | d | Compile-time error at 3. | Both class A and B are declared in the same package, so class B has access to the public, protected, and package access methods of class A. |
| 2 | a e | 1 5 | An array creation expression must have either a dimension expression or an initializer. If both are present, then a compile-time error is generated. Similarly, if neither is present, then a compile-time error is generated. If only the dimension expression is present, then an array with the specified dimension is created with all elements set to the default values. If only the initializer is present, then an array will be created that has the required dimensions to accommodate the values specified in the initializer. Java avoids the possibility of an incompatible dimension expression and initializer by not allowing both to appear in the same array creation expression. A compile-time error is generated by the array creation expression for a1, because it needs either a dimension expression or an initializer. A compile-time error is generated at 5, because either the dimension expression or the initializer must be removed. |
| 3 | a | Prints: 3,4,8 | An array variable a1 is declared, and the declaration contains the initializer {{1,2,3},{4,5,6},{7,8,9,10}}. The initializer creates an array containing three components, and each is a reference to a subarray of type int[]. Each subarray contains components of type int, so the elements of the array referenced by a1 are of type int. The array access expression, a1[0][2] = a1[1st subarray][third component] = 3. |
| 4 | b c d e | final private protected public | The access modifiers, private, protected and public, can be applied to a field. A final field can not have its value assigned more than once. The abstract modifier may be applied to methods but not to fields. |
| 5 | e | volatile | An abstract method declaration provides no method body. If one method is declared abstract, then the entire class must be declared abstract and the class can not be instantiated. The access modifiers, private, protected and public, can be applied to a method. The field modifiers, transient and volatile, are not applicable to method declarations. |
| 6 | d | Compile-time error at 2 | There is a compile-time error at 2. The char type variable, c, is not a compile-time constant, so it can not be assigned to type byte without an explicit cast. The statement, "return c;", is a return statement with an expression, c. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of the method, m3, is byte. The return statement attempts to return the value of the char type variable, c. If a char value is a compile-time constant, and if the value falls within the range of type byte, then the char value is assignable to type byte. In method m3, variable c is not a compile-time constant, so the value of variable c is not assignable to type byte. While the declaration of method m3 produces a compile-time error, the declaration of method m1 does not; because the variable is a compile-time constant with a value, \u0001, that is assignable to type byte. |
| 7 | a d f | public abstract final | The access modifiers, protected and private, can be applied to a class that is a member of an enclosing class, but can not be applied to a local class or a class that is not nested inside another class. The static modifier can be applied to a class that is a member of an enclosing class, but can not be applied to a local class or a class that is not nested inside another class. The public modifier can be applied to a top level class to allow the class to be accessed from outside of the package. The abstract modifier prevents the class from being instantiated. An abstract class may include zero, one or more abstract methods. The final modifier prevents a class from being extended. |
| 8 | b d | The compiler attempts to create a default constructor for class B. Compile-time error at 2. | If no constructor is declared explicitly, then the compiler will implicitly create a default constructor that accepts no parameters, has no throws clause, and invokes its superclass constructor. Since class A has an explicitly declared constructor, the compiler will not create an implicit default constructor. Class B does not have an explicit constructor declaration, so the compiler attempts to create a default constructor. Since class A does not have a no-parameter constructor, the attempt by class B to invoke the no parameter constructor of A would fail. As a result, a compiler error is generated at marker 2. |
| 9 | a | private | Constructors are not inherited and can not be overridden, so there is no need for the final modifier in a constructor declaration. Furthermore, an abstract constructor would be useless, since it could never be implemented. The volatile modifier can be applied to a field, but not to a constructor. Native constructors are not permitted, because it would be difficult for Java to verify that the native constructor properly invokes the superclass constructor. |
| 10 | b | Prints: 0,0,0.0,null | Each array contains the default value for its type. The default value of a primitive byte or a primitive long is printed as 0. The default value of a float primitive is printed as 0.0. The default value of an Object is null and is printed as null. |
| 11 | e | Prints: 2,5,9 | The declarations of the array variables, a1, a2 and a3, are different in terms of the position of the square brackets, but each is of type int[][]. The array access expression, a1[0][1] = a[1st subarray][2nd component] = 2. Each of the three array variable declarations has an array initializer. The same initializer, {{1,2},{3,4,5},{6,7,8,9}}, is used in each of the three cases. The initializer specifies three components of type int[], so each component is a reference to an array object containing components of type int. The size of each of the subarrays is different: The size of the first is 2, the second is 3, and the third is 4. |
| 12 | a c d | static transient volatile | A transient field is not part of the persistent state of an object. Transient fields are not serialized. Fields that are shared between threads may be marked volatile to force each thread to reconcile its own working copy of the field with the master copy stored in the main memory. The synchronized modifier may be applied to methods but not to fields. |
| 13 | d | transient | A final method can not be overridden. A static method is associated with a class, but not a particular instance of the class. A thread can not enter a synchronized method without first acquiring a lock. The field modifiers, transient and volatile, are not applicable to method declarations. A native method is implemented in platform-dependent code. |
| 14 | d | Compile-time error at 2. | There is a compile-time error at 2. The short type variable, s, is not a compile-time constant, so it can not be assigned to type byte without an explicit cast. The statement, "return s;", is a return statement with an expression, s. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of the method, m4, is byte. The return statement attempts to return the value of the short type variable, s. If a short value is a compile-time constant, and if the value falls within the range of type byte, then the short value is assignable to type byte. In method m4, variable s is not a compile-time constant, so the value of variable s is not assignable to type byte. While the declaration of method m4 produces a compile-time error, the declaration of method m2 does not; because the variable is a compile-time constant with a value, 2, that is assignable to type byte. |
| 15 | b | A local class can be declared abstract. | An anonymous class can not be extended; therefore, an anonymous class can not be declared abstract. A local class can be abstract. An abstract class can not be instantiated. If a class declaration contains an abstract method, then the class must also be declared abstract. A class can be declared abstract even if it does not contain an abstract method. An abstract class can never be declared final. |
| 16 | d | Declare all constructors using the private access modifier. | If no constructors are declared explicitly; then the compiler will create one implicitly, and it will have the same access modifier as the class. The explicit declaration of any constructor will prevent the creation of a default constructor. If all constructors are declared private, then code outside of the class will not have access to the constructors and will not have the ability to create an instance of the class. Constructors do not return a value and constructor declarations do not include a return type, so the keyword void is not applicable to a constructor declaration. |
| 17 | a b | protected public | Constructors can not be inherited, so an abstract constructor would be useless, since it could never be implemented. A static constructor would also be useless--or nearly so--since it would be unable to access the non-static members of the new instance. An object is not available to multiple threads during the construction process, so the synchronized modifier would not provide additional protection. The transient modifier can be applied to a field, but not a constructor. |
| 18 | c d e | Compile-time error at 2. Compile-time error at 3. Compile-time error at 4. | Classes A and D are not declared in the same package, so class D does not have access to package access method, m4. Since class D does not extend class A, class D does not have access to the protected method, m2, of class A. |
| 19 | e | Prints: 2,5,8 | Arrays a1, a2 and a3 all contain 3 integers. The first element of the array has an index of 0 so an index of one refers to the second element of each array. |
| 20 | e | Prints: 2,3,5 | Each of the three array variable declarations, a1, a2 and a3, is different in terms of the position of the square brackets, but each declares a variable of type int[][]. Each of the three declarations contains an array initializer. In each case, the initializer could be simplified as follows: {{1,2},{3,4,5}}. The initializer creates an array containing two components, and each is a reference to an array containing components of type int. The size of each of the subarrays is different: The size of the first is 2 and the second is 3. The array access expression, a1[0][1] = a[1st subarray][2nd component] = 2. |