【282天】我爱刷题系列(41)

叨叨两句

  1. 之后早上就作,必定要保证11点上床睡觉java

牛客网——java专项练习021

1

下面程序的输出是什么?spa

package algorithms.com.guan.javajicu;
public class TestDemo
{线程

public static String output = ””;
public static void foo(inti)
{
    try
    {
        if (i == 1)
        {
            throw new Exception();
        }
    }
    catch (Exception e)
    {
        output += “2”;
        return ;
    } finally
    {
        output += “3”;
    }
    output += “4”;
}
public static void main(String[] args)
{
    foo(0);
    foo(1);
    System.out.println(output);
}

}code

正确答案: Bblog

  1. 342继承

  2. 3423接口

  3. 34234rem

  4. 323it

首先是foo(0),在try代码块中未抛出异常,finally是不管是否抛出异常一定执行的语句,
因此 output += “3”;而后是 output += “4”;
执行foo(1)的时候,try代码块抛出异常,进入catch代码块,output += “2”;
前面说过finally是必执行的,即便return也会执行output += “3”
因为catch代码块中有return语句,最后一个output += “4”不会执行。
因此结果是3423
谁说finally块必须执行?不服来辩
try-catch-finally块中,finally块在如下几种状况将不会执行。
(1)finally块中发生了异常。
(2)程序所在线程死亡。
(3)在前面的代码中用了System.exit();
(4)关闭了CPU

2

int i=5;
int s=(i++)+(++i)+(i--)+(--i);
s=( )//s 的值是什么?
正确答案: Eio

  1. 28

  2. 25

  3. 21

  4. 26

  5. 24

  6. 23

i++是先取值再加,因此第一个括号里表达式值为5,出了括号后i的值为6
++i 是先加再取值,因此第二个括号里表达式的值至关于6+1以后取值为7,出了括号后i的值为7
i--是先取值再减,因此第三个括号里表达式值为7,出了括号后i的值为6
--i是先减再取值,因此第四个括号里表达式的值至关于6-1以后取值为5,出了括号后i的值为5
综上s=5+7+7+5=24,答案为E

3

下面哪些类实现或继承了 Collection 接口?
正确答案: B C

  1. HashMap

  2. ArrayList

  3. Vector

  4. Iterator