Programmer I Chpater 9: Advanced Class Design Flashcards

1
Q

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(); }

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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
A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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)
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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
A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the result of the following code?

1: abstract class Bird {
2: private final void fly() {System.out.println(“Bird”); }
3: protected Bird() { System.out.print(“Wow-“); }
4: }
5: public class Pelican extends Bird {
6: public Pelican() { System.out.print(“Oh-“); }
7: protected void fly() { System.out.println(“Pelican”);}
8: public static void main(String[] args) {
9: var chirp = new Pelican();
10: chirp.fly();
11: } }

A. Oh-Bird
B. Oh-Pelican
C. Wow-Oh-Bird
D. Wow-Oh-Pelican
E. The code contains a compilation error.
F. None of the above
A
D. 
The code compiles without issue. The question is making sure you know that superclass constructors are called in the same manner in abstract classes as they are in non abstract classes. Line9 calls the constructor on line 6. The compiler automatically inserts super() as the first line of the constructor defined on line6. The program then calls the constructor on line 3 and prints Wow-. Control then returns to line 6, and Oh- is printed. Finally, the method call on line 10 uses the version of fly() in the Pelican class, since it is marked private and the reference type of var is resolved as Pelican. The final output is Wow-Oh-Pelican, making option D the correct answer. Remember that private methods cannot be overridden. If the reference type of chirp was Bird, then the code would not compile as it would not be accessible outside the class
17
Q

Which of the following statements about this program is correct?

1: interface Aquatic {
2: int getNumOfGills(int p);
3: }
4: public class ClownFish implements Aquatic {
5: String getNumOfGills() { return “14”; }
6: int getNumOfGills(int input) { return 15; }
7: public static void main(String[] args) {
8: System.out.println(newClownFish().getNumOfGills(-1));
9: } }

A. It compiles and prints 14.
B. It compiles and prints 15.
C. The code will not compile because of line 4.
D. The code will not compile because of line 5.
E. The code will not compile because of line 6.
F. None of the above

A
E. 
The inherited interface method getNumOfGills(int) is implicitly public; therefore, it must be declared public in any concrete class that implements the interface. Since the method uses the default(package-private) modifier in the ClownFish class, line 6 does not compile, making option E the correct answer. If the method declaration was corrected to include public on line 6, then the program would compile and print 15 at runtime, and option B would be the correct answer
18
Q

Which statements about top-level types and member inner classes are correct? (Choose all that apply.)

A. A member inner class can be marked final.
B. A top-level type can be marked protected.
C. A member inner class cannot be marked public since that would make it a top-level class.
D. A top-level type must be stored in a .java file with a name that matches the class name.
E. If a member inner class is marked private, then it can be referenced only in the outer class for which it is defined
A
A, E. 
An inner class can be marked abstract or final, just like a regular class, making option A correct. A top-level type, such as a class, interface, or enum, can only be marked public or default(package-private), making option B incorrect. Option C is incorrect, as a member inner class can be marked public, and this would not make it a top-level class. A .java file may contain multiple top-level classes, making option D incorrect. The precise rule is that there is at most one public top-level type, and that type is used in the filename. Finally, option E is correct. When a member inner class is marked private, it behaves like any other private members and can be referenced only in the class in which it is defined
19
Q

What types can be inserted in the blanks on the lines marked X and Z that allow the code to compile? (Choose all that apply.)

interface Walk { public List move(); }
interface Run extends Walk { public ArrayList move(); }
public class Leopard {
public \_\_\_\_\_\_ move() { // X
return null;
}}
public class Panther implements Run {
public \_\_\_\_\_\_ move() { // Z
return null;
}}

A. Integer on the line marked X
B. ArrayList on the line marked X
C. List on the line marked Z
D. ArrayList on the line marked Z
E. None of the above, since the Run interface does not compile.
F. The code does not compile for a different reason

A
A, B, D. 
The Run interface correctly overrides the inherited method move() from the Walk interface using a covariant return type. Options A and B are both correct. Notice that the Leopard class does not implement or inherit either interface, so the return type of move() can be any valid reference type that is compatible with the body returning null. Because the Panther class inherits both interfaces, it must override a version of move() that is covariant with both interfaces. Option C is incorrect, as List is not a subtype of ArrayList, and using it here conflicts with the Run interface declaration. Option D is correct, as ArrayList is compatible with both List and ArrayList return types. Since the code is capable of compiling, options E and F are incorrect
20
Q

Which statements about interfaces are correct? (Choose all that apply.)

A. A class cannot extend multiple interfaces.
B. Java enables true multiple inheritance via interfaces.
C. Interfaces cannot be declared abstract.
D. If an interface does not contain a constructor, the compiler will insert one automatically.
E. An interface can extend multiple interfaces.
F. An interface cannot be instantiated
A
A, E, F. 
A class cannot extend any interface, as a class can only extend other classes and interfaces can only extend other interfaces, making option A correct. Java enables only limited multiple inheritance with interfaces, making option B incorrect.True multiple inheritance would be if a class could extend multiple classes directly. Option C is incorrect, as interfaces are implicitly marked abstract. Option D is also incorrect, as interfaces do not contain constructors and do not participate in object initialization. Option E is correct, an interface can extend multiple interfaces. Option F is also correct, as abstract types cannot be instantiated.
21
Q

Which of the following classes and interfaces are correct and compile? (Choose all that apply.)

abstract class Camel {
void travel();
}
interface EatsGrass {
protected int chew();
}
abstract class Elephant {
abstract private class SleepsAlot {
abstract int sleep();
}}
class Eagle {
abstract soar();
}
A. SleepsAlot
B. Eagle
C. Camel
D. Elephant
E. EatsGrass
F. None of the classes or interfaces compile
A
A, D. 
The implementation of Elephant and its member inner class SleepsAlot are valid, making options A and D correct. Option B is incorrect, as Eagle must be marked abstract to contain an abstract method. Option C is also incorrect. Since the travel()method does not declare a body, it must be marked abstract in an abstract class. Finally, option E is incorrect, as interface methods are implicitly public. Marking them protected results in a compiler error