Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Easy

Question 30

class Learner {

    String name;

    int age;


    Learner() {

        this("John", 20);

    }


    Learner(String name, int age) {

        this.name = name;

        this.age = age;

    }

}


public class LearnerTest {

    public static void main(String[] args) {

        Learner learner = new Learner();

        System.out.println(learner.name + ":" + learner.age);

    }

}

What will be the result of compiling and executing the LearnerTest class?

  • A

    null:0

  • B

    Compilation error

  • C

    John:20

  • D

    An exception is thrown at runtime

Reveal correct answer

Correct answer: C

Explanation

The Learner constructor correctly calls another constructor using this(...), initializing the name and age fields to "John" and 20 respectively.

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.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need