计算机二级Java备考冲刺试题及答案(2)

学人智库 时间:2018-02-10 我要投稿
【meiwen.anslib.com - 学人智库】

  5、outer: for(int i=0;i<3; i++)

  inner: for(int j=0;j<2;j++)

  {

  if(j==1) continue outer;

  System.out.println(j+ "and "+i+";");

  }

  以上代码输出是 。

  A、0 and 0;0 and 1;0 and 2;

  B、0 and 0;1 and 0;2 and 0;

  C、1 and 0;1 and 1;1 and 2;

  D、2 and 0;2 and 1;2 and 2;

  本题考查的是多重循环。

  首先介绍一下continue语句的功能:continue语句是跳过循环体中下面尚未执行的语句,回到循环体的开始,继续下一轮的循环。

  本题程序运行过程如下:

  i=0:

  j=0 条件j==1不成立,输出j和i,即0 and 0;

  j=1 条件j==1成立,执行continue,跳过System.out.println(j+ "and "+i+";");执行下一轮循环j=2;

  j=2 条件j<2不满足,退出内层循环,继续外层循环。

  i=1:

  j=0 条件j==1不成立,输出j和i,即0 and 1;

  j=1 条件j==1成立,执行continue,跳过System.out.println(j+ "and "+i+";");执行下一轮循环j=2;

  j=2 条件j<2不满足,退出内层循环,继续外层循环。

  i=2:

  j=0 条件j==1不成立,输出j和i,即0 and 2;

  j=1 条件j==1成立,执行continue,跳过System.out.println(j+ "and "+i+";");执行下一轮循环j=2;

  j=2 条件j<2不满足,退出内层循环,继续外层循环。

  i=3:条件i<3不满足,结束。

  故本题答案为A。

  编程题

  1、 编写一个Java Application 程序App.java,main程序输入10个整数给数组,通过函数getMinAndMax(int a[])得到这10个整数的最大值和最小值并输出结果。

  class App {

  static void getMinAndMax(int a[]) {

  int min,max;

  min = max = a[0];

  for(int i=1;i  if(a[i]>max)

  max=a[i];

  if(a[i]  min=a[i]; }

  System.out.println(“Array’Max Value:”+max);

  System.out.println(“Array’Min Value:”+min);

  }

  public static void main(String[] args) {

  int arr[] = {4,6,72,9,14,3,8,23,56,32};

  getMinAndMax(arr); } }

  2、编写一个完整的Java Application 程序。包含接口ShapeArea, Rectangle

  类,Triangle类及Test类,具体要求如下:

  ⑴接口ShapeArea:

  double getArea(  ):

  求一个形状的面积

  double getPerimeter (  ):

  求一个形状的周长

  ⑵类 Rectangle:实现ShapeArea接口,并有以下属性和方法:

  ① 属性

  width: double类型,表示矩形的长 height: double类型,表示矩形的高

  ② 方法

  Rectangle(double w, double h):构造函数

  toString(  )

  方法 :输出矩形的描述信息,如“width=1.0,height=2.0, perimeter=6.0, area=2.0”

  ⑶类Triangle:实现ShapeArea接口,并有以下属性和方法:

  ① 属性

  x,y,z: double型,表示三角形的三条边

  s: 周长的1/2(注:求三角形面积公式为))(  )((zsysxss,s=(x+y+z)/2 ,开方可用Math.sqrt(double)方法)

  ② 方法

  Triangle(double x, double y, double z):

  构造函数,给三条边和s赋初值。

  toString(  ):

  输出矩形的描述信息,如“three sides:3.0,4.0,5.0,perimeter=12.0,area=6.0”

  ⑷Test类作为主类要完成测试功能

  ① 生成Rectangle对象

  ②

  调用对象的toString方法,输出对象的描述信息

  interface ShapeArea { double getArea(  );

  double getPerimeter(  );

  }

  class Rectangle implements ShapeArea { double width,height;

  Rectangle(double w,double h) {ko width =w;

  height=h;

  }

  public void toString(  )

  {

  System.out.println("width="+width+",height="+height+", perimeter="+ getPerimeter(  )+", area="+ getArea(  ));

  }

  public double getArea(  )

  { return width*height;

  }

  public double getPerimeter(  )

  { return 2*(width+height);

  } }

  class Triangle implements ShapeArea { double x,y,z,s; Triangle(double x, double y, double z) { this.x =x; this.y=y;

  this.z=z; s = (x+y+z)/2; }

  public void toString(  )

  {

  System.out.println("Three Sides:"+x+","+y+","+z+",Perimeter="+ getPerimeter(  )+", area="+ getArea(  ));

  }

  public double getArea(  )

  {

  return Math.sqrt(s*(s-x)*(s-y)*(s-z));

  }

  public double getPerimeter(  )

  { return x+y+z;

  } }

  class test { public static void main(String[] args) { Rectangle rct = new Rectangle(4,5);

  rct.to_String(  );

  } }