1、MCK EDU 2011-2012年浙江师范大学《Java语言程序设计》期末试题A卷 (2011—2012学年第 1 学期) 考试形式 笔试(闭卷) 使用学生 本科生 考试时间 120 分钟 出卷时间2012年 06月 17日 【说明】 (1) 考生应将全部答案都写在答题纸上,否则作无效处理。 (2) 试题一~五为程序填空选择题,请从供选择答案中为每个空格挑选出一个正确答案,并写在答题纸上,试题六为程序填空题,试题七八为程序设计题。 试题一(程序选择,每题2分,共10分) 【程序说明】利用下列公式计算pi的近似值,要求精确到10-6
2、为止。请将程序填充完整或回答相关问题。 Pi/4 ≈ 1 – 1/3 + 1/5 – 1/7 + … 【程序代码】 public class CalculatePI { public static void main(String[] args) { System.out.printf("pi = %.6f",Pi()); } public static double Pi() { double result,item; int denominator; result = 0; denominator = 1; (1)
3、 ; do { item = (2) ; result += item; sign = -sign; denominator +=2; }while( (3) ); return result*4; } } 【可供选择的答案】 (1) A. int sign B. int sign = 0 C. int sign = 1 D. int sign = -1 (2) A. (double)sign/denominator B. (int)sign/denominator
4、 C. sign/denominator D. 1.0/denominator (3) A. Math.abs((int)sign / denominator) >=1e-6 B. Math.abs((double)sign / denominator) >=1e-6 C. (double)sign / denominator >=1e-6 D. Math.abs((int)sign / denominator) >=10-6 (4) 以下关于main方法说法正确的是 。 A. 一个类不可以没有main方法; B. 一个类可以
5、有多个main方法; C. 一个应用程序可以有多个类,并且每个类可有main方法; D. 一个可执行的应用程序可以没有main方法。 (5) 以下关于main方法前的关键字void说法正确的是 。 A. 表示该方法不能输出任何结果; B. 表示该方法的静态方法; C. 表示该方法不返回任何结果; D. 表示该方法的访问权限是公有的。 试题二(程序选择,每题2分,共10分) 【程序说明】以下程序是某个类的一个sort方法,功能是对作为参数传入的int型数组arrayX用冒泡法对其进行由小到大排序,请将程序填充完整或回答相关问题,要求选出
6、最佳答案。 【程序代码】 public void sort( (6) ) { int t; for(int k = 0; k < arrayX.length - 1; k++) { for (int j = 1; j < (7) ; j++) { if ( (8) ) { t = arrayX[j]; (9) ; arrayX[j - 1] = t; } } } } 【可供选择的
7、答案】 (6) A. int[] arrayX B. int arrayX C. int[] arrayX[] D. final int[] arrayX (7) A. arrayX.length - 1 B. arrayX.length C. arrayX.length - k D. k (8) A. arrayX[j - 1] > arrayX[j] B. arrayX[j] < arrayX[j - 1] C. arrayX[j] > arrayX[j - 1] D. arrayX[j] < arrayX[j + 1] (9) A
8、 t = arrayX[j - 1] B. arrayX[j] = t C. arrayX[j - 1] = arrayX[j] D. arrayX[j] = arrayX[j - 1] (10) 以下关于数组的说法错误的是 。 A. int型数组可以放直接放double型数据; B. 起始下标为0; C. int型数组可以放直接放byte型数据; D. 下标越界时,会抛出异常 试题三(程序选择,每题2分,共12分) 【程序说明】Triangle为三角形类,方法getArea()用于计算并返回三角形面积(根
9、据三边a,b,c求三角形面积的方法是:令, 面积),请将程序填充完整或回答相关问题。 【程序代码】 public class Triangle { private double sideA; private double sideB; private double sideC; public Triangle(double a, double b, double c) (11) { if((a + b <= c) || (b + c <= a) || (a + c <= b)) { throw new Exception(
10、"三条边不能构成三角形"); } else { sideA = a; sideB = b; sideC = c; } } public (12) getArea() { double s = 0; double l = (sideA + sideB + sideC) / 2.0; s = (13) ; return s; } } public class Test { public static void main(Stri
11、ng[] args) { double a = 3.0, b = 4.0 ,c = 5.0;//*** try { (14) ; System.out.println("三角形面积为" + tri.getArea()); } (15) { System.out.println(ex.getMessage()); } } } 【可供选择的答案】 (11) A. 空 B. throw new Exception() C. thro
12、ws Exception D. throws IOException (12) A. double B. void C. float D. int (13) A. Math.sqrt(l *(l - sideA) * (l - sideB) * (l - sideC)) B. Math.sqrt(l(l - sideA)(l - sideB)(l - sideC)) C. Math.abs(l *(l - sideA) * (l - sideB) * (l - sideC)) D. Math.abs(l(l - sideA)(l - sideB)(l
13、 sideC)) (14) A. Triangle tri = new Triangle() B. Triangle tri C. Triangle triangle = new Triangle(a, b, c) D. Triangle tri = new Triangle(a, b, c) (15) A. catch(IOException ex) B. catch(Exception e) C. catch(Exception ex) D. catch(IOException e) (16) 该程序的输出结果为
14、 。 A. 三角形面积为6.0 B. 三条边不能构成三角形 C. 6.0 D. 程序出错,没有结果 试题四(程序选择,每题2分,共8分) 【程序说明】以下程序为类Person、Animal、Chinese、Zhejiang和Test的代码,请将程序填充完整或回答相关问题。 【程序代码】 //Person.java package cn.zjnu.ks; public (22) class Person { (23) String hairColor; public abstract void s
15、peak(); public Person() { hairColor=””; System.out.println("人类"); } } //Animal.java package cn.zjnu.animal; public class Animal { } //Chinese.java package cn.zjnu.chinese; public class Chinese extends Person { public void speak() { System.out.println("我会说汉语"); } public
16、 void speak(String msg) { System.out.println("我会说"+msg); } public Chinese() { hairColor = "黑色"; System.out.println("中国人"); } } //Zhejiang.java public class Zhejiang extends Chinese { public void speak() { (24) //调用父类的方法speak() } public Zhejian
17、g() { System.out.println("浙江人"); } } //Test.java public class Test { public static void main(String[] args) { Zhejiang p = new Zhejiang(); } } 【可供选择的答案】 (22) A. void B. abstract C. fianal D. static (23) 若成员变量hairColor能被类Chinese中的方法访问,但不被Aniaml类中方法访问,则应该填入
18、 A.public B.protected C. private D.static (24) A. super.speak(); B. Chinese.speak(); C. speak(); D.this. speak(); (25) 执main()方法,输出的结果为 A. 人类 中国人 浙江人 B. 人类 我会说汉语 浙江人 C. 浙江人 中国人 人类 D. 我会说汉语 我会说浙江普通话 试题五(程序填空,每题2分,共14分) 【程序说明】请按程序模板及注释要求,将程序填充完整。 【程序代码】
19、 【代码1】//仔细阅读程序,导入必要的包 import java.awt.event.*; public class MyFrame extends Frame 【代码2】//实现事件监听接口 { TextField text; Label label; public MyFrame() { this.setBounds(100,100,300,200); 【代码3】//创建TextField对象:text,要求text的可见字符数为12个机器字符。 【代码4】//创建 Label对象:label,要求label上是"I love this g
20、ame"。 【代码5】//设置窗口的布局为FlowLayout型布局。 【代码6】//将当前窗口作为text的ActionEvent事件的监视器。 this.add(label); this.add(text); 【代码7】//设置窗口可见 this.addWindowListener(【代码8】{//创建窗体适配器的匿名类对象 public void windowClosing(WindowEvent e){ 【代码9】}//退出应用程序 });// 匿名类定义结束 } pu
21、blic void actionPerformed(ActionEvent e) { 【代码10】//label调用方法将自己名字设置为text中的文本 } } 【代码1】 【代码2】 【代码3】 【代码4】 【代码5】 【代码6】
22、 【代码7】 【代码8】 【代码9】 【代码10】 试题六(编程题,共15分) 【编程要求】根据以下的设计要求编写java源代码。 类名: Person 变量(访问权限均为private): name(String) //姓名 , age(int) //年龄 方法: 构造
23、方法(没有参数,设置姓名为“无名氏”,年龄为20) setName(有一个名为name的String型参数,将name的值设为这个新值), getName (没有参数,返回姓名) setAge(有一个名为age的int型参数,将age的值设为这个新值), getAge (没有参数,返回年龄) isSameAge(有一个参数s,是另一个Student对象的引用,如果两个Student对象的age相同,则返回true,否则返回false) 试题七(编程题,共15分) 试题八(编程题,) … … 参考答案: 选择题:50分 1、CABCC 2、A
24、CCDA 3、CAADCA 4、BCACB 5、BBAA 六、填空题:20分 1、import java.awt.*; 2、implements ActionListener 3、text = new TextField(12); 4、label = new Label(“I love this game”) 5、this.setLayout(new FlowLayout()); 6、text.addActionListener(this); 7、this.setVisible(true); 8、new WindowAdapter() 9、System.exit(
25、); 10、label.setText(text.getText()); 七、程序设计题15分 package cn.zjnu.test; public class Student { private String name; private int age; Student(){ name="无名氏"; age=20; } public String getName() { return name; } public void setName(String name) { this.name = name; } public
26、 int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isSameAge(Student s){ if (this == s) { return true; } if (s instanceof Student) { Student anotherStudent = (Student)s; int n = age; if (n == anotherStudent.
27、age) { return true; } } return false; } } 八、程序设计题15分 package cn.zjnu.test; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class FileTest { public static void main(String[] args) throws IOException { File f1=new File("d:\\abc");
28、 File f2=new File(f1,"HelloWold.txt"); //创建目录 f1.mkdir(); //创建文件 try { f2.createNewFile(); } catch (IOException e) { e.printStackTrace(); } //准备数据 char[]con="我是中国人,我热爱中国;我是师大人,我为师大添砖加瓦".toCharArray(); FileWriter fw=null; try { //将数据写入到test.txt 中 fw=new FileWriter(f2); BufferedWriter bw = new BufferedWriter(fw); bw.write(con); } catch (IOException e) { e.printStackTrace(); } finally{ bw.close(); } } } WWW.MCK&ARM.COM






