Java OCA OCP Practice Question 2339

Question

Given that classes Outer and Test are defined in separate packages and source code files, which code options, when inserted independently at //INSERT CODE HERE, will instantiate class Inner in class Test? (Choose all that apply.).

// Source code-Outer.java 
package mypkg.ocp; 
public class Outer { 
    public static class Inner{} 
} 
// Source code-Test.java 
package mypkg.exams; 
import static mypkg.ocp.Outer.Inner; 
class Test { 
    //INSERT CODE HERE 
} 
  • a Inner inner = new Inner();
  • b Outer.Inner inner = new Outer.Inner();
  • c Outer.Inner inner = new Inner();
  • d Outer.Inner inner = Outer.new Inner();


a

Note

Due to the following static import statement, only the nested static class Inner is visible in class Test:.

import static mypkg.ocp.Outer.Inner;  

Class Outer isn't visible in class Test.

Options (b) and (c) will instantiate class Inner if the following import statement is included in class Test:.

import mypkg.ocp.Outer; 



PreviousNext

Related