Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium
Question 30
Question ID: UKOCP42088
What will be the result of compiling and executing TestPoint class?
- package com.udayankhattry.ocp;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- class Point {
- private int x;
- private int y;
- public Point(int x, int y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public String toString() {
- return "Point(" + x + ", " + y + ")";
- }
- }
- public class TestPoint {
- public static void main(String [] args) {
- List<Point> points = new ArrayList<>();
- points.add(new Point(4, 5));
- points.add(new Point(6, 7));
- points.add(new Point(2, 2));
- Collections.sort(points, new Comparator<Point>() {
- @Override
- public int compare(Point o1, Point o2) {
- return o1.x - o2.x;
- }
- });
- System.out.println(points);
- }
- }
- A [Point(2, 2), Point(4, 5), Point(6, 7)]
- B [Point(6, 7), Point(4, 5), Point(2, 2)]
- C [Point(4, 5), Point(6, 7), Point(2, 2)]
- D Compilation error
Reveal correct answer
Correct answer: D
Explanation
UKOCP42088:
x and y are private variables and are accessible within the boundary of Point class.
TestPoint class is outside the boundary of Point class and hence o1.x and o2.x give compilation error.
Make sure to check the accessibility before working with the logic.
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.
