内容概要
练习
流程控制 (if 和 switch)
1. 给出以下代码:
1. public class Switch2 {
2. final static short x = 2;
3. public static int y = 0;
4. public static void main(String [] args) {
5. for (int z=0; z < 3; z++) {
6. switch (z) {
7. case y: ("0 ");
8. case x-1: ("1 ");
9. case x: ("2 ");
10. }
11. }
12. }
13. }
哪一项是运行结果?
A. 0 1 2
B. 0 1 2 1 2 2
C. 在第7行编译失败。
D. 在第8行编译失败。
E. 在第9行编译失败。
F. 运行时抛出异常。
2. 给出下面的代码:
1. public class Switch2 {
2. final static short x = 2;
3. public static int y = 0;
4. public static void main(String [] args) {
5. for (int z=0; z < 3; z++) {
6. switch (z) {
7. case x: ("0 ");
8. case x-1: ("1 ");
9. case x-2: ("2 ");
10. }
11. }
12. }
13. }
哪一项是运行结果? (1)
A. 0 1 2
B. 0 1 2 1 2 2
C. 2 1 0 1 0 0
D. 2 1 2 0 1 2
E. 在第8行编译失败。
F. 在第9行编译失败。
3. 给出下面的代码:
1. public class If1 {
2. static boolean b;
3. public static void main(String [] args) {
4. short hand = 42;
5. if ( hand < 50 & !b ) hand++;
6. if ( hand > 50 ) ;
7. else if ( hand > 40 ) {
8. hand += 7;
9. hand++; }
10. else
11. --hand;
12. (hand);
13. }
14. }
哪一项是运行结果
A. 41
B. 42
C. 50
D. 51
E. 在第5行编译失败。
F. 在第6行编译失败。
4. Given the following,
1. public class Switch2 {
2. final static short x = 2;
3. public static int y = 0;
4. public static void main(String [] args) {
5. for (int z=0; z < 4; z++) {
6. switch (z) {
7. case x: ("0 ");
8. default: ("def ");
9. case x-1: ("1 "); break;
10. case x-2: ("2 ");
11. }
12. }
13. }
14. }
what is the result?
A. 0 def 1
B. 2 1 0 def 1
C. 2 1 0 def def
D. 2 1 def 0 def 1
E. 2 1 2 0 def 1 2
F. 2 1 0 def 1 def 1
5. 给出以下代码:
1. public class If2 {
2. static boolean b1, b2;
3. public static void main(String [] args) {
4. int x = 0;
5. if ( !b1 ) {
6. if ( !b2 ) {
7. b1 = true;
8. x++;
9. if ( 5 > 6 ) {
10. x++;
11. }
12. if ( !b1 ) x = x + 10;
13. else if ( b2 = true ) x = x + 100;
14. else if ( b1 | b2 ) x = x + 1000;
15. }
16. }
17. (x);
18. }
19. }
哪一项是运行结果?
A. 0
B. 1
C. 101
D. 111
E. 1001
F. 1101
流程控制(循环)
6. 给出下面的代码:
1. public class While {
2. public void loop() {
3. int x= 0;
4. while ( 1 ) {
5. ("x plus one is " + (x + 1));
6. }
7. }
8. }
哪一项是正确的?
A. 在第1行有一个语法错误。
B. 在第1行和第4行有一个语法错误。
C. 在第1行、第4行和第5行有一个语法错误。
D. 在第4行有一个语法错误。
E. 在第4行和第5行有一个语法错误。
F. 在第5行有一个语法错误。
7. 给出下面的代码:
1. class For {
2. public void test() {
3.
4. ("x = "+ x);
5. }
6. }
7. }
and the following output,
x = 0
x = 1
哪两行语句独立地插入到第3行能够产生输出?
A. for (int x = -1; x < 2; ++x) {
B. for (int x = 1; x < 3; ++x ) {
C. for (int x = 0; x > 2; ++x ) {
D. for (int x = 0; x < 2; x++ ) {
E. for (int x = 0; x < 2; ++x ) {
8. 给出以下代码:
1. public class Test {
2. public static void main(String [] args) {
3. int I = 1;
4. do while ( I < 1 )
5. ("I is " + I);
6. while ( I > 1 ) ;
7. }
8. }
哪一项是运行结果?
A. I is 1
B. I is 1 I is 1
C. 没有输出。
D. 编译错误。
E. I is 1 I is 1 I is 1 in an infinite loop.
9. 给出下面的代码:
11. int I = 0;
12. outer:
13. while (true) {
14. I++;
15. inner:
16. for (int j = 0; j < 10; j++) {
17. I += j;
18. if (j == 3)
19. continue inner;
20. break outer;
21. }
22. continue outer;
23. }
24. (I);
25.
26.
哪一项是运行结果?(1)
A. 1
B. 2
C. 3
D. 4
10. 给出下面的代码:
1. int I = 0;
2. label:
3. if (I < 2) {
4. ("I is " + I);
5. I++;
6. continue label;
7. }
哪一项是运行的结果?
A. I is 0
B. I is 0 I is 1
C. 编译错误。
D.以上都不对。
异常
11. 给出下面的代码:
1. ("Start ");
2. try {
3. ("Hello world");
4. throw new FileNotFoundException();
5. try {
6. out = new FileOutputStream("");
7. (122);
8. }
9. catch(IOException io) {
10. ("IO Error.");
11. }
12. finally {
13. ();
14. }
15. }
16. }
下面哪一项是正确的?
A. 程序将成功编译。
B. 第4行有一个错误。
C. 第6行有一个错误。
D. 第9行有一个错误。
E. 第13行有一个错误。
14. 给出下面的代码:
1. public class MyProgram {
2. public static void throwit() {
3. throw new RuntimeException();
4. }
5. public static void main(String args[]){
6. try {
7. ("Hello world ");
8. throwit();
9. ("Done with try block ");
10. }
11. finally {
12. ("Finally executing ");
13. }
14. }
15. }
哪一项是正确的?(1)
A. 程序不能编译。
B. 程序将输出:Hello world, 然后将输出:a RuntimeException has
occurred, 然后将输出:Done with try block, 最后将输出:Finally executing。
C. 程序将输出:Hello world, 然后将输出:a RuntimeException has
occurred,最后将输出:Finally executing。
D. 程序将输出:Hello world, 然后将输出:Finally executing, 最后将输出:a
RuntimeException has occurred。
15. 给出下面的代码:
1. public class RTExcept {
2. public static void throwit () {
3. ("throwit ");
4. throw new RuntimeException();
5. }
6. public static void main(String [] args) {
7. try {
8. ("hello ");
9. throwit();
10. }
11. catch (Exception re ) {
12. ("caught ");
13. }
14. finally {
15. ("finally ");
16. }
17. ("after ");
18. }
19. }
哪一项是运行结果?
A. hello throwit caught
B. 编译失败。
C. hello throwit RuntimeException caught after
D. hello throwit RuntimeException
E. hello throwit caught finally after
F. hello throwit caught finally after RuntimeException
断言
16. 下面哪一项是正确的?
A. 在一个断言语句中,冒号(:)后面可以跟任何Java表达式。
B. 如果一个switch块没有default,添加一条语句是恰当的。
C. 在一个断言语句中,如果在冒号后面没有表达式,断言的错误信息将是空的。
D. 使用catch子句处理处理断言失败是恰当的。
17. 下面哪两项是正确的?
A. 显式地抛出一个断言错误有时是一个好的做法。
B. 在你认为不可能执行到的地方放一个断言是一个好的做法。
C. 私有的getter() 和 setter()不应当使用断言去验证参数。
D. 如果在一个try-catch块中产生了一个断言错误,finally块将被跳过。
E. 使用catch块处理断言错误是正确的。
18. 给出以下代码:
1. public class Test {
2. public static int y;
3. public static void foo(int x) {
4. ("foo ");
5. y = x;
6. }
7. public static int bar(int z) {
8. ("bar ");
9. return y = z;
10. }
11. public static void main(String [] args ) {
12. int t = 0;
13. assert t > 0 : bar(7);
14. assert t > 1 : foo(8);
15. ("done ");
16. }
17. }
哪一项是运行的结果?
A. bar
B. bar done
C. foo done
D. bar foo done
E. 编译失败。
F. 运行时抛出异常。
19. 下面哪两项是正确的?
A. 包含断言语句的程序在运行时不加参数,缺省情况下断言语句将被执行。
B. 在版本中,缺省情况下断言语句能够被编译。
C. 正确地使用运行时参数,可以指示虚拟机关闭某一个类中的断言同时打开某一
个包中的断言。
D. 下面都是合法的运行时断言标志:
-ea, -esa, -dsa, -enableassertions, -disablesystemassertions
E. 虚拟机在处理命令行参数时,对–ea标志的处理会优先于–da标志。
20. 给出下面的代码:
1. public class Test2 {
2. public static int x;
3. public static int foo(int y) {
4. return y * 2;
5. }
6. public static void main(String [] args) {
7. int z = 5;
8. assert z > 0;
9. assert z > 2: foo(z);
10. if ( z < 7 )
11. assert z > 4;
12. switch (z) {
13. case 4: ("4 ");
14. case 5: ("5 ");
15. default: assert z < 10;
16. }
17. if ( z < 10 )
18. assert z > 4: z++;
19. (z);
20. }
21. }
哪一行是不恰当地使用了断言的例子?
A. 第8行。
B. 第9行。
C. 第11行。
D. 第15行。
E. 第18行。