niusouti.com

publicclassPerson{2.privateStringname;3.publicPerson(Stringname){this.name=name;}4.publicbooleanequals(Personp){5.returnp.name.equals(this.name);6.}7.}Whichistrue?()A.TheequalsmethoddoesNOTproperlyoverridetheObject.equalsmethod.B.Compilationfailsbecauseth

题目
publicclassPerson{2.privateStringname;3.publicPerson(Stringname){this.name=name;}4.publicbooleanequals(Personp){5.returnp.name.equals(this.name);6.}7.}Whichistrue?()

A.TheequalsmethoddoesNOTproperlyoverridetheObject.equalsmethod.

B.Compilationfailsbecausetheprivateattributep.namecannotbeaccessedinline5.

C.Toworkcorrectlywithhash-baseddatastructures,thisclassmustalsoimplementthehashCodemethod.

D.WhenaddingPersonobjectstoajava.util.Setcollection,theequalsmethodinline4willpreventduplicates.


相似考题
更多“publicclassPerson{2.privateStringname;3.publicPerson(Stringname){this.name=name;}4.publicbooleanequals(Personp){5.returnp.name.equals(this.name);6.}7.}Whichistrue?() ”相关问题
  • 第1题:

    Giventhismethodinaclass:publicStringtoString(){StringBufferbuffer=newStringBuffer();buffer.append(?<?);buffer.append(this.name);buffer.append(?>?);returnbuffer.toString();}Whichistrue?()

    A.ThiscodeisNOTthread-safe.

    B.TheprogrammercanreplaceStringBufferwithStringBuilderwithnootherchanges.

    C.ThiscodewillperformwellandconvertingthecodetouseStringBuilderwillnotenhancetheperformance.

    D.Thiscodewillperformpoorly.Forbetterperformance,thecodeshouldberewritten:return“<“+this.name+“>”;


    参考答案:B

  • 第2题:

    publicclassPerson{privatename;publicPerson(Stringname){this.name=name;}publicinthashCode(){return420;}}Whichistrue?()

    A.ThetimetofindthevaluefromHashMapwithaPersonkeydependsonthesizeofthemap.

    B.DeletingaPersonkeyfromaHashMapwilldeleteallmapentriesforallkeysoftypePerson.

    C.InsertingasecondPersonobjectintoaHashSetwillcausethefirstPersonobjecttoberemovedasaduplicate.

    D.ThetimetodeterminewhetheraPersonobjectiscontainedinaHashSetisconstantanddoesNOTdependonthesizeofthemap.


    参考答案:A

  • 第3题:

    Person p = new Person(“张三”,23);这条语句会调用下列哪个构造方法给属性进行初始化()

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


    答案:B
    解析:创建对象时会找到匹配的构造方法给属性进行初始化,由于Person p = new Person(“张三”,23);这条语句中有两个参数,而且第1个参数是String类型的,第2个参数是int类型的,因此会调用B选项中的构造方法。

  • 第4题:

    publicclassPlant{privateStringname;publicPlant(Stringname){this.name=name;}publicStringgetName(){returnname;}}publicclassTreeextendsPlant{publicvoidgrowFruit(){}publicvoiddropLeaves(){}}Whichistrue?()

    A.Thecodewillcompilewithoutchanges.

    B.ThecodewillcompileifpublicTree(){Plant();}isaddedtotheTreeclass.

    C.ThecodewillcompileifpublicPlant(){Tree();}isaddedtothePlantclass.

    D.ThecodewillcompileifpublicPlant(){this(”fern”);}isaddedtothePlantclass.

    E.ThecodewillcompileifpublicPlant(){Plant(”fern”);}isaddedtothePlantclass.


    参考答案:D

  • 第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");"

    这里显示有错。