单选题public class Person {  private String name, comment;  private int age;  public Person(String n, int a, String c) {  name = n; age = a; comment = c;  }  public boolean equals(Object o) {  if(! (o instanceof Person)) return false;  Person p = (Person)o; 

题目
单选题
public class Person {  private String name, comment;  private int age;  public Person(String n, int a, String c) {  name = n; age = a; comment = c;  }  public boolean equals(Object o) {  if(! (o instanceof Person)) return false;  Person p = (Person)o;  return age == p.age && name.equals(p.name);  }  }  What is the appropriate definition of the hashCode method in class Person?()
A

 return super.hashCode();

B

 return name.hashCode() + age * 7;

C

 return name.hashCode() + comment.hashCode() /2;

D

 return name.hashCode() + comment.hashCode() / 2 - age * 3;


相似考题
更多“public class Person {  private String name, comment;  privat”相关问题
  • 第1题:

    阅读下面代码 abstract class Person { public Person(String n) { name=n; } public______String getDescription(); public String getName() { return name; } private String name; } 在下画线处应填入的修饰符是

    A.static

    B.abstract

    C.protected

    D.final


    正确答案:B

  • 第2题:

    给出下面不完整的类代码,则横线处的语句应该为( )。 class Person { String name,department; int age; public Person (Strings) {name=s;} public Person (String s,int

    A.{name=s;age=a;} public Person (String n,String d,intA){ __________ department=d; } }A)Person (n,A);

    B.this (Person(n,A));

    C.this(n,A);

    D.this(name,age);


    正确答案:C
    解析:本题主要考查在同一个类的不同构造方法中调用该类的其他构造方法需要使用 this(…)的形式,而且必须是在构造方法的第一行调用。这个和普通方法重载调用的方式不同,普通方法可以直接使用方法名加参数来调用,而且调用位置没有限制,因此选项A是不行的,选项B的语法就是错误的,选项D的错误在于在父类型的构造方法被调用前不能引用类的成员。构造方法是一个类对象实例化的开始,因此在构造方法中不能将成员作为参数引用。

  • 第3题:

    为使下列代码正常运行,应该在下画线处填入的选项是( )。 abstract class person{ public Person(String n){ name=n: } Public String getDescription; public String getName{ return name; } private string name; }

    A.static

    B.private

    C.abstract

    D.final


    正确答案:C
    C。【解析】抽象类中的抽象方法可以只声明,定义延迟到其子类。

  • 第4题:

    public class Person {  private String name, comment;  private int age;  public Person(String n, int a, String c) {  name = n; age = a; comment = c;  }  public boolean equals(Object o) {  if(! (o instanceof Person)) return false;  Person p = (Person)o;  return age == p.age && name.equals(p.name);  }  }  What is the appropriate definition of the hashCode method in class Person?() 

    • A、 return super.hashCode();
    • B、 return name.hashCode() + age * 7;
    • C、 return name.hashCode() + comment.hashCode() /2;
    • D、 return name.hashCode() + comment.hashCode() / 2 - age * 3;

    正确答案:B

  • 第5题:

    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”)后程序的输出是哪项?() 

    • A、 smith
    • B、 null
    • C、 编译错误
    • D、 name

    正确答案:C

  • 第6题:

    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?() 

    • A、 The code will compile without changes.
    • B、 The code will compile if public Tree() { Plant(); } is added to the Tree class.
    • C、 The code will compile if public Plant() { Tree(); } is added to the Plant class.
    • D、 The code will compile if public Plant() { this(”fern”); } is added to the Plant class.
    • E、 The code will compile if public Plant() { Plant(”fern”); } is added to the Plant class.

    正确答案:D

  • 第7题:

    单选题
    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”)后程序的输出是哪项?()
    A

     smith,SALES

    B

     null,SALES

    C

     smith,null

    D

     null,null

    E

     编译错误


    正确答案: C
    解析: 暂无解析

  • 第8题:

    单选题
    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”)后程序的输出是哪项?()
    A

     smith

    B

     null

    C

     SALES

    D

     编译错误


    正确答案: C
    解析: 暂无解析

  • 第9题:

    单选题
    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?()
    A

     The equals method does NOT properly override the Object.equals method.

    B

     Compilation fails because the private attribute p.name cannot be accessed in line 5.

    C

     To work correctly with hash-based data structures, this class must also implement the hashCode method.

    D

     When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.


    正确答案: B
    解析: 暂无解析

  • 第10题:

    单选题
    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”)后程序的输出是哪项?()
    A

     smith

    B

     null

    C

     编译错误

    D

     name


    正确答案: D
    解析: 暂无解析

  • 第11题:

    单选题
    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?()
    A

     The code will compile without changes.

    B

     The code will compile if public Tree() { Plant(); } is added to the Tree class.

    C

     The code will compile if public Plant() { Tree(); } is added to the Plant class.

    D

     The code will compile if public Plant() { this(”fern”); } is added to the Plant class.

    E

     The code will compile if public Plant() { Plant(”fern”); } is added to the Plant class.


    正确答案: A
    解析: 暂无解析

  • 第12题:

    单选题
    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”)后程序的输出是哪项?()
    A

     smith,SALES

    B

     null,SALES

    C

     smith,null

    D

     null,null


    正确答案: D
    解析: 暂无解析

  • 第13题:

    给出下列的不完整的类代码,则下列的( )语句可以加到横线处。 class Person{ String name,department; int age public Person(String n){name=n;} public Person(String n,int a){name=n;age=a;} pubilc Person(String n,String d,int a) { _______________ department=d; } }

    A.Person(n,a);

    B.this(Person(n,a));

    C.this(n,a);

    D.this(name,age);


    正确答案:C

  • 第14题:

    Java代码查错

    1.

    abstract class Name {

    private String name;

    public abstract boolean isStupidName(String name) {}

    }

    大侠们,这有何错误?


    正确答案:

     

    错。abstract method 必须以分号结尾,且不带花括号。

  • 第15题:

    给出下列的不完整的类代码,则下列的哪个语句可以加到横线处? class Person { String name,department; int age; public Person( String n ){ name = n;} public Person( String n,int a ) { name = n;age = a;} public Person( String n,String d,int a ) { _____________ department = d; } }

    A.Person(n,a);

    B.this(Person(n,a) );

    C.this(n,a);

    D.this(name,age);


    正确答案:C
    解析:在同一个类的不同构造方法中调用该类的其他构造方法需要使用this(…)的形式,而且必须是在构造方法的第一行调用,这个和普通方法重载调用的方式不同,普通方法可以直接使用方法名加参数来调用,而且调用位置没有限制,因此选项A)是不行的,选项B)的语法就是错误的,选项D)的错误在于在父类型的构造方法被调用前不能引用类的成员。构造方法是一个类对象实例化的开始,因此在构造方法中不能将成员作为参数引用。

  • 第16题:

    public class Person {  private name;  public Person(String name) {  this.name = name;  }  public int hashCode() {  return 420;  }  }  Which is true?() 

    • A、 The time to find the value from HashMap with a Person key depends on the size of the map.
    • B、 Deleting a Person key from a HashMap will delete all map entries for all keys of typePerson.
    • C、 Inserting a second Person object into a HashSet will cause the first Person object to beremoved as a duplicate.
    • D、 The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map.

    正确答案:A

  • 第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”)后程序的输出是哪项?() 

    • A、 smith,SALES
    • B、 null,SALES
    • C、 smith,null
    • D、 null,null

    正确答案:A

  • 第18题:

    public class Person {  private name;  public Person(String name) {  this.name = name;  }  public boolean equals(Object o) {  if( !o instanceof Person ) return false;  Person p = (Person) o;  return p.name.equals(this.name);  }  }  Which is true?() 

    • A、 Compilation fails because the hashCode method is not overridden.
    • B、 A HashSet could contain multiple Person objects with the same name.
    • C、 All Person objects will have the same hash code because the hashCode method is not overridden.
    • D、 If a HashSet contains more than one Person object with name=”Fred”, then removing another person, also with name=”Fred”, will remove them all.

    正确答案:B

  • 第19题:

    单选题
    public class Person {  private name;  public Person(String name) {  this.name = name;  }  public boolean equals(Object o) {  if( !o instanceof Person ) return false;  Person p = (Person) o;  return p.name.equals(this.name);  }  }  Which is true?()
    A

     Compilation fails because the hashCode method is not overridden.

    B

     A HashSet could contain multiple Person objects with the same name.

    C

     All Person objects will have the same hash code because the hashCode method is not overridden.

    D

     If a HashSet contains more than one Person object with name=”Fred”, then removing another person, also with name=”Fred”, will remove them all.


    正确答案: B
    解析: 暂无解析

  • 第20题:

    单选题
    public class Person {  private name;  public Person(String name) {  this.name = name;  }  public int hashCode() {  return 420;  }  }  Which is true?()
    A

     The time to find the value from HashMap with a Person key depends on the size of the map.

    B

     Deleting a Person key from a HashMap will delete all map entries for all keys of typePerson.

    C

     Inserting a second Person object into a HashSet will cause the first Person object to beremoved as a duplicate.

    D

     The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map.


    正确答案: C
    解析: 暂无解析

  • 第21题:

    单选题
    public class Person {  private String name, comment;  private int age;  public Person(String n, int a, String c) {  name = n; age = a; comment = c;  }  public boolean equals(Object o) {  if(! (o instanceof Person)) return false;  Person p = (Person)o;  return age == p.age && name.equals(p.name);  }  }  What is the appropriate definition of the hashCode method in class Person?()
    A

     return super.hashCode();

    B

     return name.hashCode() + age * 7;

    C

     return name.hashCode() + comment.hashCode() /2;

    D

     return name.hashCode() + comment.hashCode() / 2 - age * 3;


    正确答案: D
    解析: 暂无解析

  • 第22题:

    多选题
    public class NamedCounter {  private final String name;  private int count;  public NamedCounter(String name) { this.name = name; }  public String getName() { return name; }  public void increment() { coount++; }  public int getCount() { return count; } public void reset() { count = 0; } }  Which three changes should be made to adapt this class to be used safely by multiple threads? ()
    A

    declare reset() using the synchronized keyword

    B

    declare getName() using the synchronized keyword

    C

    declare getCount() using the synchronized keyword

    D

    declare the constructor using the synchronized keyword

    E

    declare increment() using the synchronized keyword


    正确答案: B,D
    解析: 暂无解析

  • 第23题:

    单选题
    Given the uncompleted code of a class:     class Person {  String name, department;     int age;  public Person(String n){  name = n; }  public Person(String n, int a){  name = n;  age = a;  }  public Person(String n, String d, int a) {  // doing the same as two arguments version of constructor     // including assignment name=n,age=a    department = d;     }     }  Which expression can be added at the "doing the same as..." part of the constructor?()
    A

     Person(n,a);

    B

     this(Person(n,a));

    C

     this(n,a);

    D

     this(name,age);


    正确答案: A
    解析: 在同一个类的不同构造方法中调用该类的其它构造方法需要使用this(…)的形式,而且必须是在构造方法的第一行调用,这个和普通的方法重载调用的方式不同,普通的方法可以直接使用方法名加参数来调用,而且调用位置没有限制,因此答案A是不行的,B的语法就是错误的,D的错误在于在父类型的构造函数被调用前不能引用类的成员。构造方法是一个类对象实例化的起点(虽然严格来说首先执行的并不是构造方法的第一个语句,而是内存的分配),因此在构造方法中不能将成员作为参数引用。

  • 第24题:

    单选题
    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”)后程序的输出是哪项?()
    A

     smith

    B

     null

    C

     SALES

    D

     编译错误


    正确答案: C
    解析: 暂无解析