4 流程控制异常和断言有关断言的题不用做.docx
《4 流程控制异常和断言有关断言的题不用做.docx》由会员分享,可在线阅读,更多相关《4 流程控制异常和断言有关断言的题不用做.docx(16页珍藏版)》请在冰豆网上搜索。
4流程控制异常和断言有关断言的题不用做
内容概要
练习
◆流程控制(if和switch)
1.给出以下代码:
1.publicclassSwitch2{
2.finalstaticshortx=2;
3.publicstaticinty=0;
4.publicstaticvoidmain(String[]args){
5.for(intz=0;z<3;z++){
6.switch(z){
7.casey:
System.out.print("0");
8.casex-1:
System.out.print("1");
9.casex:
System.out.print("2");
10.}
11.}
12.}
13.}
哪一项是运行结果?
A.012
B.012122
C.在第7行编译失败。
D.在第8行编译失败。
E.在第9行编译失败。
F.运行时抛出异常。
2.给出下面的代码:
1.publicclassSwitch2{
2.finalstaticshortx=2;
3.publicstaticinty=0;
4.publicstaticvoidmain(String[]args){
5.for(intz=0;z<3;z++){
6.switch(z){
7.casex:
System.out.print("0");
8.casex-1:
System.out.print("1");
9.casex-2:
System.out.print("2");
10.}
11.}
12.}
13.}
哪一项是运行结果?
(1)
A.012
B.012122
C.210100
D.212012
E.在第8行编译失败。
F.在第9行编译失败。
3.给出下面的代码:
1.publicclassIf1{
2.staticbooleanb;
3.publicstaticvoidmain(String[]args){
4.shorthand=42;
5.if(hand<50&!
b)hand++;
6.if(hand>50);
7.elseif(hand>40){
8.hand+=7;
9.hand++;}
10.else
11.--hand;
12.System.out.println(hand);
13.}
14.}
哪一项是运行结果
A.41
B.42
C.50
D.51
E.在第5行编译失败。
F.在第6行编译失败。
4.Giventhefollowing,
1.publicclassSwitch2{
2.finalstaticshortx=2;
3.publicstaticinty=0;
4.publicstaticvoidmain(String[]args){
5.for(intz=0;z<4;z++){
6.switch(z){
7.casex:
System.out.print("0");
8.default:
System.out.print("def");
9.casex-1:
System.out.print("1");break;
10.casex-2:
System.out.print("2");
11.}
12.}
13.}
14.}
whatistheresult?
A.0def1
B.210def1
C.210defdef
D.21def0def1
E.2120def12
F.210def1def1
5.给出以下代码:
1.publicclassIf2{
2.staticbooleanb1,b2;
3.publicstaticvoidmain(String[]args){
4.intx=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.elseif(b2=true)x=x+100;
14.elseif(b1|b2)x=x+1000;
15.}
16.}
17.System.out.println(x);
18.}
19.}
哪一项是运行结果?
A.0
B.1
C.101
D.111
E.1001
F.1101
◆流程控制(循环)
6.给出下面的代码:
1.publicclassWhile{
2.publicvoidloop(){
3.intx=0;
4.while
(1){
5.System.out.print("xplusoneis"+(x+1));
6.}
7.}
8.}
哪一项是正确的?
A.在第1行有一个语法错误。
B.在第1行和第4行有一个语法错误。
C.在第1行、第4行和第5行有一个语法错误。
D.在第4行有一个语法错误。
E.在第4行和第5行有一个语法错误。
F.在第5行有一个语法错误。
7.给出下面的代码:
1.classFor{
2.publicvoidtest(){
3.
4.System.out.println("x="+x);
5.}
6.}
7.}
andthefollowingoutput,
x=0
x=1
哪两行语句独立地插入到第3行能够产生输出?
A.for(intx=-1;x<2;++x){
B.for(intx=1;x<3;++x){
C.for(intx=0;x>2;++x){
D.for(intx=0;x<2;x++){
E.for(intx=0;x<2;++x){
8.给出以下代码:
1.publicclassTest{
2.publicstaticvoidmain(String[]args){
3.intI=1;
4.dowhile(I<1)
5.System.out.print("Iis"+I);
6.while(I>1);
7.}
8.}
哪一项是运行结果?
A.Iis1
B.Iis1Iis1
C.没有输出。
D.编译错误。
E.Iis1Iis1Iis1inaninfiniteloop.
9.给出下面的代码:
11.intI=0;
12.outer:
13.while(true){
14.I++;
15.inner:
16.for(intj=0;j<10;j++){
17.I+=j;
18.if(j==3)
19.continueinner;
20.breakouter;
21.}
22.continueouter;
23.}
24.System.out.println(I);
25.
26.
哪一项是运行结果?
(1)
A.1
B.2
C.3
D.4
10.给出下面的代码:
1.intI=0;
2.label:
3.if(I<2){
4.System.out.print("Iis"+I);
5.I++;
6.continuelabel;
7.}
哪一项是运行的结果?
A.Iis0
B.Iis0Iis1
C.编译错误。
D.以上都不对。
◆异常
11.给出下面的代码:
1.System.out.print("Start");
2.try{
3.System.out.print("Helloworld");
4.thrownewFileNotFoundException();
5.try{
6.out=newFileOutputStream("test.txt");
7.out.write(122);
8.}
9.catch(IOExceptionio){
10.System.out.println("IOError.");
11.}
12.finally{
13.out.close();
14.}
15.}
16.}
下面哪一项是正确的?
A.程序将成功编译。
B.第4行有一个错误。
C.第6行有一个错误。
D.第9行有一个错误。
E.第13行有一个错误。
14.给出下面的代码:
1.publicclassMyProgram{
2.publicstaticvoidthrowit(){
3.thrownewRuntimeException();
4.}
5.publicstaticvoidmain(Stringargs[]){
6.try{
7.System.out.println("Helloworld");
8.throwit();
9.System.out.println("Donewithtryblock");
10.}
11.finally{
12.System.out.println("Finallyexecuting");
13.}
14.}
15.}
哪一项是正确的?
(1)
A.程序不能编译。
B.程序将输出:
Helloworld,然后将输出:
aRuntimeExceptionhas
occurred,然后将输出:
Donewithtryblock,最后将输出:
Finallyexecuting。
C.程序将输出:
Helloworld,然后将输出:
aRuntimeExceptionhas
occurred,最后将输出:
Finallyexecuting。
D.程序将输出:
Helloworld,然后将输出:
Finallyexecuting,最后将输出:
aRuntimeExceptionhasoccurred。
15.给出下面的代码:
1.publicclassRTExcept{
2.publicstaticvoidthrowit(){
3.System.out.print("throwit");
4.thrownewRuntimeException();
5.}
6.publicstaticvoidmain(String[]args){
7.try{
8.System.out.print("hello");
9.throwit();
10.}
11.catch(Exceptionre){
12.System.out.print("caught");
13.}
14.finally{
15.System.out.print("finally");
16.}
17.System.out.println("after");
18.}
19.}
哪一项是运行结果?
A.hellothrowitcaught
B.编译失败。
C.hellothrowitRuntimeExceptioncaughtafter
D.hellothrowitRuntimeException
E.hellothrowitcaughtfinallyafter
F.hellothrowitcaughtfinallyafterRuntimeException
◆断言
16.下面哪一项是正确的?
A.在一个断言语句中,冒号(:
)后面可以跟任何Java表达式。
B.如果一个switch块没有default,添加一条语句是恰当的。
C.在一个断言语句中,如果在冒号后面没有表达式,断言的错误信息将是空的。
D.使用catch子句处理处理断言失败是恰当的。
17.下面哪两项是正确的?
A.显式地抛出一个断言错误有时是一个好的做法。
B.在你认为不可能执行到的地方放一个断言是一个好的做法。
C.私有的getter()和setter()不应当使用断言去验证参数。
D.如果在一个try-catch块中产生了一个断言错误,finally块将被跳过。
E.使用catch块处理断言错误是正确的。
18.给出以下代码:
1.publicclassTest{
2.publicstaticinty;
3.publicstaticvoidfoo(intx){
4.System.out.print("foo");
5.y=x;
6.}
7.publicstaticintbar(intz){
8.System.out.print("bar");
9.returny=z;
10.}
11.publicstaticvoidmain(String[]args){
12.intt=0;
13.assertt>0:
bar(7);
14.assertt>1:
foo(8);
15.System.out.println("done");
16.}
17.}
哪一项是运行的结果?
A.bar
B.bardone
C.foodone
D.barfoodone
E.编译失败。
F.运行时抛出异常。
19.下面哪两项是正确的?
A.包含断言语句的程序在运行时不加参数,缺省情况下断言语句将被执行。
B.在Java1.4版本中,缺省情况下断言语句能够被编译。
C.正确地使用运行时参数,可以指示虚拟机关闭某一个类中的断言同时打开某一个包中的断言。
D.下面都是合法的运行时断言标志:
-ea,-esa,-dsa,-enableassertions,-disablesystemassertions
E.虚拟机在处理命令行参数时,对–ea标志的处理会优先于–da标志。
20.给出下面的代码:
1.publicclassTest2{
2.publicstaticintx;
3.publicstaticintfoo(inty){
4.returny*2;
5.}
6.publicstaticvoidmain(String[]args){
7.intz=5;
8.assertz>0;
9.assertz>2:
foo(z);
10.if(z<7)
11.assertz>4;
12.switch(z){
13.case4:
System.out.println("4");
14.case5:
System.out.println("5");
15.default:
assertz<10;
16.}
17.if(z<10)
18.assertz>4:
z++;
19.System.out.println(z);
20.}
21.}
哪一行是不恰当地使用了断言的例子?
A.第8行。
B.第9行。
C.第11行。
D.第15行。
E.第18行。