绝密*启用前
2011-2012年上海交通大学《Java语言程序设计》期
末试题B卷
(2011—2012学年第 1 学期)
考试形式 笔试(闭卷) 使用学生 11 本科生 考试时间 120 分钟 出卷时间2012年 06月 17日 【说明】
(1) 考生应将全部答案都写在答题纸上,否则作无效处理。
(2) 试题一~五为程序填空选择题,请从供选择答案中为每个空格挑选出一个
正确答案,并写在答题纸上,试题六为程序填空题,试题七八为程序设计题。
试题一(程序选择,每题2分,共10分)
【程序说明】利用下列公式计算pi的近似值,要求精确到10为止。请将程序填充完整或回答相关问题。
Pi/4 ≈ 1 – 1/3 + 1/5 – 1/7 + …
【程序代码】
public class CalculatePI {
public static void main(String[] args) { }
public static double Pi() { }
double result,item; int denominator; result = 0; denominator = 1; (1) C ; do
{ item = (2) A ;
result += item; sign = -sign; denominator +=2;
System.out.printf(\
-6
}while( (3) B ); return result*4;
}
【可供选择的答案】
B. int sign = 0 C. int sign = 1 D. int sign = -1 (1) A. int sign
B. (int)sign/denominator (2) A. (double)sign/denominator
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方法说法正确的是 C 。 A. 一个类不可以没有main方法; B. 一个类可以有多个main方法; C. 一个应用程序可以有多个类,并且每个类可有main方法; D. 一个可执行的应用程序可以没有main方法。 (5) 以下关于main方法前的关键字void说法正确的是 C 。 A. 表示该方法不能输出任何结果; B. 表示该方法的静态方法; C. 表示该方法不返回任何结果; D. 表示该方法的访问权限是公有的。
试题二(程序选择,每题2分,共10分)
【程序说明】以下程序是某个类的一个sort方法,功能是对作为参数传入的int型数组arrayX用冒泡法对其进行由小到大排序,请将程序填充完整或回答相关问题,要求选出最佳答案。 【程序代码】 public void sort( (6)A )
{ }
int t;
for(int k = 0; k < arrayX.length - 1; k++) { }
for (int j = 1; j < (7)D ; j++) { }
if ( (8)A ) { }
t = arrayX[j]; (9) D ; arrayX[j - 1] = t;
【可供选择的答案】
A. int[] arrayX B. int arrayX (6)
C. int[] arrayX[] D. final int[] arrayX
A. arrayX.length - 1 B. arrayX.length (7)
C. arrayX.length - k D. k
A. arrayX[j - 1] > arrayX[j] B. arrayX[j] < arrayX[j - 1] (8)
C. arrayX[j] > arrayX[j - 1] D. arrayX[j] < arrayX[j + 1]
A. t = arrayX[j - 1] B. arrayX[j] = t (9)
C. arrayX[j - 1] = arrayX[j] D. arrayX[j] = arrayX[j - 1]
(10) 以下关于数组的说法错误的是 A 。 A. int型数组可以放直接放double型数据; B. 起始下标为0; C. int型数组可以放直接放byte型数据; D. 下标越界时,会抛出异常
试题三(程序选择,每题2分,共12分)
【程序说明】Triangle为三角形类,方法getArea()用于计算并返回三角形面积(根据三边a,b,c求三角形面积的方法是:令p?请将程序填充完整或回答相关问题。 【程序代码】
public class Triangle {
private double sideA; private double sideB; private double sideC;
public Triangle(double a, double b, double c) (11) C { }
public (12) A getArea()
if((a + b <= c) || (b + c <= a) || (a + c <= b)) {
throw new Exception(\三条边不能构成三角形\} else { }
sideA = a; sideB = b; sideC = c;
a?b?c, 面积s?p(p?a)(p?b)(p?c)),2 }
{ }
double s = 0;
double l = (sideA + sideB + sideC) / 2.0; s = (13) A ; return s;
public class Test { }
public static void main(String[] args) { }
double a = 3.0, b = 4.0 ,c = 5.0;//*** try {
(14) D ;
System.out.println(\三角形面积为\}
(15) C { }
System.out.println(ex.getMessage());
【可供选择的答案】 A. 空 (11)
C. throws Exception
B. throw new Exception() D. throws IOException C. float
D. int
(12)
A. double B. void
(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 - sideC)) A. Triangle tri = new Triangle() B. Triangle tri
C. Triangle triangle = new Triangle(a, b, c) D. Triangle tri = new Triangle(a, b, c) A. catch(IOException ex) C. catch(Exception ex)
B. catch(Exception e) D. catch(IOException e)
(14)
(15)
(16) 该程序的输出结果为 A 。 A. 三角形面积为6.0 B. 三条边不能构成三角形 C. 6.0 D. 程序出错,没有结果
试题四(程序选择,每题2分,共8分)
【程序说明】以下程序为类Person、Animal、Chinese、Zhejiang和Test的代码,请将程
序填充完整或回答相关问题。 【程序代码】
//Person.java package cn.zjnu.ks;
public (22) B class Person { }
//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 void speak(String msg) {
System.out.println(\我会说\}
public Chinese() {
hairColor = \黑色\
System.out.println(\中国人\}
(23) B String hairColor; public abstract void speak(); public Person() {
hairColor=””;
System.out.println(\人类\}