就业数据资源平台
当前位置:首页 > Java语言程序设计
2011年计算机等级考试二级Java章节练习题(4)

第4章 流程控制

  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 &&year%100!=0||year%400= = 0),或者 if (year%400= =0|| year%4= =0 && year%100!=0)


  10 阅读下列代码段:


  Int x=3 ;


  While (x<9)


  x+=2 ;


  x++ ;


  while 语句执行的次数是 A


  A) 3 B)4 C)6 D)9

就业数据资源平台