Answer is D.
Employee class is ok.
In the test class, as informed by Snover, the error is thrown because an object is instantiated without respecting the arguments presented in the constructor.
To test, Employee Class:
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) {
this(name, age);
setSalary(salary);
}
public void setName(String name){this.name=name;}
public void setAge(int age){this.age=age;}
public void setSalary(int salary){this.salary=salary;}
public void printDetails() {
System.out.println(name + " : " + salary);
}
}
To test, Test Class:
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee(); // Error
Employee e2 = new Employee("Jack", 50);
Employee e3 = new Employee("Chloe", 40, 5000);
e1.printDetails();
e2.printDetails();
e3.printDetails();
}
}
The letter D is correct, because in the Test class the variable e1 was instantiated with an empty constructor, but in the Employee class there is no empty constructor, so a compilation error occurs.
upvoted 1 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
MPignaProTech
2 months, 3 weeks agocarloswork
2 years, 2 months agocarloswork
2 years, 2 months agoiSnover
2 years, 3 months ago