| No. | Answer | Remark | |
|---|---|---|---|
| 1 | c d e | 3 4 5 | If a class C is declared as a member of an enclosing class then C may be declared using no access modifier or any of the three access modifiers, private, protected or public. However, if class C is not a local class, anonymous class or a member of an enclosing class or interface; then C may be declared with the public modifier or with package access (i.e. no modifier). The other two access modifiers, private and protected, are not applicable to any class that is not a member class. The class declaration, Class Basics4 {}, generates a compile-time error, because all of the letters of the reserved word class must be lower case. |
| 2 | a | 1 | Only one class in a source code file can be declared public. The other classes may not be public. Therefore, the declarations for classes Basics2, Basics3 and Basics4 generate compile-time errors. |
| 3 | d | transient | A transient field is not part of the persistent state of an object, so it is not serialized. A static field is also not part of the persistent state of an object, and also is not serialized. |
| 4 | a b e f g | final private static synchronized native | A final or private method can not be overridden, and can not be abstract. An abstract method declaration provides no implementation of the method, and all implementation details are left to the overriding method in the subclass. The synchronized modifier specifies an implementation detail that can be omitted from the declaration of an overriding method of a subclass, so it makes no sense to allow the use of the synchronized modifier in an abstract method declaration. |
| 5 | e | Compile-time error at 3. | There is a compile-time error at line 3. The long type variable, l, can not be assigned to type int without an explicit cast. The statement, "return l;", is a return statement with an expression, l. 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 int. The type of the variable, l, is long, so an explicit cast is needed to perform the narrowing primitive conversion, "return (int)l;". The declarations of methods m1 and m2 do not generate compile-time errors, because the types of the expressions contained in the return statements are assignable to type int. Widening conversions from types byte, char, or short to type int do not require an explicit cast. |
| 6 | a d e h | class A extends Object. The compiler will insert a default constructor implicitly. The default constructor has no throws clause. The default constructor invokes the no-parameter constructor of the superclass. | Field i1 and m1 both have package access. When no constructor is declared explicitly the compiler will insert one implicitly. The implicitly declared default constructor will have the same access privileges as the class. In this case, the class is public, so the default constructor is also public. The default constructor accepts no parameters and throws no exceptions. |
| 7 | a b c | The compiler will create a default constructor if no other constructor is declared. The default constructor takes no arguments. If a class A has a direct superclass, then the default constructor of class A invokes the no-argument constructor of the superclass. | If no constructor is declared explicitly, then the compiler will implicitly insert a default constructor. The default constructor takes no arguments. The primordial class Object has no superclass; so the default constructor of type Object does not invoke a superclass constructor. If a class A has a direct superclass, then the default constructor of class A will invoke the no-argument superclass constructor. It is unlikely that the real exam would try to trick you with a question that requires you to know that the constructor of type Object does not invoke a superclass constructor. For the purposes of the real exam, it might be safer to overlook that particular unique feature of type Object. If a subclass constructor attempts to invoke the no-argument superclass constructor when none exists, then a compile-time error is generated. The access modifier implicitly assigned to the default constructor is the same as that assigned to the class. The default constructor does not have a throws clause. Consequently, a compile-time error is generated if the no-argument constructor of the superclass has a throws clause. |
| 8 | d e | Compile-time error at 3. Compile-time error at 4. | Class A and C are not declared in the same package; therefore, class C does not have access to package access method, m4. Since class C extends class A, class C does have access to the protected method, m2, of class A. |
| 9 | a | 1 | A compile-time error occurs at the line marked 1, because the array reference declaration can not be used to declare the number of components contained in the array. Instead, the dimension expression should be contained in an array creation expression such as new int[5]. |
| 10 | a d e | A value can not be assigned to a final field more than once. A compile-time error is thrown if a blank final instance variable is not assigned a value before the end of each constructor. A field can not be declared both final and volatile. | Static and non-static field variables may be declared final. All final fields must be definitely assigned a value once and only once. If the declaration of a final variable does not include an initializer then the variable is called a blank final. All blank, final, static variables must be assigned in a static initializer. All blank final non-static variables must be assigned by the end of the instance construction process. 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. |
| 11 | e | None of the above. | An abstract method of a subclass can override by an abstract method of a superclass. The overriding abstract method declaration can be a good place to add comments. An abstract method of a subclass can override a concrete implementation of a method of a superclass. An abstract method declaration can have a throws clause. The body of an abstract method is a semicolon. |
| 12 | d | Compile-time error at 2. | There is a compile-time error at 2, because a narrowing primitive conversion from type float to type int requires an explicit cast. There is no compile-time error at 1, because widening primitive conversions from types byte, char, or short to type int do not require an explicit cast. |
| 13 | c | The no-argument superclass constructor must not have a throws clause that includes a checked exception. | The default constructor takes no arguments, and it invokes the superclass constructor with no arguments. If the superclass does not have an accessible no-argument constructor, then a compile-time error is generated. The default constructor does not have a throws clause. Consequently, a compile-time error is generated if the no-parameter constructor of the superclass has a throws clause. |
| 14 | a b c d e f | 1 2 3 4 5 6 | A variable that is local to a method can not be accessed from outside of the class, so the access modifiers are not useful and not legal. A variable that is local to a method can not be part of the persistent state of an object, so the transient modifier is not useful and not legal. Local variables can not be shared between threads, so the volatile modifier is not useful and not legal. A local variable can be declared final to prevent its value from being assigned more than once. If the value of the variable needs to be accessed from a local class or an anonymous class, then the local variable or method parameter must be declared final. |
| 15 | d | The keyword, super, may be used in the body of a static method. | The keyword, this, refers to the instance on which the method has been invoked. A static method--also known as a class method-- is not invoked on a particular instance of an object, but is instead invoked on the class. Since a static method is not associated with a particular instance, an attempt to use the keyword, this, within the body of a static method results in a Compile-time error. Similarly, the keyword, super, can not be used within the body of a static method. |
| 16 | d e f | Compile-time error at 2. Compile-time error at 3. Compile-time error at 4. | At line 2, the statement, "return i;", contains the expression, i. The enclosing method, m2, is declared void. The return statement generates a compile-time error, because it contains an expression. At line 3, the statement, "return;", does not contain an expression. The enclosing method, m3, is declared with the result type, int. The return statement generates a compile-time error, because it does not contain an expression that produces a value that is assignable to the declared result type. |
| 17 | c | Compile-time error at 3. | The compiler creates a constructor for class C implicitly. The implicitly created constructor accepts no parameters and has no throws clause. The constructors for class B and class C both invoke the constructor for A. The constructor for class A declares Exception in the throws clause. Since the constructors for B and C invoke the constructor for A implicitly, both B and C must declare Exception in their throws clause. A compile-time error is generated at marker 3, because the default constructor does not declare Exception in the throws clause. |
| 18 | c | Prints: 4 | The array initializer, {{1,2},{3,4,5},{6,7,8,9},{}}, creates an array containing four components, and each is a reference to a subarray of type int[]. The size of the array referenced by variable a1, is 4, because a1 contains four components. The size of the first subarray is 2, the second is 3, the third is 4 and the fourth is zero. |
| 19 | e | Compile-time error | Variable b1 is initialized to false, because it is a class member. The array component array[0] is initialized to the default value, false, of the array type, boolean[], even though the array is declared locally. Local variable b2 is not initialized, because it is local. A compile-time error is generated by the statement that attempts to print the value of b2. |
| 20 | a | Prints: 2,3,4,0, | The array initializer, {{1,2},{3,4,5},{6,7,8,9},{}}, creates an array containing four components, and each is a reference to an array of type int[]. The size of the first subarray is 2, the second is 3, the third is 4, and the fourth is zero. While the components of the array referenced by a1 are of type int[], the elements of the array referenced by a1 are of type int. |