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文件 ...
随机推荐
- SRM 403(1-250pt, 1-500pt)
DIV1 250pt 题意:称各个数位只含有4和7的数为lucky number,给定a,b,求[a, b]中的lucky number有多少个.a, b <= 10^9 解法:很明显的数位dp ...
- zoj 1760 floyd构图+Dinic最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 #include <cstdio> #includ ...
- multipath.conf
# This is a basic configuration file with some examples, for device mapper# multipath.# For a comple ...
- Redis 实现用户积分排行榜
排行榜功能是一个很普遍的需求.使用 Redis 中有序集合的特性来实现排行榜是又好又快的选择. 一般排行榜都是有实效性的,比如“用户积分榜”.如果没有实效性一直按照总榜来排,可能榜首总是几个老用户,对 ...
- IOS多线程的小总结
ios中多线程的实现方案有4种 1.pthread :一套通用的多线程API/适用于Unix\Linux\Windows等系统 (跨平台可移植/使用难度大) C语言 几乎不用 ...
- Qt 学习之路 :Qt 绘制系统简介
Qt 的绘图系统允许使用相同的 API 在屏幕和其它打印设备上进行绘制.整个绘图系统基于QPainter,QPainterDevice和QPaintEngine三个类. QPainter用来执行绘制的 ...
- Fedora14下首次搭建Samba服务器遇到的一些问题
SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的通信协议.而Samba则是在Linux和Unix系统上实现SMB协议的一个免费软件,由服务器及客户端程 ...
- Notice : Soft open files now is 1024, We recommend greater than 10000
在研究 workerman 时, 报了这个错误, 感觉只是个notice级别的, 就一直给忽略掉了, 今天有时间, 就查了一下. 其实本质就是 ulimit 这个命令 打开一个命令行, 输入 ulim ...
- CentOS修改SSH默认端口
1. 修改配置文件 vim /etc/ssh/sshd_config 修改 #Port 22 这行, 去掉 # 修改后面的端口号 例如 Port 2123 重启sshd服务 /etc/init.d ...
- call, apply, bind作用
call, apply作用就是(改变方法中的this指向)借用别人的方法来调用,就像调用自己的一样 function Person(name) { this.name = name; } Person ...