• SCJP_104——题目分析(2)


    3.

    public class IfTest{
        public static void main(String args[]){
            int x=3;
            int y=1;
            if(x=y)
                System.out.println("Not equal");
            else
                System.out.println("Equal");
        }
    }

    what is the result?

    这一题考察的是 if 语句。if 语句的写法如下:

    if (boolean expression)
    {
        statement;
        ...
    }

    所以,括号中必须是一个布尔值,或者是能得到布尔值的表达式

    同时,这一题还考察了 = 与 == 区别

    = :这是赋值符号,比如

    int x = 0;

    这句话定义了一个 int 类型的变量,命名为 x,同时赋值为 0。

    == :这是比较符号,用来比较符号两边的表达式,结果返回一个 boolean 值,比如

    1 == 2;

    这句话返回 false。

    所以正确答案为 compile error(编译错误)


    4.

    public class Foo{
        public static void main(String args[]){
            try{
                return;
            }
            finally{
                System.out.println("Finally");
            }
        }
    }

    what is the result?
    A. print out nothing
    B. print out "Finally"
    C. compile error

    这一题目考察的是 try—catch—finally 的用法。

    try{} 负责抛出异常,catch(){} 负责捕捉异常。而 finally{} 代码块,不管有没有抛出异常,总是会被执行到。

    注意:只有一种情况,fanally{} 不会被执行。就是程序终止的时候,比如:

     1 public class Test
     2 {
     3     public static String output = "";
     4 
     5     public static void foo(int i)
     6     {
     7         try
     8         {
     9             if (i == 1)
    10             {
    11                 throw new Exception();
    12             }
    13             output += "1";
    14         } catch (Exception e)
    15         {
    16             output += "2";
    17             System.exit(0);            // 程序被终止了,下面的代码全部不会执行
    18         } finally
    19         {
    20             output += "3";
    21         }
    22         output += "4";
    23     }
    24 
    25     public static void main(String args[])
    26     {
    27         foo(0);    // i = 134
    28         System.out.println(output);
    29         foo(1);    // i = 134234
    30         System.out.println(output);
    31     }
    32 }

    所以,正确答案为 B


    public class Test
    {
        public static String output = "";
    
        public static void foo(int i)
        {
            try
            {
                if (i == 1)
                {
                    throw new Exception();
                }
                output += "1";
            } catch (Exception e)
            {
                output += "2";            
            } finally
            {
                output += "3";
            }
            output += "4";
        }
    
        public static void main(String args[])
        {
            foo(0);
            foo(1);
            
            // (24)
        }
    }

    what is the value of output at line 24?

    这里也是考察 try-catch-finally

    在这里,catch(){} 代码块中有一个 return 语句。说明了 return 语句后面的代码不会被执行到。

    但是,这里有一个 finally{} 代码块,所以,在执行 return 语句之前会先执行 finally{} 代码块,之后才会执行 return 语句

    所以程序执行到

    foo(0);

    时,output = "134",程序执行到

    foo(1);

    时,output = "13423"

    所以,答案为 "13423"

  • 相关阅读:
    VSCODE打开一个文件,另一个文件就关闭的问题的解决方法
    elementui的el-tree第一次加载无法展开和选中的问题
    Java线程知识:二、锁的简单使用
    “商家参数格式有误”应用切微信H5支付完美解决方案
    git 基础操作,公私钥认证/ssh公私钥登录
    Python数据分析之亚马逊股价
    Python分析6000家破产IT公司
    Python数据分析之股票数据
    Python数据分析之全球人口数据
    Vue 面试重点真题演练
  • 原文地址:https://www.cnblogs.com/owenbeta/p/3702252.html
Copyright © 2020-2023  润新知