Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 63
Question ID: UKOCP87887
Given code:
- package com.udayankhattry.ocp;
- class A {
- public void someMethod(final String name) {
- /*INSERT*/ {
- void print() {
- System.out.println("Hello " + name);
- }
- }
- new B().print();
- }
- }
- public class Test {
- public static void main(String[] args) {
- new A().someMethod("World!");
- }
- }
Which of the following options can replace /*INSERT*/ such that on executing Test class, "Hello World!" is displayed in the output?
Select 2 options.
- A public class B
- B protected class B
- C class B
- D private class B
- E final class B
- F abstract class B
Reveal correct answers
Correct answers: C, E
Explanation
UKOCP87887:
Method-local inner classes cannot be defined using explicit access modifiers (public, protected and private) but non-access modifiers: final and abstract can be used with method-local inner class.
In this case, abstract is also not possible as new B() is used.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
