five Flashcards
What modifiers are implicitly applied to all interface methods? (Choose all that apply) A. protected B. public C. static D. void E. abstract F. default
B. All interface methods are implicitly public, so option B is correct and option A is
not. Interface methods may be declared as static or default but are never implicitly
added, so options C and F are incorrect. Option D is incorrect—void is not a modifier;
it is a return type. Option E is a tricky one, because prior to Java 8 all interface methods would be assumed to be abstract. Since Java 8 now includes default and static
methods and they are never abstract, you cannot assume the abstract modifier will be
implicitly applied to all methods by the compiler.
What is the output of the following code? 1: class Mammal { 2: public Mammal(int age) { 3: System.out.print("Mammal"); 4: } 5: } 6: public class Platypus extends Mammal { 7: public Platypus() { 8: System.out.print("Platypus"); 9: } 10: public static void main(String[] args) { 11: new Mammal(5); 12: } 13: } A. Platypus B. Mammal C. PlatypusMammal D. MammalPlatypus E. The code will not compile because of line 8. F. The code will not compile because of line 11
E. The code will not compile because the parent class Mammal doesn’t define a no-argu-ment constructor, so the first line of a Platypus constructor should be an explicit call to super(int age). If there was such a call, then the output would be MammalPlatypus, since the super constructor is executed before the child constructor.
Which of the following statements can be inserted in the blank line so that the code will compile successfully? (Choose all that apply) public interface CanHop {} public class Frog implements CanHop { public static void main(String[] args) { \_\_\_\_\_\_\_\_\_\_\_\_\_ frog = new TurtleFrog(); } }
public class BrazilianHornedFrog extends Frog {} public class TurtleFrog extends Frog {} A. Frog B. TurtleFrog C. BrazilianHornedFrog D. CanHop E. Object F. Long
A, B, D, E. The blank can be filled with any class or interface that is a supertype of TurtleFrog. Option A is a 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 CanHope interface, so option D is correct. All classes inherit Object, so option E is correct. Finally, Long is an unrelated class that is not a superclass of TurtleFrog, and is therefore incorrect.
Which statement(s) are correct about the following code? (Choose all that apply) public class Rodent { protected static Integer chew() throws Exception { System.out.println("Rodent is chewing"); return 1; } } public class Beaver extends Rodent { public Number chew() throws RuntimeException { System.out.println("Beaver is chewing on wood"); return 2; } } A. It will compile without issue. B. It fails to compile because the type of the exception the method throws is a subclass of the type of exception the parent method throws. C. It fails to compile because the return types are not covariant. D. It fails to compile because the method is protected in the parent class and public in the subclass. E. It fails to compile because of a static modifier mismatch between the two methods.
C, E. The code doesn’t compile, so option A is incorrect. Option B is also not correct because the rules for overriding a method allow a subclass to define a method with an exception that is a subclass of the exception in the parent method. Option C is correct because the return types are not covariant; in particular, Number is not a subclass of Integer. Option D is incorrect because the subclass defines a method that is more accessible than the method in the parent class, which is allowed. Finally, option E is correct because the method is declared as static in the parent class and not so in the child class. For nonprivate methods in the parent class, both methods must use static (hide) or neither should use static (override).
Which of the following may only be hidden and not overridden? (Choose all that apply) A. private instance methods B. protected instance methods C. public instance methods D. static methods E. public variables F. private variables
A, D, E, F. First off, options B and C are incorrect because protected and public meth-ods may be overridden, not hidden. Option A is correct because private methods are
always hidden in a subclass. Option D is also correct because static methods cannot
be overridden, only hidden. Options E and F are correct because variables may only be
hidden, regardless of the access modifier.
Choose the correct statement about the following code:
1: interface HasExoskeleton {
2: abstract int getNumberOfSections();
3: }
4: abstract class Insect implements HasExoskeleton {
5: abstract int getNumberOfLegs();
6: }
7: public class Beetle extends Insect {
8: int getNumberOfLegs() { return 6; }
9: }
A. It compiles and runs without issue.
B. The code will not compile because of line 2.
C. The code will not compile because of line 4.
D. The code will not compile because of line 7.
E. It compiles but throws an exception at runtime
D. The code fails to compile because Beetle, the first concrete subclass, doesn’t imple-ment getNumberOfSections(), which is inherited as an abstract method; therefore, option D is correct. Option B is incorrect because there is nothing wrong with this interface method definition. Option C is incorrect because an abstract class is not required to implement any abstract methods, including those inherited from an inter-face. Option E is incorrect because the code fails at compilation-time
Which of the following statements about polymorphism are true? (Choose all that apply) A. A reference to an object may be cast to a subclass of the object without an explicit cast. B. If a method takes a superclass of three objects, then any of those classes may be passed as a parameter to the method. C. A method that takes a parameter with type java.lang.Object will take any reference. D. All cast exceptions can be detected at compile-time. E. By defining a public instance method in the superclass, you guarantee that the specific method will be called in the parent class at runtime.
B, C. A reference to an object requires an explicit cast if referenced with a subclass, so option A is incorrect. If the cast is to a superclass reference, then an explicit cast is not required. Because of polymorphic parameters, if a method takes the superclass of an object as a parameter, then any subclass references may be used without a cast, so option B is correct. All objects extend java.lang.Object, so if a method takes that type, any valid object, including null, may be passed; therefore, option C is correct. Some cast exceptions can be detected as errors at compile-time, but others can only be detected at runtime, so D is incorrect. Due to the nature of polymorphism, a public instance method can be overridden in a subclass and calls to it will be replaced even in the superclass it was defined, so E is incorrect.
Choose the correct statement about the following code:
1: public interface Herbivore {
2: int amount = 10;
3: public static void eatGrass();
4: public int chew() {
5: return 13;
6: }
7: }
A. It compiles and runs without issue.
B. The code will not compile because of line 2.
C. The code will not compile because of line 3.
D. The code will not compile because of line 4.
E. The code will not compile because of lines 2 and 3.
F. The code will not compile because of lines 3 and 4.
F. The interface variable amount is correctly declared, with public and static being
assumed and automatically inserted by the compiler, so option B is incorrect. The
method declaration for eatGrass() on line 3 is incorrect because the method has been
marked as static but no method body has been provided. The method declaration for
chew() on line 4 is also incorrect, since an interface method that provides a body must
be marked as default or static explicitly. Therefore, option F is the correct answer
since this code contains two compile-time errors.
B, C, E. Encapsulation requires using methods to get and set instance variables so
other classes are not directly using them. Instance variables must be private for this
to work. Immutability takes this a step further, allowing only getters, so the instance
variables do not change state.
A. Although the definition of methods on lines 2 and 5 vary, both will be converted to
public abstract by the compiler. Line 4 is fine, because an interface can have pub-lic or default access. Finally, the class Falcon doesn’t need to implement the interface
methods because it is marked as abstract. Therefore, the code will compile without
issue.
Which statements are true for both abstract classes and interfaces? (Choose all that apply)
A. All methods within them are assumed to be abstract.
B. Both can contain public static final variables.
C. Both can be extended using the extend keyword.
D. Both can contain default methods.
E. Both can contain static methods.
F. Neither can be instantiated directly.
G. Both inherit java.lang.Object.
A. Although the definition of methods on lines 2 and 5 vary, both will be converted to
public abstract by the compiler. Line 4 is fine, because an interface can have pub-lic or default access. Finally, the class Falcon doesn’t need to implement the interface
methods because it is marked as abstract. Therefore, the code will compile without
issue.
What modifiers are assumed for all interface variables? (Choose all that apply) A. public B. protected C. private D. static E. final F. abstract
A, D, E. Interface variables are assumed to be public static final; therefore, options
A, D, and E are correct. Options B and C are incorrect because interface variables must
be public—interfaces are implemented by classes, not inherited by interfaces. Option F
is incorrect because variables can never be abstract.
What is the output of the following code?
1: interface Nocturnal {
2: default boolean isBlind() { return true; }
3: }
4: public class Owl implements Nocturnal }
public boolean isBlind() { return false; }
6: public static void main(String[] args) {
7: Nocturnal nocturnal = (Nocturnal)new Owl();
8: System.out.println(nocturnal.isBlind());
9: }
10: }
A. true
B. 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
B. This code compiles and runs without issue, outputting false, so option B is the
correct answer. The first declaration of isBlind() is as a default interface method,
assumed public. The second declaration of isBlind() correctly overrides the default
interface method. Finally, the newly created Owl instance may be automatically cast to
a Nocturnal reference without an explicit cast, although adding it doesn’t break the
code.
What is the output of the following code?
1: class Arthropod
2: public void printName(double input) { System.out
.print(“Arthropod”); }
3: }
4: public class Spider extends Arthropod {
5: public void printName(int input) { System.out.print(“Spider”); }
6: public static void main(String[] args) {
7: Spider spider = new Spider();
8: spider.printName(4);
9: spider.printName(9.0);
10: }
11: }
A. SpiderArthropod
B. ArthropodSpider
C. SpiderSpider
D. ArthropodArthropod
E. The code will not compile because of line 5.
F. The code will not compile because of line 9.
A. The code compiles and runs without issue, so options E and F are incorrect. The
printName() method is an overload in Spider, not an override, so both methods may
be called. The call on line 8 references the version that takes an int as input defined
in the Spider class, and the call on line 9 references the version in the Arthropod class
that takes a double. Therefore, SpiderArthropod is output and option A is the correct
answer.
Which statements are true about the following code? (Choose all that apply) 1: interface HasVocalCords { 2: public abstract void makeSound(); 3: } 4: public interface CanBark extends HasVocalCords { 5: public void bark(); 6: } A. The CanBark interface 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 CanBark only inherits the bark() method. E. An interface cannot extend another interface.
C. The code compiles without issue, so option A is wrong. Option B is incorrect, since an abstract class could implement HasVocalCords without the need to override the makeSound() method. Option C is correct; any class that implements CanBark auto-matically inherits its methods, as well as any inherited methods defined in the parent interface. Because option C is correct, it follows that option D is incorrect. Finally, an interface can extend multiple interfaces, so option E is incorrect.
Which of the following is true about a concrete subclass? (Choose all that apply) A. A concrete subclass can be declared as abstract. B. A concrete subclass must implement all inherited abstract methods. C. A concrete subclass must implement all methods defined in an inherited interface. D. A concrete subclass cannot be marked as final. E. Abstract methods cannot be overridden by a concrete subclass.
B. 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. Option C is incorrect; a superclass may have already implemented an inherited interface, so the concrete subclass would not need to implement the method. Concrete classes can be both final and not final, so option D is incorrect. Finally, abstract methods must be overridden by a concrete subclass, so option E is incorrect.