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 https://sybextestbanks.wiley.com/.
Question 2
What is the output of the following code?
1
2
3
4
5
6
7
8
9
10
11
12
13
class Mammal {
public Mammal(int age) {
System.out.println("Mammal");
}
}
public class Platypus extends Mammal {
public Platypus() {
System.out.println("Platypus");
}
public static void main(String[] args) {
new Mammal(5);
}
}
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)
- 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
protected
in the parent class andpublic
in the subclass. - It fails to compile because of a
static
modifier 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 final
variables. - Both can be extended using the
extends
keyword. - Both can contain
default
methods. - Both can contain
static
methods. - 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)
- The
CanBark
interface does not compile. - A class that implements
HasVocalCords
must override themakeSound()
method. - A class that implements
CanBark
inherits both themakeSound()
andbark()
methods. - A class that implements
CanBark
only inherits thebark()
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
public abstract class Whale {
public abstract void dive() {};
public static void main(String[] args) {
Whale whale = new Orca();
whale.dive();
}
}
class Orca extends Whale {
public void dive(int depth) {
System.out.println("Orca diving");
}
}
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?
1
2
3
4
5
6
7
8
9
10
public abstract class Bird {
private void fly() { System.out.println("Bird is flying"); }
public static void main(String[] args) {
Bird bird = new Pelican();
bird.fly();
}
}
class Pelican extends Bird {
protected void fly() { System.out.println("Pelican is flying"); }
}
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.