Multiple choice technology programming languages

Given: 10. class Line { 11. public class Point { public int x,y; } 12. public Point getPoint() { return new Point(); } 13. } 14. class Triangle { 15. public Triangle() { 16. // insert code here 17. } 18. } Which code, inserted at line 16, correctly retrieves a local instance of a Point object?

  1. Point p = Line.getPoint();

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

  3. Point p = (new Line()).getPoint();

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

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

getPoint() is an instance method of class Line, so you need a Line instance to call it. Options A and B try to call it statically on the class itself, which is invalid. Option C creates the instance correctly but fails to specify the full type Line.Point for the inner class. Option D correctly creates a Line instance and properly declares the variable with the inner class type Line.Point.