跳到主要内容

2016-2017学年上学期期末

2016-2017学年上学期期末试卷(含答案)​

一、单项选择题(每题 3 分,共 15 分)​

  1. 在 JAVA 语言中,变量、常量等标识符必须遵循一定的规则。以下哪一项不是合法的标识符?

    A. int1

    B. 1int

    C. __int

    D. INT

    答案:

    B


  2. 以下哪句话描述不正确?

    A. JAVA 源文件的后缀名是“.java”

    B. JAVA 源文件的后缀名是“.class”

    C. 每个 JAVA 源文件中最多包含一个由 public 修饰的类或接口

    D. 一个 JAVA 源文件中可以包含由 public 修饰的类或接口,也可以不包含

    答案:

    B


  3. 关键词“final”在 JAVA 中非常重要,以下哪一项不是 final 的功能?

    A. 用在异常处理机制中,描述必须要执行的一段代码

    B. 用于修饰一个类,表示这个类无法被继承

    C. 用于修饰类中的成员变量,表示该变量的值无法被更改

    D. 用于修饰类中的方法,表示该方法无法被子类重写

    答案:

    A


  4. JAVA 语言支持数组操作,以下语句哪一句是不正确的?

    A. int a[] = new int(5);

    B. int[] a = new int(5);

    C. int a[] = {1,2,3};

    D. int a[] = int{1,2,3};

    答案:

    D

    提示

    原题 A、B 均使用 new int(5),D 使用 int{1,2,3},按 Java 语法均不能编译;原参考答案仅给出 D。此处按原题与原参考答案保留。


  5. 假设文件“file.txt”存在,且包含 ASCII 文本。

    try {
    File f = new File("file.txt");
    OutputStream out = new FileOutputStream(f, true);
    }
    catch(IOException){}

    以上代码的运行结果是:

    A. 代码不能编译成功

    B. 代码将运行并且原文件不会发生变化

    C. 代码将运行并且设置该文件的长度为零

    D. 因为文件没有被关闭,所以会抛出异常

    答案:

    A


二、读程序题(每题 5 分,共 25 分)​

  1. 给出下面程序的运行结果:

    class A{
    public static void main(String args[]){
    String s = "2011";
    int a = 10;
    System.out.println(s+1);
    System.out.println(1+s);
    System.out.println(a+(a+1)+s);
    System.out.println(s+a+(a+1));
    }
    }
    答案:
    20111
    12011
    212011
    20111011

  2. 给出下面程序的运行结果:

    public class A{
    public static void main(String[] args){
    String s1 = "foo";
    String s2 = new String("foo");
    If(s1==s2)
    System.out.println("equal");
    System.out.println("how to compare two string");
    }
    }
    答案:
    How to compare two string
    提示

    原题将 Java 关键字 if 写为大写 If,原样代码无法编译;原参考答案按小写 if 的意图给出了上述输出。此处按原题与原参考答案保留。


  3. 给出下面程序的运行结果:

    class A{
    String name;
    public A(){
    System.out.println("class a");
    }
    }
    class B extends A{
    int price;
    B(String name_, int price_){
    name = name_;
    price = price_;
    System.out.println("name and price"+""+name+""+price);
    }
    }
    public class TextInherite {
    public static void main(String args[]){
    B b = new B("icecream",10);
    }
    }
    答案:
    class a
    name and price icecream 10

  4. 给出下面程序的运行结果:

    class A{
    void play(){
    System.out.println("A plays.");
    }
    }
    class B extends A{
    void play(){
    System.out.println("B plays.");
    }
    public static void main(String args[]){
    A ab = new B();
    ab.play();
    ab=(A)ab;
    ab.play();
    }
    }
    答案:

    略


  5. 给出下面程序的运行结果:

    package sei;
    class Person
    {
    public int age=20;
    public Percon(int age)
    {
    this.age=age;
    }
    }
    public class TestSwap
    {
    public static void main(String[] args)
    {
    int a = 10,b = 20;
    swap(a,b);
    System.out.println(a+","+b);
    Person tom = new Person(10);
    Person jack = new Person(20);
    Swap(tom,jack);
    System.out.println(tom.age+","+jack.age);
    }
    public static void swap(int a,int b)
    {
    a=a+b;
    b=a-b;
    a=a-b;
    }
    public static void swap(Person a,Person b)
    {
    a.age=a.age+b.age;
    b.age=a.age-b.age;
    a.age=a.age-b.age;
    }
    }
    答案:

    略

    提示

    原题中构造函数写为 Percon,与类名 Person 不一致;调用还写为大写 Swap(tom,jack),与已声明的 swap 不一致,因此原样代码无法编译。原参考答案仅写“略”,此处均按原文保留。


三、改错和简答题(每题 6 分,共 30 分)​

  1. 以下代码计算 1+2+⋯+1001+2+\cdots+100 的值,但有两个错误,指出并改正。

    public class A{
    int sum;
    public static void main(String args[]){
    int i=1;
    for(;i<100;i++){
    sum += i;
    }
    System.out.println("The sum is:"+sum);
    }
    }
    答案:
    int i = 0;
    i<=100

  2. 该程序有两个错误,请指出并改正。

    public class SimpleClass{
    double d;
    public SimpleClass(double t){
    d=t;
    }
    public void method(){
    System.out.println("the result is"+d);
    }
    public static void main(String args[]){
    d=20.9;
    SimpleClass sc=new SimpleClass();
    sc.method();
    SimpleClass sc1 = new SimpleClass(22);
    sc1.method();
    }
    }
    答案:

    d=20.9 未定义类型。

    SimpleClass sc=new SimpleClass(); 构造函数不存在。


  3. 该程序有两个错误,请指出并改正。

    package package2;
    public class person{
    String name;
    public person(){
    name="";
    }
    public person(String n){
    name=n;
    }
    protected void setName(String n){
    name=n;
    }
    }

    package package1;
    import package2.*;
    public class student extends person{
    person p;
    public student(){}
    public void setName(String n){
    super.setName(n);
    p=new person();
    p.setName();
    }
    }
    答案:

    protected void setName() 只能在相同包内访问,应为 public。

    p.setName(); 缺少参数。


  4. 请描述关键字 super 和 this 的区别。


  5. 重载(overload)和重写(override)是 Java 中的常见概念,请谈谈你对两个概念的理解。


四、编程题(共 30 分)​

  1. 请写一个函数,有三个字符串参数 a、b、c。请实现将字符串 a 中所有的 b 都替换成 c。(String.replaceAll 函数不可以使用)(10 分)


  2. 假设有一个文件 word.txt,内容如下:

    Work,工作
    Question,问题
    Substitution,替换
    Pension,养老金
    ……

    请写一个程序读入此文件,并提供给用户查询。用户可以通过输入单词查询意思。输入“-1”时,程序退出。(20 分)