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{} 不会被执行。就是程序终止的时候,比如:
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)的更多相关文章
- SCJP_104——题目分析(5)
18. public class Test { public static void add3(Integer i) { int val=i.intvalue(); val+=3; i=new Int ...
- SCJP_104——题目分析(1)
1.1) public class ReturnIt{2) returnType methodA(byte x, double y){3) return (short)x/y*2;4) }5) }wh ...
- SCJP_104——题目分析(4)
14. which three are valid declaraction of a float? ADFA. float foo=-1; B. float foo=1.0; C. float fo ...
- SCJP_104——题目分析(3)
11. what is reserved words in java?A. run B. default C. implement D. import Java 中,给标识符取名的时候,不能使用关键字 ...
- SCTF 2014 pwn题目分析
因为最近要去做ctf比赛的这一块所以就针对性的分析一下近些年的各大比赛的PWN题目.主防项目目前先搁置起来了,等比赛打完再去搞吧. 这次分析的是去年的SCTF的赛题,是我的学长们出的题,个人感觉还是很 ...
- 路由器漏洞复现分析第三弹:DVRF INTRO题目分析
这个项目的目的是来帮助人们学习X86_64之外其他架构环境,同时还帮助人们探索路由器固件里面的奥秘. 本文通过练习DVRF 中INTRO 部分的题目来学习下MIPS 结构下的各种内存攻击. DVRF: ...
- 二分查找总结及部分Lintcode题目分析 2
Search in a big sorted array,这个比之前的二分法模板多了一个很不同的特性,就是无法知道一个重要的条件end值,也是题目中强调的重点 The array is so big ...
- 【算法】题目分析:Aggressive Cow (POJ 2456)
题目信息 作者:不详 链接:http://poj.org/problem?id=2456 来源:PKU JudgeOnline Aggressive cows[1] Time Limit: 1000M ...
- *CTF babyarm内核题目分析
本文从漏洞分析.ARM64架构漏洞利用方式来讨论如何构造提权PoC达到读取root权限的文件.此题是一个ARM64架构的Linux 5.17.2 版本内核提权题目,目的是读取root用户的flag文件 ...
随机推荐
- mysql 一般操作
mysql -u root -p ->mysql show databases; ->mysql use [database_name]; ->mysql show tables; ...
- First Record
今天我在博客园安家了! R Python Scala hadoop Spark MachineLearning
- SRM 395(1-250pt)
DIV1 250pt 题意:在平面直角坐标系中,只能走到整点,每次有两种移动方法,可以沿平行于坐标轴方向走,也可以沿45度方向走,前者走一步耗时wt,后者走一步耗时st.比如从(x, y)可以走到(x ...
- Azkaban2官方配置文档
最近工作实在是太忙了,我把之前翻译的官方的文档先放上来吧,希望对大家有所帮助~ 介绍 Azkaban2新功能: 1.Web UI 2.简单工作流上传 3.更容易设置job的依赖关系 4.调度工作流 5 ...
- Ueditor 1.4.3 jsp utf-8版图片上传问题
- 422. Valid Word Square
似乎可以沿着对角线往右往下检查,也可以正常按题设检查. 我用的后者.. public class Solution { public boolean validWordSquare(List<S ...
- Html中src、href的相对路径与绝对路径
What is a path? Why is this something developers should care about? A path is simply the location of ...
- 【转】AngularJs $location获取url参数
// 带#号的url,看?号的url,见下面 url = http://qiaole.sinaapp.com?#name=cccccc $location.absUrl(); // http://qi ...
- SpringMVC在上传文件的时候提示The current request is not a multipart request错误
@RequestMapping("/insertOrder") @ResponseBody public Object insertOrder(String userId,Htt ...
- UIAlertView弹出框
<Alert弹出框提示用户信息> 1.遵循代理方法<UIAlertViewDelete> 2.调用方法UIAlertView *alert = [[UIAlertV ...