PASS - TAKER : Anonymos

Java SE 8 OCP - Inner Classes

Correct : 10

76% Complete (success)

76 %

False : 3

23% Complete (success)

23 %



Anonymos 2018-08-27T16:24:45

Correct

1. Which are true about a static nested class? (Choose all that apply.)

B is correct : a static nested class is not tied to an instance of the enclosing class, and thus can't access the nonstatic members of the class (just as a static method can't access nonstatic members of a class). D uses the correct syntax for instantiating a static nested class. A is incorrect : static nested classes do not need (and can't use) a reference to an instance of the enclosing class. C is incorrect : static nested classes can declare and define nonstatic members. E is wrong because… it just is. There's no rule that says an inner or nested class has to extend anything.

Correct

2. Given: {code}
Which statements create an anonymous inner class from within class Bar? (Choose all that apply.)

B is correct : anonymous inner classes are no different from any other class when it comes to polymorphism. That means you are always allowed to declare a reference variable of the superclass type and have that reference variable refer to an instance of a subclass type, which in this case is an anonymous subclass of Bar. Since Bar is a subclass of Boo, it all works. C uses correct syntax for creating an instance of Boo. A is incorrect : it passes an int to the Boo constructor, and there is no matching constructor in the Boo class. D is incorrect : it violates the rules of polymorphism; you cannot refer to a superclass type using a reference variable declared as the subclass type. The superclass doesn't have everything the subclass has. E uses incorrect syntax.

Correct

3. Which are true about a method-local inner class? (Choose all that apply.)

B is correct : a method-local inner class can be abstract, although it means a subclass of the inner class must be created if the abstract class is to be used (so an abstract method-local inner class is probably not useful). E is correct : a method-local inner class works like any other inner class—it has a special relationship to an instance of the enclosing class, thus it can access all members of the enclosing class. A is incorrect : a method-local inner class does not have to be declared final (although it is legal to do so). C and D are incorrect : a method-local inner class cannot be made public (remember—local variables can't be public) or static.

Correct

4. Given: {code}
What is the result?

G is correct. This code would be legal if line 7 ended with a semicolon. Remember that line 3 is a statement that doesn't end until line 7, and a statement needs a closing semicolon! A, B, C, D, E, and F are incorrect based on the program logic just described. If the semicolon were added at line 7, then answer B would be correct—the program would print true, the return from the equals() method overridden by the anonymous subclass of Object.

Correct

5. Given: {code}
What is the result?

If you use a reference variable of type Object, you can access only those members defined in class Object.

Correct

6. Given: {code}
What is the result?

You can define an inner class as abstract, which means you can instantiate only concrete subclasses of the abstract inner class. The object referenced by the variable t is an instance of an anonymous subclass of AbstractTest, and the anonymous class overrides the getNum() method to return 22. The variable referenced by f is an instance of an anonymous subclass of Bar, and the anonymous Bar subclass also overrides the getNum() method to return 57. Remember that to create a Bar instance, we need an instance of the enclosing AbstractTest class to tie to the new Bar inner class instance. AbstractTest can't be instantiated because it's abstract, so we created an anonymous subclass (non-abstract) and then used the instance of that anonymous subclass to tie to the new Bar subclass instance.

Correct

7. Given: {code}
Which, inserted independently at line 6, compile and produce the output "spooky"? (Choose all that apply.)

It is the only code that uses the correct inner class instantiation syntax

Not Correct

8. Given: {code}
What is the result?

The "inner" version of class A isn't used because its declaration comes after the instance of class A is created in the go() method.

Not Correct

9. Given: {code}
Which, inserted independently at line 5, produce the output "hi"? (Choose all that apply.)

C is the correct syntax to access an inner class's outer instance method from an initialization block, and G is the correct syntax to access it from a constructor.

Correct

10. Given: {code}
What is the result?

The inner classes are valid, and all the methods (including main()), correctly throw an exception, given that doStuff() throws an exception. The doStuff() in class TimesSquare overrides class Manhattan's doStuff() and produces no output.

Correct

11. Given: {code}
What is the result?

The inner class Gazer has access to Navel's private static and private instance variables.

Not Correct

12. Given: {code}
What is the result?

The inner class Sorter must be declared static to be called from the static method main(). If Sorter had been static, answer E would be correct.

Correct

13. Given: {code}
What is the result?

We’re using a lambda expression to stand in for the Comparator we pass to Arrays.sort(). There is no inner (or outer) class we need to supply; the JDK knows we are supplying a lambda expression that implements the compareTo() method for Comparator because of the type signature of Arrays.sort(). In this case, we are sorting the list in ascending order.