| No. | Answer | Remark | |
|---|---|---|---|
| 1 | a e | abstract public | The modifier, abstract, is applicable to an interface declaration, but its use is strongly discouraged; because every interface is implicitly abstract. An interface can not be final. The modifiers, private and protected, are applicable only to an interface declaration that is a member of a directly enclosing class declaration. If an interface is not a member of a directly enclosing class, or if the interface is a member of a directly enclosing interface; then, the modifiers, private and protected, are not applicable. If an interface is declare public, then the compiler will generate an error if the class is not stored in a file that has the same name as the interface plus the extension .java. |
| 2 | e | None of the above | A tightly encapsulated class may have public mutator methods. |
| 3 | a b | abstract public | The modifier, abstract, is applicable to an interface declaration, but its use is strongly discouraged; because every interface is implicitly abstract. If an interface is declare public, then the compiler will generate an error if the class is not stored in a file that has the same name as the interface plus the extension .java. The modifier, static, is applicable to a 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. |
| 4 | c | The internal data model can be read and modified only through accessor and mutator methods. | A class is not tightly encapsulated if the internal data model can be read and/or modified without working through accessor (i.e. get) and mutator (i.e. set) methods. |
| 5 | c f | final public | The modifier, abstract, is not applicable to a variable. All field declarations within an interface are implicitly public, static and final. Use of those modifiers is redundant but legal. Although const is a Java keyword, it is not currently used by the Java programming language. An interface member can never be private or protected. |
| 6 | d | None of the above | All three classes are tightly encapsulated, because the data members are private. A tightly encapsulated class can have public accessor and mutator methods, but it is not required to have those methods. |
| 7 | a | static | All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. A field that is declared final can not also be declared volatile; so a field of an interface can not be declared volatile. The modifier, synchronized, is never applicable to a field. |
| 8 | d | None of the above | Class GFC503 is not tightly encapsulated; so no subclass of GFC503 is tightly encapsulated. |
| 9 | a d e f | public abstract static final | A class that is declared within an enclosing interface is implicitly public and static; so the access modifiers, protected and private, are not applicable. |
| 10 | b | GFC507 | Class GFC507 has a public field; so it is not tightly encapsulated. |
| 11 | e | None of the above | All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. No other modifiers can be applied to a field declaration within an interface. |
| 12 | g | None of the above | If a superclass method is not private and is accessible to code in a subclass, then any subclass method that has the same signature as the superclass method must also have the same return type. In other words, if a superclass method is overridden or hidden in a subclass, then the overriding or hiding subclass method must have the same return type as the superclass method. No such restriction applies to method overloading. If two methods share an overloaded method name but not the same parameter list, then the two methods need not have the same return type. Class A declares one method: The name is m1 and the single parameter is of type String. Class B extends A and declares six methods that overload the name m1. The method, B.m1(String s1), overrides the superclass method, A.m1(String s1). The overriding subclass method must have the same return type as the superclass method, but the methods that overload the name m1 are free to have different return types. An overriding subclass method is not permitted to throw any checked exception that is not listed or is not a subclass of any of those listed in the throws clause of the superclass method. No such restriction applies to method overloading. If two methods share an overloaded method name but not the same parameter list, then the two methods are free to have differing throws clauses. |
| 13 | e | 5 | All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. No other modifiers can be applied to a field declaration within an interface. |
| 14 | a | Prints: main,B.m1 | Suppose a superclass method is not private and is accessible to code in a subclass. If the superclass method is declared static, then any subclass method sharing the same signature must also be declared static. Similarly, if the superclass method is not declared static, then any subclass method sharing the same signature must not be declared static. The rules governing method overloading are different. If a superclass method is declared static, then any subclass method that overloads the superclass method is free to be declared static or non-static. Similarly, if a method is declared non-static, then any overloading method is free to be declared static or non-static. Method B.m1() shares the same signature as the non-static superclass method A.m1(), so B.m1() must also be non-static. The method B.m1(String s) overloads the method name m1, but does not share the same signature with any superclass method; therefore, B.m1(String s) can be declared static even though the other methods of the same name are non-static. |
| 15 | f | Compile-time error | The two-parameter constructor of U does not explicitly invoke the two-parameter constructor of T; therefore, the constructor of U will try to invoke a no-parameter constructor of T, but none exists. |
| 16 | a | Prints: AAA | The reference c2 is of the superclass type, A; so it can be used to invoke only the method, m1, declared in class A. The methods that overload the method name m1 in the subclasses, B and C, can not be invoked using the reference c2. |
| 17 | a | Prints: ABCI | Suppose that a class extends a superclass, X, or implements an interface, X. The field access expression ((X)this).hiddenField is used to access the hidden field, hiddenField, that is accessible within the superclass or interface, X. |
| 18 | d | Compile-time error | The declarations of b1 and c1 cause compile-time errors, because a reference of a subclass type can not refer to an instance of the superclass type. |
| 19 | b | Prints: DE | At run-time, the actual field that is accessed depends on the compile-time type of the reference--not the run-time type of the object. The compile-time type of the reference x in the name x.s1 is D; so the selected field is the one declared in class D. A non-static method is selected based on the run-time type of the object--not the compile-time type of the reference. The same reference variable x is used in the method invocation expression x.getS1(), and the compile-time type is still D. At run-time, the actual type of the object is E; so the selected method is the one declared in class E. |
| 20 | d | Compile-time error | The declaration of c2 causes a compile-time error, because a reference of a subclass type can not refer to an instance of the superclass class. |
| 21 | c | Prints: CBA | Class C extends B, and B extends A. The static method C.m hides method B.m, and B.m hides A.m. In the method invocation expression c.m(), the compile-time type of the reference c is C. A static method is invoked based on the compile-time type of the reference; so the method invocation expression c.m() invokes the method m declared in class C. The compile-time type of the reference b is B; so the method invocation expression b.m() invokes the method m declared in class B. The compile-time type of the reference a is A; so the method invocation expression a.m() invokes the method m declared in class A. |
| 22 | b | Prints: ABC | In all three cases, the object passed to method m1 is an instance of class C; however, the type of the reference is different for each method. Since fields are accessed based on the type of the reference, the value printed by each method is different even though the same instance is used for each method invocation. |