Given: 10. class Line { 11. public static class Point { } 12. } 13. 14. class Triangle { 15. // insert code here 16. } Which code, inserted at line 15, creates an instance of the Point class defined in Line?

  1. Point p = new Point();

  2. Line.Point p = new Line.Point();

  3. The Point class cannot be instatiated at line 15

  4. Line 1 = new Line() ; 1.Point p = new 1.Point();


Correct Option: B
Explanation:

To solve this question, the user needs to know how to create an instance of a nested class.

Option A: Point p = new Point(); This option is incorrect because the Point class is a nested static class and must be accessed through the outer class. In this case, the outer class is Line and the correct way to access the nested class is Line.Point.

Option B: Line.Point p = new Line.Point(); This option is correct. Since Point is a nested static class, it can be accessed using the outer class name followed by a dot and the nested class name. This option correctly creates an instance of the Point class defined in Line.

Option C: The Point class cannot be instantiated at line 15. This option is incorrect. The Point class can be instantiated from within the Triangle class, but it must be done using the correct syntax since it is a nested class.

Option D: Line 1 = new Line() ; 1.Point p = new 1.Point(); This option is incorrect because it contains a syntax error. The variable 1 cannot be used as a variable name in Java.

The Answer is: B. Line.Point p = new Line.Point();

Find more quizzes: