publicclassPet{privateStringname;publicPet(Stringname){this.name=name;}publicvoidspeak(){System.out.print(name);}}publicclassDogextendsPet{publicDog(Stringname){super(name);}publicvoidspeak(){super.speak();System.out.print(Dog”);}}执行代码Petpet=newDog(京巴”);pet.speak();后输出的内容是哪项?()
A.京巴
B.京巴Dog
C.null
D.Dog京巴
第1题:
类Teacher:classTeacher{Stringname;floatsalary;Teacher(Stringname){this.name=name;}Teacher(Stringname,floatsalary){this.name=name;this.salary=salary;}}执行语句Teachert=newTeacher(Tom”,2000.0f);后,字段salary的值是哪一项?()
A.2000.0f
B.0.0f
C.null;
D.2000
第2题:
A.Thecodewillcompilewithoutchanges.
B.ThecodewillcompileifpublicTree(){Plant();}isaddedtotheTreeclass.
C.ThecodewillcompileifpublicPlant(){Tree();}isaddedtothePlantclass.
D.ThecodewillcompileifpublicPlant(){this(”fern”);}isaddedtothePlantclass.
E.ThecodewillcompileifpublicPlant(){Plant(”fern”);}isaddedtothePlantclass.
第3题:
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; }
第4题:
A.TheequalsmethoddoesNOTproperlyoverridetheObject.equalsmethod.
B.Compilationfailsbecausetheprivateattributep.namecannotbeaccessedinline5.
C.Toworkcorrectlywithhash-baseddatastructures,thisclassmustalsoimplementthehashCodemethod.
D.WhenaddingPersonobjectstoajava.util.Setcollection,theequalsmethodinline4willpreventduplicates.
第5题:
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");"
这里显示有错。