Which is the result?

Given the code fragment:


Which is the result?
A. Compilation fails in the Employee class.
B. null: 0: 0
Jack : 50 : 0
Chloe : 40 : 5000
C. null 0:0
Jack : 50 : 2000
Chloe : 40 : 5000
D. Compilation fails in the Test class.
E. Both the Employee class and the test class fail to compile.

Download Printable PDF. VALID exam to help you PASS.

10 thoughts on “Which is the result?

  1. -this() needs to be the first statement of a constructor
    -implicit default constructor is no longer provided due to other constructors implemented

  2. E (errors for “set” methods can be ignored):

    Test39.java:7: error: cannot find symbol
    setName(name);
    ^
    symbol: method setName(String)
    location: class Employee
    Test39.java:8: error: cannot find symbol
    setAge(age);
    ^
    symbol: method setAge(int)
    location: class Employee
    Test39.java:9: error: cannot find symbol
    setSalary (2000);
    ^
    symbol: method setSalary(int)
    location: class Employee
    Test39.java:12: error: cannot find symbol
    setSalary(salary);
    ^
    symbol: method setSalary(int)
    location: class Employee
    Test39.java:13: error: call to this must be first statement in constructor
    this(name,age);
    ^
    Test39.java:21: error: no suitable constructor found for Employee()
    Employee e1 = new Employee ();
    ^
    constructor Employee.Employee(String,int,int) is not applicable
    (actual and formal argument lists differ in length)
    constructor Employee.Employee(String,int) is not applicable
    (actual and formal argument lists differ in length)
    6 errors

  3. Answer E:

    In Employee class, and in the second constructor, compilation fails because the this() keyword must be the first statement in the constructor body.

    In the Test class, compilation fails because there is an attempt to initialize an Employee instance with no-args. Since we already have constructors in the Employee class, and since there is no no-args constructor defined explicitly, JVM will not supply the class with a default no-args constructor, leading the compilation to fail.

  4. Answer is E

    class Employee{
    private String name;
    private int age;
    private int salary;

    public Employee(String name, int age){
    setName(name);
    setAge(age);
    setSalary(2000);
    }

    public Employee(String name, int age, int salary){
    setSalary(2000);
    this(name, age);
    }

    public void printDetails(){
    System.out.println(name + ” : ” + age + ” : ” + salary);
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }

    public int getSalary() {
    return salary;
    }

    public void setSalary(int salary) {
    this.salary = salary;
    }

    }

    public class Test {

    public static void main (String[] args){
    Employee e1 = new Employee();
    Employee e2 = new Employee(“Jack”, 50);
    Employee e3 = new Employee(“Chloe”, 40, 5000);
    e1.printDetails();
    e2.printDetails();
    e3.printDetails();
    }
    }

  5. this(name, age) is a constructor and a constructor call must be the first statement in a constructor. Therefore, the Employee class gives compilation fail and because of that Test class gives compilation fails as “Constructor call must be the first statement in a constructor” as well
    Answer is E

    1. Test fails because there is no “Suitable constructor with no parameters in Employee”. And yes I also concluded the answer is E

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.