The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.
Today, you can find my book at amazon.com.
| 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 | e | Compile-time error | The access modifiers public, protected and private, can not be applied to variables declared inside methods. |
| 4 | d | Prints: 0 | Member variables are initialized automatically. Type int variables are initialized to zero. |
| 5 | d | Prints: 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. |
| 6 | e | Compile-time error | A static method can not access a non-static variable. |
| 7 | 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. |
| 8 | 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. |
| 9 | 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. |
| 10 | 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. |
| 11 | 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. |
| 12 | 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. |
| 13 | 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. |
| 14 | 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. |
| 15 | a b e | A final method can not be overridden. All methods declared in a final class are implicitly final. A 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. |
| 16 | a | 1 | 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. |
| 17 | c g | 3 7 | 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. |
| 18 | a c e | If an accessible superclass method is static, then any method with the same signature in a subclass must also be static. If a superclass method is public, then the overriding method must also be public. If 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. |
| 19 | d | 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 be applied to any class; because it is a field modifier. |
| 20 | e | 5 | The synchronized modifier is a method modifier and can not be applied to a field. |