Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard
Question 42
Consider the code of Test.java file:
- package com.udayan.oca;
- class Student {
- String name;
- int age;
- Student() {
- Student("James", 25);
- }
- Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
- }
- public class Test {
- public static void main(String[] args) {
- Student s = new Student();
- System.out.println(s.name + ":" + s.age);
- }
- }
What will be the result of compiling and executing Test class?
-
A
null:0
-
B
James:25
-
C
An exception is thrown at runtime
-
D
Compilation error
Reveal correct answer
Correct answer: D
Explanation
A constructor can call another constructor by using this(...) and not the constructor name.
Hence Student("James", 25); 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.
