一、例题:
例1 阅读下面代码段: Public class Test
{
Public static void main (string args [ ])
{
Int m ; Switch (m) {
Case 0 : System.out.println(“case 0”); break; Case 1: Case 2:
Case 3: system.out.println(“Non Zero”); } } }
将不输出 “Non Zero”的m值是 A
A) 0 B)1 C)2 D) 3 例2 阅读下列代码段: Int i= 3, j ;
Outer:while(i>0)
{
j = 3 ;
inner: while (j>0)
{
if (j<2) break outer ;
System.out.println (j +”and” + i ) ; j -- ; } i -- ; }
下列哪一项将输出到屏幕上? A
A) 3and3 B) 3and2 C) 3and1 D) 3and0 例3 switch 语句不能用于的数据类型是 A
A) double B) byte C) short D) char 例4 循环语句包括(for 语句 ),(while 语句 )和( do while 语句) 例5 Java 的跳转语句中————(包含/不包含)goto 语句 答案:不包含
例6 else 子句不能单独作为语句使用,它必须和if 子句配使用,那么else 子句与 if 子
句的配对原则是:else 子句总是与离它-----的IF子句配对使用。 答案——最近。 例7 阅读下面代码段: Public class Test {
Public static void main (String args[])
{
Int n=10;
For (int i=1;i<=n;i++) {
If(n%i!=0) continue; System.out.print(i++”,”); }
System.out.println(); } }
不被输出的选项是----C。
A) 1 B)2 C)4 D)5
例8 下面是验证哥德巴赫“1+1=2”的程序,即将6~~100之内的偶数表示为两个素数之和
(注:哥德巴赫猜想是要证明对任何大于6的自然数n之内的所有偶数可以表示为两个素数之和),请在划线处编写适当的语句,完成此程序,使它能正确执行。 Public class Test {
Public static void main (String args[])
{
For(int i=6;i<=100;i+=2) {
For(int j=2;j<100;j++) {
-----------------------------
System.out.println(i+”=”+j+”+”+(i-j)); Break; }//loop j }//loop i }
Public static boolean isPrime (int n)
{
For(int i=2;i If(n%i==0) Return false; } Return true; } } 答案:if(isPrime(j)&&isPrime(i-j)){ 例9 请在划线处编写适当语句,完成此程序使它能正确执行。 Import java.io.* ; Public class LeapYear { Public static void main (String args [ ]) throws IOException { InputStreamReader ir ; BufferedReader in ; ir=new InputstreamReader (system . in) ; in=new BufferedReader (ir) ; System . out .println(“输入年份:”); String s=in .readLine() ; Int year=Integer .parseInt(s) ; ----------------------------------- { System .out.println (“” +year +”年是闰年。“) ; } Else { System.out.println(“”+year+”年不是闰年。”) ; } } } 答案: if (year%4= = 0 &&year0!=0||year@0= = 0),或者 if (year@0= =0|| year%4= =0 && year0!=0) 例10 阅读下列代码段: Int x=3 ; While (x<9) x+=2 ; x++ ; while 语句执行的次数是 A A) 3 B)4 C)6 D)9 二、自测题 1 阅读下列代码段: Int i=3, j ; Outer : while (i>0) { J=3 ; Inner:while (j>0) { If (j<2) break inner; System.out.println(j+”and”+i) ; j-- ; } i -- ; } 下列哪一项将不会被输出到屏幕上? A ) 3and3 B) 3and2 C) 3and1 D)3and0 2. 如果要抛出异常,应该采用的子句是---------- A) catch B) throw C) try D) finally 3.阅读下面代码段: Public class person { Int arr [ ]= new int [10]; Public static vond main (string a[ ]) { System.out.println(arr[1]); } } 执行结果正确的说法是------------。 A) 编译时将产生错误 B) 编译时正确,运行时将产生错误 C) 输出零 D) 输出空 4.阅读下面代码段: Public class Test { Public static void main (string args [ ]) { Char ch ; Switch (ch) { Case ?a?: system.out.print(“abc”);break; Case?b?: system.out.print(“ab”); Case?c?: system.out.print(“c”);break; Default: system.out.print(“abc”); } } } 不输出”abc”的ch值是-----------。 A)?a? B) ?b? C) ?c? D) ?d? 5.和语句 For (int x=0;x <15; x+=2) sum+=x+5; 作用一样的语句是--------------。 A) for (int x =5; x<20;x+=2)sum+=x; B) for(int x=5;x<20;x+=x-2)x+=2; C) for(int x=0;x<15;x+=2)sum+=x+3;x+=2; D) 上述全对 6 和语句 y=x+7; x++; While (x<9) { y=x+7; x++; } 执行结果相同的语句是------------ A) y=x+7; x++; do{ y=x+7; x++; }while (x<9) B) do { y=x+7; x++; }while (x<9) C) do { y=x+7; x++; }while (x<=9) D) 上述都不对 7 下列可以实现无限循环的语句是------------- A) for (;;) {…} B) if(true){…} C) while(false) {…} D) 上述都可以 8请写出下列代码段的输出结果: Int x=12,y=14,z; z=y/x+7; x=z*z; system.out.println(x); 9阅读下列代码段: Int x=10,y=12,r; If (y>x) { int t=y; Y=x; X=t; } While (y!=0) { R=x%y; X=y; Y=r; } System.out.println(x); 程序运行结果为---------- A)2 B)4 C) 6 D)8