2021-09-05错题

场景:

在练习


错题

错题1描述:

When is the text “Hi there” displayed?

public class StaticTest
{
    static
    {
        System.out.println("Hi there");
    }
 
    public void print()
    {
        System.out.println("Hello");
    }
 
    public static void main(String args[])
    {
        StaticTest st1 = new StaticTest();
        st1.print();
        StaticTest st2 = new StaticTest();
        st2.print();
    }
}
选项

A.Never.

B.Each time a new object of type StaticTest is created.

C.Once when the class is loaded into the Java virtual machine.

D.Only when the main() method is executed.

原因分析:

类中静态语句块仅在类加载时被执行一次
A.从不
B.每次都会创建一个 StaticTest 类型的新对象
C. 一旦将类加载到 Java 虚拟机中
D.仅当 main() 方法被执行时。

答案: C


错题2描述:

下列说法正确的是()


A.WebLogic中开发消息Bean的non-persistent 方式可以保证消息的可靠

B.EJB容器发生错误,non-persistent方式下JMS容器仍然会将消息发送

C.EJB容器发生错误,persistent方式下JMS容器仍然会将消息发送

D.EJB容器发生错误,两种方式下JMS容器仍会在MDB可用的时候将消息发送
 

原因分析:

选C。
weblogic中开发消息Bean时的persistent与non-persisten的差别:

persistent方式的MDB可以保证消息传递的可靠性,也就是如果EJB容器出现问题而JMS服务器依然会将消息在此MDB可用的时候发送过来。
non-persistent方式的消息将被丢弃。

答案:C


错题3描述:

以下代码执行的结果是多少()?

 public class Demo {
 public static void main(String[] args) {
 //集合数组
  Collection<?>[] collections = 
  {new HashSet<String>(), new ArrayList<String>(), 
  new HashMap<String, String>().values()};
  //自定义类
      Super subToSuper = new Sub();
     for(Collection<?> collection: collections) {
  System.out.println(subToSuper.getType(collection));
	}
}
abstract static class Super {
    public static String getType(Collection<?> collection) {//顺序执行优先调用
        return “Super:collection”;
}
public static String getType(List<?> list) {
        return “Super:list”;
}
public String getType(ArrayList<?> list) {
        return “Super:arrayList”;
}
public static String getType(Set<?> set) {
        return “Super:set”;
}
public String getType(HashSet<?> set) {
        return “Super:hashSet”;
}
}
static class Sub extends Super {
    public static String getType(Collection<?> collection) {
            return "Sub"; }
}
}
 
选项	

A. Sub:collection
	Sub:collection
	Sub:collection

B.	Sub:hashSet
	Sub:arrayList
	Sub:collection

C.  Super:collection
	Super:collection
	Super:collection

D.  Super:hashSet
	Super:arrayList
	Super:collection

原因分析:

考察点
1.方法重载,根据形参列表(这里hash的value集合也属于List也属于Collection)
2.多态对象如果父类的方法被static修饰,子类不会重写,所以会调用父类的
3.形参列表都符合,则谁在前则调用谁

如果去掉全部static
输出结果为:

Sub
Sub
Sub

答案:C


错题4描述:

关于equals和hashCode描述正确的是 ()


A.两个obj,如果equals()相等,hashCode()一定相等(符合代码规范的情况下)

B.两个obj,如果hashCode()相等,equals()不一定相等

C.两个不同的obj, hashCode()可能相等

D.其他都不对


原因分析:

两个对象的equals比较为true,那么他们的HashCode一定相同,
反之,两个对象的HashCode相同不一满足equals比较为true,
解释一下为什么,这是因为HashCode采用的其实是一种杂凑算法,
当然,这也是Java认为最有效的算法,既然是凑出来的那么就有一种情况就是两个不同的对象返回相同的hashCode,并且这种算法越糟糕返回相同的概率越大

答案:A B C


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

关于我成为程序员这档事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值