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.
hi did you take the oca?
Option E becoz constructor call this is first line and default construction not defined.
-this() needs to be the first statement of a constructor
-implicit default constructor is no longer provided due to other constructors implemented
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
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.
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();
}
}
E
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
Test fails because there is no “Suitable constructor with no parameters in Employee”. And yes I also concluded the answer is E
D