1. public class Employee { 2. String name; 3. double baseSalary; 4. Employee(String name, double baseSalary) { 5. this.name = name; 6. this.baseSalary = baseSalary; 7. } 8. } And: 1. public class Salesperson extends Employee { 2. double commission; 3. public Salesperson(String name, double baseSalary, 4. double commission) { 5. // insert code here 6. } 7. } Which code, inserted at line 7, completes the Salesperson constructor?()
第1题:
试题六(共15分)
阅读以下说明和Java代码,填补Java代码中的空缺(1)~(6),将解答写在答题纸的对应栏内。
【说明】
己知某公司按周给员工发放工资,其工资系统需记录每名员工的员工号、姓名、工资等信息。其中一些员工是正式的,按年薪分周发放(每年按52周计算);另一些员工是计时工,以小时工资为基准,按每周工作小时数核算发放。
下面是实现该工资系统的Java代码,其中定义了四个类:工资系统类PayRoll,员工类Employee,正式工类Salaried和计时工类Hourly,Salaried和Hourly是Employee的子类。
【Java代码】
abstract class Employee{
protected String name; //员工姓名
protected int empCode; //员工号
protected double salary; //周发放工资
public Employee(int empCode, String name){
this.empCode= empCode;
this.name= name;
}
public double getSalary(){
return this.salary;
}
public abstract void pay();
}
class Salaried (1) Employee{
private double annualSalary;
Salaried(int empCode, String name, double payRate){
super(empCode, name);
this.annualSalary= payRate;
}
public void pay(){
salary= (2) ;//计算正式员工的周发放工资数
System.out.println(this.name+":"+this.salary);
}
}
class Hourly (3) Employee{
private double hourlyPayRate;
private int hours;
Hourly(int empCode, String name, int hours, double payRate){
super(empCode, name);
this.hourlyPayRate= payRate;
this.hows= hours,
}
public void pay(){
salary= (4) ;//计算计时工的周发放工资数
System.out.println(this.name+":"+this.salary);
}
}
public class PayRoll{
private (5) employees[]={
new Salaried(l001,"Zhang San", 58000.00),
//此处省略对其他职工对象的生成
new Hourly(1005,"Li", 12, 50.00)
};
public void pay(Employee e[]){
for (int i=0;i<e.length; i++){
e[i].pay();
}
}
public static void main(String[] args)
{
PayRoll payRoll= new PayRoll();
payRoll.pay( (6) );
double total= 0.0;
for (int i=0;i<payRoll.employees.length; i++){//统计周发放工资总额
total+=payRoll.employees[i].getSalary();
}
System.out.println(total);
}
}
第2题:
public class Employee{ private String name; public Employee(String name){ this.name = name; } public String getName(){ return name; } } public class Manager extends Employee{ private String department; public Manager(String name,String department){ this.department = department; super(name); (应于上一行掉位置) System.out.println(getName()); } } Super的位置是否在方法的首行 执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()
第3题:
Class TestException 1. public class TestException extends Exception { 2. } Class a: 1. public class a { 2. 3. public String sayHello(String name) throws TestException { 4. 5. if(name == null) { 6. throw new TestException(); 7. } 8. 9. return “Hello “+ name; 10. } 11. 12. } A programmer wants to use this code in an application: 45. A a=new A(); 46. System.out.println(a.sayHello(”John”)); Which two are true?()
第4题:
1. public class A { 2. public void doit() { 3. } 4. public String doit() { 5. return “a”; 6. } 7. public double doit(int x) { 8. return 1.0; 9. } 10.} What is the result?()
第5题:
public class Employee{ private String name; public Employee(String name){ this.name = name; } public String getName(){ return name; } } public class Manager extends Employee{ private String department; public Manager(String name,String department){ this.department = department; super(name); System.out.println(getName()); } } 执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()
第6题:
public class Employee{ private String name; public Employee(String name){ this.name = name; } public void display(){ System.out.print(name); } } public class Manager extends Employee{ private String department; public Manager(String name,String department){ super(name); this.department = department; } public void display(){ System.out.println( super.display()+”,”+department); } } 执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()
第7题:
public class Plant { private String name; public Plant(String name) { this.name = name; } public String getName() { return name; } } public class Tree extends Plant { public void growFruit() { } public void dropLeaves() { } } Which is true?()
第8题:
11. class Person { 12. String name = “No name‟; 13. public Person(String nm) { name = nm; } 14. } 15. 16. class Employee extends Person { 17. String emplD = “0000”; 18. public Employee(String id) { empID = id; } 19. } 20. 21. public class EmployeeTest { 22. public static void main(String[] args) { 23. Employee e = new Employee(”4321”); 24. System.out.println(e.empID); 25. } 26. } What is the result?()
第9题:
smith,SALES
null,SALES
smith,null
null,null
编译错误
第10题:
smith
null
编译错误
name
第11题:
class Manager extends Employee{
public int getSalary(double x){}
}
class Manager extends Employee{
public double getSalary(int x,int y){}
}
class Manager extends Employee{
public double getSalary(){}
}
class Manager extends Employee{
public int getSalary(int x,int y){}
}
第12题:
Class a will not compile.
Line 46 can throw the unchecked exception TestException.
Line 45 can throw the unchecked exception TestException.
Line 46 will compile if the enclosing method throws a TestException.
Line 46 will compile if enclosed in a try block, where TestException is caught.
第13题:
类Account中字段声明正确的是?()
第14题:
1. public interface A { 2. public void doSomething(String thing); 3. } 1. public class AImpl implements A { 2. public void doSomething(String msg) { } 3. } 1. public class B { 2. public A doit() { 3. // more code here 4. } 5. 6. public String execute() { 7. // more code here 8. } 9. } 1. public class C extends B { 2. public AImpl doit() { 3. // more code here 4. } 5. 6. public Object execute() { 7. // more code here 8. } 9. } Which statement is true about the classes and interfaces in the exhibit?()
第15题:
1. public class Person { 2. private String name; 3. public Person(String name) { this.name = name; } 4. public boolean equals(Person p) { 5. return p.name.equals(this.name); 6. } 7. } Which is true?()
第16题:
public class Employee{ private String name; public Employee(String name){ this.name = name; } public void display(){ System.out.print(name); } } public class Manager extends Employee{ private String department; public Manager(String name,String department){ super(name); this.department = department; } public void display(){ System.out.println(super.display()+”,”+department); } } 执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()
第17题:
10. abstract public class Employee { 11. protected abstract double getSalesAmount(); 12. public double getCommision() { 13. return getSalesAmount() * 0.15; 14. } 15. } 16. class Sales extends Employee { 17. // insert method here 18. } Which two methods, inserted independently at line 17, correctly complete the Sales class?()
第18题:
我们定义一个Account类来描述银行账户,银行账户有账户名、金额等属性特征,同时有存款、取款等行为特征,下述代码适合描述的是哪项?()
第19题:
1. class super { 2. public float getNum() {return 3.0f;} 3. } 4. 5. public class Sub extends Super { 6. 7. } Which method, placed at line 6, will cause a compiler error?()
第20题:
public class Employee{ private String name; public Employee(String name){ this.name = name; } public String getName(){ return name; } } public class Manager extends Employee{ public Manager(String name){ System.out.println(getName()); } } 执行语句new Manager(“smith”)后程序的输出是哪项?()
第21题:
The code will compile without changes.
The code will compile if public Tree() { Plant(); } is added to the Tree class.
The code will compile if public Plant() { Tree(); } is added to the Plant class.
The code will compile if public Plant() { this(”fern”); } is added to the Plant class.
The code will compile if public Plant() { Plant(”fern”); } is added to the Plant class.
第22题:
public void getNum(){}
public void getNum(double d){}
public float getNum() { return 4.0f; }
public double getNum(float d) { return 4.0d; }
第23题:
An exception is thrown at runtime.
Compilation fails because of an error in line 7.
Compilation fails because of an error in line 4.
Compilation succeeds and no runtime errors with class A occur.