OCA Review 7 - Online Test Chapter 5
Today, I’m going to review my online test, chapter 5 of Oracle Certified Associate (OCA), provided by SYBEX. If you need to access to this online resources, you need to buy their book first. See.
Question 2
What is the output of the following code?
Platypus
Mammal
PlatypusMammal
MammalPlatypus
- The code will not compile because of line 8.
- The code will not compile because of line 11.
The answer is E. The code will not compile because the parent class
Mammal does not define no-argument constructor, so the first line of a
Playtypus constructor should make an explicit call to
super(int). If this
were the case, then the output would be D, because the parent is called first,
then the child constructor is called.
Question 4
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; } } }
- It will compile without issue.
- It fails to compile because the type of the exception the method throws is a subclass of the type in the parent method throws.
- It fails to compile because the return types are not covariant.
- It fails to compile because the method is
protectedin the parent class and
publicin the subclass.
- It fails to compile because of a
staticmodifier mismatch between the two methods.
The answers is CE. The code does not compile so answer A is wrong. Option B
is wrong because a subclass can throw an exception which is a subclass of the
exception in the parent class. However, a subclass cannot declare an overridden
method with a new or border exception than in the super class, since the
method may be accessed using a reference to the superclass. Option C is
correct. The return types are not covariant, in particular,
Number is not a
subclass of
Integer. Option D is wrong. A subclass can have a higher
visibility than its parent. Option E is correct. For non-private methods in the
parent class, both methods must use
static (hide) or neither should use
static (override).
Question 10
Which modifiers are assumed for all interface variables? (Choose all that apply)
- All methods within them are assumed to be abstract.
- Both can contain
public static finalvariables.
- Both can be extended using the
extendskeyword.
- Both can contain
defaultmethods.
- Both can contain
staticmethods.
- Neither can be instantiated directly.
- Both inherit
java.lang.Object.
The answer is BCEF. Option A is wrong, because an abstract class may
contain concrete method. Since Java 8, interfaces can also contain methods in
form of static of default methods. Option B is true, interface can contain
variables in form of public static final, abstract class can contain variables
too. Option C is correct because interface can be extended by another
interface, and abstract class can be extended by another concrete or abstract
class.Option D is wrong, abstract class cannot contain default modifier. Option
E is correct. Both interface and abstract class require a subclass to be
instantiated, so option F is correct. Java does not support multiple
inheritance for objects, and interface itself does not inherit
java.lang.Object.
Question 14
Which statements are true about the following code? (Choose all that apply)
interface HasVocalCords { public abstract void makeSound(); } public interface CanBark extends HasVocalCords { public void bark(); }
- The
CanBarkinterface does not compile.
- A class that implements
HasVocalCordsmust override the
makeSound()method.
- A class that implements
CanBarkinherits both the
makeSound()and
bark()methods.
- A class that implements
CanBarkonly inherits the
bark()method.
- An interface cannot extend another interface.
The answer is C. Option A is wrong because when a method is extended from
its parent interface, the methods of the parent interface are inherited
automatically. Option B is wrong because an abstract class which implements
the interface
CanBark does not need to override its method. This is different
from concrete class. Option C is correct. Option D is wrong. An interface can
extend another interface, so option E is wrong.
Question 17
What is the output of the following code?
Orca diving
- The code will not compile because of line 2.
- The code will not compile because of line 8.
- The code will not compile because of line 9.
- The output cannot be determined from the code provided.
The answer is B. An abstract method does not have body.
Question 20
What is the result of the following code?
Bird is flying.
Pelican is flying
- The code will not compile because of line 4.
- The code will not compile because of line 5.
- The code will not compile because of line 9.
The answer is A. The code compiles here, so options CDE are wrong. The
tricky thing is that when
Pelican extends
Bird, bird’s method
fly is
hidden, not overridden since it is private. With a hidden method, the
location of the caller determines which method will be used. Since it is
located in the class of
Bird, the method of bird is used. So A is correct.