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"

SCJP_104——题目分析(2),码迷,mamicode.com

时间: 2024-08-06 09:07:58

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

SCJP_104——题目分析(5)

18. public class Test { public static void add3(Integer i) { int val=i.intvalue(); val+=3; i=new Integer(val); } public static void main(String args[]) { Integer i=new Integer(0); add3(i); System.out.println(i.intvalue()); } } what is the result? bA.

SCJP_104——题目分析(3)

11. what is reserved words in java?A. run B. default C. implement D. import Java 中,给标识符取名的时候,不能使用关键字和保留字. 在 Java 中常用的关键字有: 1.访问控制符: public.protected.private 2.数据类型 byte.short.int.long.float.double.char.boolean 3.与类.方法有关的 new.class.extends.implements.

SCJP_104——题目分析(1)

1.1) public class ReturnIt{2) returnType methodA(byte x, double y){3) return (short)x/y*2;4) }5) }what is valid returnType for methodA in line 2? 这一题考的是Java中的类型转换. 在这里,自需要看第(3)据代码就行了.分析以下这一行代码 (short)x / y * 2 这一行代码对 x 进行类型转换,将 byte 类型的 x 转换成 short 类

SCJP_104——题目分析(4)

14. which three are valid declaraction of a float? ADFA. float foo=-1; B. float foo=1.0; C. float foo=42e1; D. float foo=2.02f; E. float foo=3.03d; F. float foo=0x0123; 这一题考的是 Java 的基本数据类型和类型转换. 在 Java 中规定,整数字面值默认为 int 类型,浮点数字面值默认为 double 类型. A:-1 是

最长英文单词串题目分析

题目来源: 这是题目来源 序: 简述一下事情的经过 一天邹欣老师在群里向我提出了一个问题: "把这个题目分析一下,主要是从工程上考虑,假如50个学生提交了程序,你如何写一个测试的系统,判断对错,和测量学生程序的对错" 一.首先分析一下这道题的核心 "最长的能首尾相连的英语单词链,每个单词最多只能使用一次" 这里题目的描述有一点不明确,关于"最长",题目并没有给出详细定义. 因此可能有两种情况. 单词链中单词数最多 单词链中字符数最多 (看到这里我

MySQL索引题目分析

1.之前看视频呢的时候,里面提到一道索引题目:假设某个表有一个联合索引(c1,c2,c3,c4)-只能使用该联合索引的c1,c2,c3部分 a.where c1=x and c2=x and c4>x and c3=x b.where c1=x and c2=x and c4=x order by c3 c.where c1=x and c4=x group by c3,c2 d.where c1=? and c5=? order by c2,c3 e.where c1=? and c2=? a

3Sum 等类似题目分析

3Sum 题目描述:给定一个整数数组,找出其中的三个数之和为0的所有组合(排除相同的组合). 分析:利用TwoSum 中两指针的思路,我们可以先将数组排序.要找到3个数之和为0,我们可以先固定一个数num[i],将i+1和len-1分别作为头指针和尾指针,当num[i].num[i+1]与num[len-1]的和大于0时,移动尾指针:小于0时,移动头指针:等于0则找到一组数.在循环查找下一组解时,当下一个值与当前值相等会出现重复的解,此时可以跳过当前循环来去除重复.具体代码如下: 1 vecto

SCTF 2014 pwn题目分析

因为最近要去做ctf比赛的这一块所以就针对性的分析一下近些年的各大比赛的PWN题目.主防项目目前先搁置起来了,等比赛打完再去搞吧. 这次分析的是去年的SCTF的赛题,是我的学长们出的题,个人感觉还是很符合套路的:一道栈溢出.一道格式化字符串.一道堆溢出. pwn200 一个栈溢出. 题目给出了libc.保护只有nx. 拿到题后的基本思路就是leak出got表中函数的地址,然后拿libc算偏移算出system的地址.然后用这个地址去覆盖一个函数的got表. pwn300 一个明显的格式化字符串漏洞

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

Search in a big sorted array,这个比之前的二分法模板多了一个很不同的特性,就是无法知道一个重要的条件end值,也是题目中强调的重点 The array is so big so that you can not get the length of the whole array directly~所以这里单独分析这个问题~ 一般涉及到sorted array,肯定可以的解决方法是for loop,然而for loop的复杂度是O(n),而且对于这个长度无法衡量的big