Which code, inserted at line 16, correctly retrieves a local instance of a Point object?

Given:

class Line {
public class Point {
public int x, y;
}

public Point getPoint() {
return new Point();
}
}

class Triangle {
public Triangle() {
// insert code here
}
}

Which code, inserted at line 16, correctly retrieves a local instance of a Point object?

Given:

class Line {
public class Point {
public int x, y;
}

public Point getPoint() {
return new Point();
}
}

class Triangle {
public Triangle() {
// insert code here
}
}

Which code, inserted at line 16, correctly retrieves a local instance of a Point object?

A.
Point p = Line.getPoint();

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

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

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

Explanation:
A:
Main.java:13: cannot find symbol
symbol : class Point
location: class Triangle
Point p = Line.getPoint();
^
Main.java:13: non-static method getPoint() cannot be referenced from a static context
Point p = Line.getPoint();
^
2 errors

B:
Main.java:13: non-static method getPoint() cannot be referenced from a static context
Line.Point p = Line.getPoint();
^
1 error

C:
Main.java:13: cannot find symbol
symbol : class Point
location: class Triangle
Point p = (new Line()).getPoint();
^
1 error

D:
The program compiled successfully



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Alvaro

Alvaro

Option D is the correct answer. Option A is false because class “Point” will not be recognized by itself, it must be referenced using its father’s name before. Option B is false because method getPoint from class “Line” can’t be called statically. Option C is false because the same reason of option A.