Which is the result?

Given the code fragment:

Test.java

Which is the result?

Given the code fragment:

Test.java

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.



Leave a Reply 5

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


renko

renko

Answer: D
package q025;

public 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(salary);
this(name, age);
}
//getter and setter methods for attributes go here
public void printDetails() {
System.out.println(name +” : “+ age +” : “+ salary);
}

}
package q025;

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();
}

}
Output:
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The constructor Employee() is undefined

at q025.Test.main(Test.java:6)

Mick B

Mick B

Class employee has a call to the this keyword in its second constructor below a call to a method.
this or super must be then the first line of constructor.
as this is called second it also will not compile.

So strictly speaking neither file will compile.
try compiling them separately for proof ( use command line).

Correct answer is E

renko

renko

Indeed, E is the correct answer.

rfvc

rfvc

the correct answer is E – both fail .

Employee class fails because this or super must be then the first line of constructor.

Test fails in line Employee e1 = new Employee(); because employee class doesn’t have a Constructor empty. If you add a constructor with parameter and don’t make a constructor empty jvm doesn’t add the default constructor.

in the Employee class should have public Employee(){} and then only Employee class should fails.