欢迎来到易学考试网 易学考试官网
全部科目 > Java认证考试 > Java认证考试综合练习

单项选择题

有下面代码importjava.util.*;classStudent{intage;Stringname;publicStudent(){}publicStudent(Stringname,intage){this.name=name;this.age=age;}publicinthashCode(){returnname.hashCode()+age;}publicbooleanequals(Objecto){if(o==null)returnfalse;if(o==this)returntrue;if(o.getClass()!=this.getClass())returnfalse;Studentstu=(Student)o;if(stu.name.equals(name)&&stu.age==age)returntrue;elsereturnfalse;}}publicclassTestHashSet{publicstaticvoidmain(Stringargs[]){Setset=newHashSet();Studentstu1=newStudent();Studentstu2=newStudent(“Tom”,18);Studentstu3=newStudent(“Tom”,18);set.add(stu1);set.add(stu2);set.add(stu3);System.out.println(set.size());}}下列说法正确的是()

    A.编译错误
    B.编译正确,运行时异常
    C.编译运行都正确,输出结果为3
    D.编译运行都正确,输出结果为2

点击查看答案&解析

相关考题

  • 单项选择题
    请甄别下列对二叉树的使用和理解正确的是()

    A.二叉树(BinaryTree)是n(n≥0)个结点的有限集
    B.0个结点的树不可以称为二叉树
    C.1个结点的树不可以称为二叉树
    D.二叉树必须有左右结点

  • 单项选择题
    java当中关于二叉树的理解错误的是()

    A.二叉树是每个节点最多有两个子树的树结构
    B.通常子树被称作“左子树”(leftsubtree)和“右子树”(rightsubtree)
    C.二叉树常被用于实现二叉查找树和二叉堆
    D.二叉树的子树有左右之分,次序可以颠倒

  • 单项选择题
    关于hashcode()与equals()描述错误的是哪个()

    A.hashcode()不等,equals()一定也不等
    B.hashcode()相等,equals()可能相等,也可能不等
    C.equals()方法判断相等的两个对象,hashcode()一定相等
    D.equals()方法判断不相等的两个对象,hashcode()一定不相等

微信小程序免费搜题
微信扫一扫,加关注免费搜题

微信扫一扫,加关注免费搜题