class Sock {  String size;  String color;  public boolean eq

题目
多选题
class Sock {  String size;  String color;  public boolean equals(Object o) {  Sock s = (Sock) o;  return color.equals(s.color);   }  // insert code here  }  哪两个满足 hashCode 的约定?()
A

public int hashCode() { return 343; }

B

public int hashCode() { return size.hashCode (); }

C

public int hashCode() { return color.hashCode (); }

D

public int hashCode() { return (int) (Math.random() * 1000);

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

阅读下面程序 public class My Val{ public static void main(String args[]){ My Val m=new My Val(); m. amethod(); } public void amethod(){ boolean b[]=new Boolean[5]; } } 程序编译或运行结果是

A.1

B.null

C.

D.编译不能过


正确答案:C
解析:编译能通过,但不在控制台输出任何信息。程序只是实例化了一个布尔类型的数组,且由于此数组为局部变量,不会自动初始化,故其中值都为null。

第2题:

下列代码的编译或执行结果是( )。 public class Myval{ public static void main(string args[]){ MyVal m=new MyVal; aMethod; } public void aMethod{ boolean b[]=new Boolean[5]; System.OUt.println(b[0]); } }

A.1

B.null

C.0

D.编译错误


正确答案:A
A。【解析】boolean类型的变量值只有ture或false,b[0]的默认初始值为false。

第3题:

【Java代码】

import Java.util.ArrayList;

import java.util.List;

(1) class AbstractFile{

protected String name;

public void printName(){System.out.println(name);}

public abstract boolean addChild(AbstractFile file);

public abstract boolean removeChild(AbstractF ile file);

public abstract List<AbstractFile> getChildren();

class File extends AbstractFile{

public File(String name){this.name=name;}

public boolean addChild(AbstractFile file){return false;}

public boolean removeChild(AbstractFile file){return false;}

public List<AbstractFile> getChildren(){return (2) ;}

class Folder extends AbstractFile{

private List <AbslractFile> childList;

public Folder(String name){

this.name=name;

this.childList=new ArrayList<AbstractFile>();

public boolean addChild(AbstractFile file) { return childList.add(file);}

public boolean removeChild(AbstractFile file){return childList.remove(file);}

public (3) <AbstractFile> getChildren(){return (4) ;}

public class Client{

public static void main(String[] args){

//构造一个树形的文件/目录结构

AbstractFile rootFolder= new Folder("c:\\ ");

AbstractFile compositeFolder=new Folder("composite");

AbstractFile windowsFolder=new Folder("windows");

AbstractFile file=new File("TestComposite.java");

rootFolder.addChild(compositeFolder) ;

rootFolder.addChild(windowsFolder);

compositeFolder.addChild(file) ;

//打印目录文件树

printTree(rootFolder);

private static void printTree(AbslractFile ifile){

ifile.printName();

List <AbslractFile> children=ifile.getChildreno:

if(children==null) return;

for (AbstractFile file:children) {

(5) ;

}

该程序运行后输出结果为:

c:\

composite

TestComposite.java

Windows


正确答案:
(1)abstract
(2)null
(3)List
(4)childList
(5)printTree(file)

第4题:

Which two allow the class Thing to be instantiated using new Thing()?

  • A、 public class Thing { }
  • B、 public class Thing { public Thing() {} }
  • C、 public class Thing { public Thing(void) {} }
  • D、 public class Thing { public Thing(String s) {} }
  • E、 public class Thing { public void Thing() {} public Thing(String s) {} }

正确答案:A,B

第5题:

Which declarations will allow a class to be started as a standalone program?()  

  • A、public void main(String args[])
  • B、public void static main(String args[])
  • C、public static main(String[] argv)
  • D、final public static void main(String [] array)
  • E、public static void main(String args[])

正确答案:D,E

第6题:

Java代码查错

1.

abstract class Name {

private String name;

public abstract boolean isStupidName(String name) {}

}

大侠们,这有何错误?


正确答案:

 

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

第7题:

试题五(共 15 分)阅读以下说明和 Java 程序,填补代码中的空缺,将解答填入答题纸的对应栏内。【说明】以下 Jave 代码实现一个简单客户关系管理系统(CrM) 中通过工厂 (Customerrfactory )对象来创建客户(Customer) 对象的功能。客户分为创建成功的客户 (realCustomer) 和空客户(NullCustomer) 。空客户对象是当不满足特定条件时创建或获取的对象。类间关系如图 5-1 所示。【Java 代码】Abstract class Customer﹛Protected String name;()boolean isNil()()String getName();﹜ Class realCustomer ()Customer﹛Public realCustomer(String name )﹛ return false; ﹜﹜ Class NullCustomer()Customer﹛Public String getName()﹛ return ″Not Available in Customer Database″; ﹜Public boolean isNil()﹛ return true; ﹜﹜ class Customerfactory {public String[] names = {"rob","Joe","Julie"};public Customer getCustomer(String name) {for (int i = 0; i < names.length;i++) {if (names[i].())﹛return new realCusωmer(name);﹜﹜return ()﹜﹜ Public class CrM﹛Public viod get Customer()﹛Customerfactory()Customer customer1-cf.getCustomer(″rob″);Customer customer2=cf.getCustomer(″rob″);Customer customer3= cf.getCustomer(″Julie″);Customer customer4= cf.getCustomer(″Laura″);System.out.println(″customer1.getName());System.out.println(″customer2getName());System.out.println(″customer3.getName());System.out.println(″customer4.getName());﹜ Public static viod main (String[]arge)﹛CrM crm =new CrM();Crm,getCustomer();﹜﹜/*程序输出为:CustomerrobNot Available in Customer DatabaseJulieNot Available in Customer Datable*/int main()﹛CrM*crs=newCrM();Crs->getCustomer();Crs->getCustomer();Delete crs;return();﹜/*程序输出为:CustomerrobNot Available ini Customer DatabaseJulieNot Available in Customer Database


答案:
解析:
1)public abstract2) public abstract3)extends4)extends5)equals(name)6)new Null Customer()7) cf=New CustomerFactory();
【解析】

本题考察Java程序设计客户关系管理系统。1)public abstract 定义可访问方法2) public abstract3)extends 继承Customer类4)extends5)equals(name) 判断名字是否在数组集合内6)new Null Customer() 当不满足条件时创建一个空对象7) cf=New CustomerFactory(); 实例化对象cf

第8题:

You have recently created a serializable class named Vehicle.The class is shown below:[Serializable]public class Vehicle{public string VIN;public string Make;public string Model;public string Year;}You are planning to create a custom formatter class to control the formatting of Vehicle objects when they are serialized.You need to ensure that is achieved with as little development effort as possible.What should you do?()

A.

B.

C.

D.


参考答案:D

第9题:

Which statements concerning the following code are true?()   class a {   public a() {}   public a(int i) { this(); }   }   class b extends a {   public boolean b(String msg) { return false; }   }   class c extends b  {  private c() { super(); }   public c(String msg) { this(); }   public c(int i) {}   }  

  • A、The code will fail to compile.
  • B、The constructor in a that takes an int as an argument will never be called as a result of constructing an     object of class b or c.
  • C、Class c has three constructors.
  • D、Objects of class b cannot be constructed.
  • E、At most one of the constructors of each class is called as a result of constructing an object of class c.

正确答案:B,C

第10题:

程序员正在进行一个项目,必须实现equals方法与所给的hashCode方法协调运行:()  public int hashCode() {   return (size.hashCode() + color.hashCode()) * 17;   }   哪一个equals方法支持此目标?()  

  • A、 无法确定
  • B、 public boolean equals(Object o) {  Sock s = (Sock) o;return size.equals(s.size);  } 
  • C、 public boolean equals(Object o) {  Sock s = (Sock) o;return color.equals(s.color); }
  • D、 public boolean equals(Object o) {  Sock s = (Sock) o;return size.equals(s.size) &&color.equals(s.color); }

正确答案:D

更多相关问题