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{} 不会被执行。就是程序终止的时候,比如:

 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";
System.exit(0); // 程序被终止了,下面的代码全部不会执行
} finally
{
output += "3";
}
output += "4";
} public static void main(String args[])
{
foo(0); // i = 134
System.out.println(output);
foo(1); // i = 134234
System.out.println(output);
}
}

所以,正确答案为 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"

SCJP_104——题目分析(2)的更多相关文章

  1. SCJP_104——题目分析(5)

    18. public class Test { public static void add3(Integer i) { int val=i.intvalue(); val+=3; i=new Int ...

  2. SCJP_104——题目分析(1)

    1.1) public class ReturnIt{2) returnType methodA(byte x, double y){3) return (short)x/y*2;4) }5) }wh ...

  3. SCJP_104——题目分析(4)

    14. which three are valid declaraction of a float? ADFA. float foo=-1; B. float foo=1.0; C. float fo ...

  4. SCJP_104——题目分析(3)

    11. what is reserved words in java?A. run B. default C. implement D. import Java 中,给标识符取名的时候,不能使用关键字 ...

  5. SCTF 2014 pwn题目分析

    因为最近要去做ctf比赛的这一块所以就针对性的分析一下近些年的各大比赛的PWN题目.主防项目目前先搁置起来了,等比赛打完再去搞吧. 这次分析的是去年的SCTF的赛题,是我的学长们出的题,个人感觉还是很 ...

  6. 路由器漏洞复现分析第三弹:DVRF INTRO题目分析

    这个项目的目的是来帮助人们学习X86_64之外其他架构环境,同时还帮助人们探索路由器固件里面的奥秘. 本文通过练习DVRF 中INTRO 部分的题目来学习下MIPS 结构下的各种内存攻击. DVRF: ...

  7. 二分查找总结及部分Lintcode题目分析 2

    Search in a big sorted array,这个比之前的二分法模板多了一个很不同的特性,就是无法知道一个重要的条件end值,也是题目中强调的重点 The array is so big ...

  8. 【算法】题目分析:Aggressive Cow (POJ 2456)

    题目信息 作者:不详 链接:http://poj.org/problem?id=2456 来源:PKU JudgeOnline Aggressive cows[1] Time Limit: 1000M ...

  9. *CTF babyarm内核题目分析

    本文从漏洞分析.ARM64架构漏洞利用方式来讨论如何构造提权PoC达到读取root权限的文件.此题是一个ARM64架构的Linux 5.17.2 版本内核提权题目,目的是读取root用户的flag文件 ...

随机推荐

  1. Postman newman

    安装 官方文档: Newman - Running collections in the command line Installing Newman on Windows 1. 安装NodeJs 下 ...

  2. Unity3D 画线插件 Vectrosity_Simple2DLine

    Vectrosity是一个很方便的画线插件,用它我们可以画出2D,3D,贝塞尔,圆,椭圆等各种线条图案. :链接: http://pan.baidu.com/s/1pJjTFjt 密码: uesn 首 ...

  3. android获取apk签名信息

    因为工作需要,需要获取应用的apk传递给新浪微博或者微信去申请授权 所以需要程序内获取签名上传服务器 做法如下:   public static String getAPPSecretString(A ...

  4. Mongodb快速入门之使用Java操作Mongodb

    [IT168 专稿]在上一篇文章中,我们学习了Mongodb的安装和初步使用,在本文中,将学习如何使用Java去编程实现对Mongodb的操作. HelloWorld程序 学习任何程序的第一步,都是编 ...

  5. Java中数组的初始化方式

    Java中数组的初始化方式    初始化方式有两种: 1.静态初始化:初始化时由程序猿显式指定每一个数组元素的初始值,由系统指定数组长度 2.动态初始化:初始化时由程序猿仅仅指定数组长度,由系统为数组 ...

  6. Win7下Qt5.2中使用OpenGL的glu函数库无法使用的解决方案

          最近在Window7使用Qt5.2学习OpenGL时,出现了以OpenGL中glu开头的函数库无法使用的错误,例如: 'gluPerspective'  was not declared ...

  7. windows下如何设置mysql环境变量

    方法一: windows下如何设置mysql环境变量 关键词: mysql, Setting Environment Variables, 环境变量设置 我的电脑->属性->高级-> ...

  8. HDU 1756 Cupid's Arrow 判断点在多边形的内部

    Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  9. 动态代理 Proxy InvocationHandler

      前奏 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等. 代理类与委托类之间通常 ...

  10. noip 2013 华容道

    /*双向bfs (得分和单项的一样多....)70*/ #include<iostream> #include<cstdio> #include<cstring> ...