# OCA Review 7 - Online Test Chapter 5

Today, I’m going to review my online test, chapter 5 of Oracle CertifiedAssociate (OCA), provided by SYBEX. If you need to access to this onlineresources, you need to buy their book first. Seehttps...

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. Platypus
2. Mammal
3. PlatypusMammal
4. MammalPlatypus
5. The code will not compile because of line 8.
6. 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)

1. It will compile without issue.
2. It fails to compile because the type of the exception the method throws is a subclass of the type in the parent method throws.
3. It fails to compile because the return types are not covariant.
4. It fails to compile because the method is protected in the parent class and public in the subclass.
5. 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)

1. All methods within them are assumed to be abstract.
2. Both can contain public static final variables.
3. Both can be extended using the extends keyword.
4. Both can contain default methods.
5. Both can contain static methods.
6. Neither can be instantiated directly.
7. 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)

1. The CanBark interface does not compile.
2. A class that implements HasVocalCords must override the makeSound() method.
3. A class that implements CanBark inherits both the makeSound() and bark() methods.
4. A class that implements CanBark only inherits the bark() method.
5. 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. Orca diving
2. The code will not compile because of line 2.
3. The code will not compile because of line 8.
4. The code will not compile because of line 9.
5. 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. Bird is flying.
2. Pelican is flying
3. The code will not compile because of line 4.
4. The code will not compile because of line 5.
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.