Programmer I Chpater 9: Advanced Class Design Flashcards
Which line or lines of this top-level interface declaration do not compile?
1: private final interface Crawl {
2: String distance;
3: private int MAXIMUM_DEPTH = 100;
4: protected abstract boolean UNDERWATER = false;
5: private void dig(int depth);
6: protected abstract double depth();
7: public final void surface(); }
Every single line of this example, including the interface declaration,does not compile!
Line 1 does not compile for two reasons. First, it is marked as final, which cannot be applied to an interface since it conflicts with the implicit abstract keyword. Next, it is marked as private, which conflicts with the public or package-private access for top-level interfaces.
Line 2 does not compile because the distance variable is notinitialized. Remember that interface variables are assumed to bestatic final constants and initialized when they are declared. Lines 3and 4 do not compile because interface variables are also assumed tobe public, and the access modifiers on these lines conflict with this.
Line 4 also does not compile because variables cannot be markedabstract.Next, lines 5 and 6 do not compile because all interface abstractmethods are assumed to be public and marking them as private orprotected is not permitted.
Finally, the last line doesn’t compilebecause the method is marked as final, and since interface methodswithout a body are assumed to be abstract, the compiler throws anexception for using both abstract and final keywords on a method.
What modifiers are implicitly applied to all interface methods that do not declare a body? (Choose all that apply.)
A. protected B. public C. static D. void E. abstract F. default
B, E.
A method that does not declare a body is by definition abstract, making option E correct. All abstract interface methods are assumed to be public, making option B correct. Interface methods cannot be marked protected, so option A is incorrect.Interface methods can be marked static or default, although if they are, they must provide a body, making options C and F incorrect. Finally, void is a return type, not a modifier, so option D is incorrect
Which of the following statements can be inserted in the blank line so that the code will compile successfully? (Choose all that apply.)
interface CanHop {} public class Frog implements CanHop { public static void main(String[] args) { \_\_\_\_\_\_\_\_\_\_\_\_ frog = new TurtleFrog(); }}
class BrazilianHornedFrog extends Frog {} class TurtleFrog extends Frog {}
A. Frog B. TurtleFrog C. BrazilianHornedFrog D. CanHop E. Object F. Long G. None of the above; the code contains a compilation error
A, B, D, E. The code compiles without issue, so option G is incorrect. The blank can be filled with any class or interface that is a supertype of TurtleFrog. Option A is the direct superclass of TurtleFrog, and option B is the same class, so both are correct.BrazilianHornedFrog is not a superclass of TurtleFrog, so option C is incorrect. TurtleFrog inherits the CanHop interface, so option Dis correct. All classes inherit Object, so option E is also correct.Finally, Long is an unrelated class that is not a superclass of TurtleFrog and is therefore incorrect
Which of the following is true about a concrete class? (Choose all that apply.)
A. A concrete class can be declared as abstract. B. A concrete class must implement all inherited abstract methods. C. A concrete class can be marked as final. D. If a concrete class inherits an interface from one of its superclasses, then it must declare an implementation for all methods defined in that interface. E. A concrete method that implements an abstract method must match the method declaration of the abstract method exactly
B, C. Concrete classes are, by definition, not abstract, so option A is incorrect. A concrete class must implement all inherited abstract methods, so option B is correct. Concrete classes can be optionally marked final, so option C is correct. Option D is incorrect; a superclass may have already implemented an inherited interface method. The concrete class only needs to implement the inherited abstract methods. Finally, a method in a concrete class that implements an inherited abstract method overrides the method. While the method signature must match,the method declaration does not need to match, such as using a covariant return type or changing the throws declaration. For these reasons, option E is incorrect
Which statements about the following program are correct?(Choose all that apply.)
1: interface HasExoskeleton {
2: double size = 2.0f;
3: abstract int getNumberOfSections();
4: }
5: abstract class Insect implements HasExoskeleton {
6: abstract int getNumberOfLegs();
7: }
8: public class Beetle extends Insect {
9: int getNumberOfLegs() { return 6; }
10: int getNumberOfSections(int count) { return 1; }
11: }
A. It compiles without issue.
B. The code will produce a ClassCastException if called at runtime.
C. The code will not compile because of line 2.
D. The code will not compile because of line 5.
E. The code will not compile because of line 8.
F. The code will not compile because of line 10
E. First, the declarations of HasExoskeleton and Insect are correct and do not contain any errors, making options C and D incorrect. The concrete class Beetle extends Insect and inherits two abstract methods, getNumberOfSections() and getNumberOfLegs(). The Beetle class includes an overloaded version ofgetNumberOfSections() that takes an int value. The method declaration is valid, making option F incorrect, although it does not satisfy the abstract method requirement. For this reason, only one of the two abstract methods is properly overridden. The Beetle class therefore does not compile, and option E is correct.Since the code fails to compile, options A and B are incorrect
What modifiers are implicitly applied to all interface variables?(Choose all that apply.)
A. private B. nonstatic C. final D. const E. abstract F. public G. default (package-private)
C, F.
All interface variables are implicitly assumed to be public,static, and final, making options C and F correct. Option A and G, private and default (package-private), are incorrect since they conflict with the implicit public access modifier. Options B and Dare incorrect, as nonstatic and const are not modifiers. Finally,option E is incorrect because a variable cannot be marked abstract
Which statements about the following program are correct?(Choose all that apply.)
1: public abstract interface Herbivore {
2: int amount = 10;
3: public void eatGrass();
4: public abstract int chew() { return 13; }
5: }
6:
7: abstract class IsAPlant extends Herbivore {
8: Object eatGrass(int season) { return null; }
9: }
A. It compiles and runs without issue.
B. The code will not compile because of line 1.
C. The code will not compile because of line 2.
D. The code will not compile because of line 4.
E. The code will not compile because of line 7.
F. The code will not compile because line 8 contains an invalid method override
D, E. Lines 1 and 2 are declared correctly, with the implicit modifier abstract being applied to the interface and the implicit modifiers public, static, and final being applied to the interface variable, making options B and C incorrect. Option D is correct, as an abstract method cannot include a body. Option E is also correct because the wrong keyword is used. A class implements an interface; it does extend it. Option F is incorrect as the implementation of eatGrass() in IsAPlant does not have the same signature; therefore, it is an overload, not an override
Which statements about the following program are correct?(Choose all that apply.)
1: abstract class Nocturnal {
2: boolean isBlind();
3: }
4: public class Owl extends Nocturnal {
5: public boolean isBlind() { return false; }
6: public static void main(String[] args) {
7: var nocturnal = (Nocturnal)new Owl();
8: System.out.println(nocturnal.isBlind());
9: } }
A. It compiles and prints true.
B. It compiles and prints false.
C. The code will not compile because of line 2.
D. The code will not compile because of line 5.
E. The code will not compile because of line 7.
F. The code will not compile because of line 8.
G. None of the above
C.
The code does not compile because the isBlind() method in Nocturnal is not marked abstract and does not contain a method body. The rest of the lines compile without issue, making option C the only correct answer. If the abstract modifier was added to line2, then the code would compile and print false at runtime,making option B the correct answer.
Which statements are true about the following code? (Choose allthat apply.)
interface Dog extends CanBark, HasVocalCords { abstract int chew(); } public interface CanBark extends HasVocalCords { public void bark(); } interface HasVocalCords { public abstract void makeSound(); }
A. The CanBark declaration doesn’t compile. B. A class that implements HasVocalCords must override the makeSound() method. C. A class that implements CanBark inherits both the makeSound() and bark() methods. D. A class that implements Dog must be marked final. E. The Dog declaration does not compile because an interface cannot extend two interfaces
C. The code compiles without issue, so option A is incorrect.Option B is incorrect, as an abstract class could implement HasVocalCords without the need to override the makeSound() method. Option C is correct; a class that implements CanBark automatically inherits its abstract methods, in this case makeSound() and bark(). Option D is incorrect, as a concrete class that implements Dog may be optionally marked final. Finally, an interface can extend multiple interfaces, so option E is incorrect.
Which access modifiers can be applied to member inner classes?(Choose all that apply.)
A. static B. public C. default (package-private) D. final E. protected F. private
B, C, E, F.
Member inner classes, including both classes and interfaces, can be marked with any of the four access modifiers:public, protected, default (package-private), or private. For this reason, options B, C, E, and F are correct. Options A and D are incorrect as static and final are not access modifiers
Which statements are true about the following code? (Choose all that apply.)
5: public interface CanFly {
6: int fly()
7: String fly(int distance);
8: }
9: interface HasWings {
10: abstract String fly();
11: public abstract Object getWingSpan();
12: }
13: abstract class Falcon implements CanFly, HasWings {}
A. It compiles without issue.
B. The code will not compile because of line 5.
C. The code will not compile because of line 6.
D. The code will not compile because of line 7.
E. The code will not compile because of line 9.
F. The code will not compile because of line 10.
G. The code will not compile because of line 13
C, G. The implicitly abstract interface method on line 6 does not compile because it is missing a semicolon (;), making option C correct. Line 7 compiles, as it provides an overloaded version of the fly() method. Lines 5, 9, and 10 do not contain any compilation errors, making options A, E, and F incorrect. Line 13 does not compile because the two inherited fly() methods, declared on line 6 and 10, conflict with each other. The compiler recognizes that it is impossible to create a class that overrides fly() to return both String and int, since they are not covariant return types, and therefore blocks the Falcon class from compiling. For this reason, option G is correct
Which modifier pairs can be used together in a method declaration? (Choose all that apply.)
A. static and final B. private and static C. static and abstract D. private and abstract E. abstract and final F. private and final
A, B, F.
The final modifier can be used with private and static,making options A and F correct. Marking a private method final is redundant but allowed. A private method may also be marked static, making option B correct. Options C, D, and E are incorrect because methods marked static, private, or final cannot be overridden; therefore, they cannot be marked abstract
Which of the following statements about the FruitStand program are correct? (Choose all that apply.)
1: interface Apple {}
2: interface Orange {}
3: class Gala implements Apple {}
4: class Tangerine implements Orange {}
5: final class Citrus extends Tangerine {}
6: public class FruitStand {
7: public static void main(String… farm) {
8: Gala g = new Gala();
9: Tangerine t = new Tangerine();
10: Citrus c = new Citrus();
11: System.out.print(t instanceof Gala);
12: System.out.print(c instanceof Tangerine);
13: System.out.print(g instanceof Apple);
14: System.out.print(t instanceof Apple);
15: System.out.print(c instanceof Apple);
16: } }
A. Line 11 contains a compiler error. B. Line 12 contains a compiler error. C. Line 13 contains a compiler error. D. Line 14 contains a compiler error. E. Line 15 contains a compiler error. F. None of the above
A, E. Line 11 does not compile because a Tangerine and Gala are unrelated types, which the compiler can enforce for classes,making option A correct. Line 12 is valid since Citrus extends Tangerine and would print true at runtime if the rest of the class compiled. Likewise, Gala implements Apple, so line 13 would also print true at runtime if the rest of the code compiled. Line 14 does compile, even though Apple and Tangerine are unrelated types.While the compiler can enforce unrelated type rules for classes, it has limited ability to do so for interfaces, since there may be a subclass of Tangerine that implements the Apple interface.Therefore, this line would print false if the rest of the code compiled. Line 15 does not compile. Since Citrus is marked final,the compiler knows that there cannot be a subclass of Citrus that implements Apple, so it can enforce the unrelated type rule. For this reason, option E is correct
What is the output of the following code?
1: interface Jump {
2: static public int MAX = 3;
3: }
4: public abstract class Whale implements Jump {
5: public abstract void dive();
6: public static void main(String[] args) {
7: Whale whale = new Orca();
8: whale.dive(3);
9: }
10: }
11: class Orca extends Whale {
12: public void dive() {
13: System.out.println(“Orca diving”);
14: }
15: public void dive(int… depth) {
16: System.out.println(“Orca diving deeper “+MAX);
17: } }
A. Orca diving
B. Orca diving deeper 3
C. The code will not compile because of line 2.
D. The code will not compile because of line 4.
E. The code will not compile because of line 11.
F. The code will not compile because of line 16.
G. None of the above
G. The interface and classes are structured correctly, but the body of the main() method contains a compiler error. The Orca object is implicitly cast to a Whale reference on line 7. This is permitted because Orca is a subclass of Whale. By performing the cast, the whale reference on line 8 does not have access to the dive(int...depth) method. For this reason, line 8 does not compile. Since this is the only compilation error, option G is the correct answer. If the reference type of whale was changed to Orca, then the main()would compile and print Orca diving deeper 3 at runtime,making option B the correct answer. Note that line 16 compiles because the interface variable MAX is inherited as part of the class structure
Which statements are true for both abstract classes and interfaces? (Choose all that apply.)
A. Both can be extended using the extends keyword.
B. All methods within them are assumed to be abstract.
C. Both can contain public static final variables.
D. The compiler will insert the implicit abstract modifier automatically on methods declared without a body, if they are
not marked as such.
E. Both interfaces and abstract classes can be declared with the abstract modifier.
F. Both inherit java.lang.Object.
A, C, E. A class may extend another class, and an interface may extend another interface, making option A correct. Option B is incorrect. An abstract class can contain concrete instance and static methods. Interfaces can also contain non abstract methods,although knowing this is not required for the 1Z0-815 exam.Option C is correct, as both can contain static constants. Option D is incorrect. The compiler only inserts implicit modifiers for interfaces. For abstract classes, the abstract keyword must be used on any method that does not define a body. An abstract class must be declared with the abstract keyword, while the abstract keyword is optional for interfaces. Since both can be declared with the abstract keyword, option E is correct. Finally, interfaces do not extend java.lang.Object. If they did, then Java would support true multiple inheritance, with multiple possible parent constructors being called as part of initialization. Therefore,option F is incorrect