2000.0f
0.0f
null;
2000
第1题:
interface Playable {
void play();
}
interface Bounceable {
void play();
}
interface Rollable extends Playable, Bounceable {
Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
private String name;
public String getName() {
return name;
}
public Ball(String name) {
this.name = name;
}
public void play() {
ball = new Ball("Football");
System.out.println(ball.getName());
}
}
这个错误不容易发现。
错。"interface Rollable extends Playable, Bounceable"没有问题。interface 可继承多个
interfaces,所以这里没错。问题出在interface Rollable 里的"Ball ball = new Ball("PingPang");"。
任何在interface 里声明的interface variable (接口变量,也可称成员变量),默认为public static
final。也就是说"Ball ball = new Ball("PingPang");"实际上是"public static final Ball ball = new
Ball("PingPang");"。在Ball 类的Play()方法中,"ball = new Ball("Football");"改变了ball 的
reference,而这里的ball 来自Rollable interface,Rollable interface 里的ball 是public static final
的,final 的object 是不能被改变reference 的。因此编译器将在"ball = new Ball("Football");"
这里显示有错。
第2题:
试题六(共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);
}
}
第3题:
执行以下代码后,下面哪些描述是正确的() public class Student{ private String name = “Jema”; public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public static void main(String[] args){ Student s; System.out.println(s.getName()); } }
第4题:
类Student代码如下: class Student{ String name; int age; Student(String nm){ name = nm; } } 执行语句Student stu = new Student()后,字段age的值是哪项?()
第5题:
public class Pet{ private String name; public Pet(String name){ this.name = name; } public void speak(){ System.out.print(name); } } public class Dog extends Pet{ public Dog(String name){ super(name); } public void speak(){ super.speak(); System.out.print(“ Dog ”); } } 执行代码 Pet pet = new Dog(“京巴”); pet.speak(); 后输出的内容是哪项?()
第6题:
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”)后程序的输出是哪项?()
第7题:
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”)后程序的输出是哪项?()
第8题:
2000.0f
0.0f
null;
2000
第9题:
smith
null
SALES
编译错误
第10题:
smith,SALES
null,SALES
smith,null
null,null
第11题:
a teacher
a teacher has
that of a teacher does
that of a teacher
第12题:
输出null
第10行编译报错
第11行编译报错
输出Jema
第13题:
A.public Person(){}
B.public Person(String name,int age) { this.name = name; this.age = age; }
C.public Person(int age,String name) { this.age = age; this.name = name; }
D.public Person(String name) { this.name = name; }
第14题:
第15题:
类Teacher: class Teacher{ String name; float salary; Teacher(String name){ this.name = name; } Teacher(String name,float salary){ this.name = name; this.salary = salary; } } 执行语句Teacher t = new Teacher(“Tom”,2000.0f);后,字段salary的值是哪一项?()
第16题:
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”)后程序的输出是哪项?()
第17题:
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”)后程序的输出是哪项?()
第18题:
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”)后程序的输出是哪项?()
第19题:
a teacher
a teacher has
that of a teacher does
that of a teacher
第20题:
smith,SALES
null,SALES
smith,null
null,null
编译错误
第21题:
0
null
false
编译错误
第22题:
smith
null
编译错误
name
第23题:
京巴
京巴 Dog
null
Dog京巴
第24题:
smith
null
SALES
编译错误