Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Hard
Question 16
Consider the code of Test.java file:
- package com.udayan.ocp;
- class Point {
- private int x;
- private int y;
- Point(){
- Point(10, 20);
- }
- Point(int x, int y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public String toString() {
- return "Point{" + x + ", " + y + "}";
- }
- }
- public class Test {
- public static void main(String[] args) {
- Point p = new Point();
- System.out.println(p);
- }
- }
What will be the result of compiling and executing Test class?
- A Point{10, 20}
- B Point{0, 0}
- C Compilation error in Test class
- D Compilation error in Point class
Reveal correct answer
Correct answer: D
Explanation
To call another constructor of the class use this(10, 20);
No-argument constructor of Point class causes compilation error.
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.
