沈师PTA--JAVA程序设计-第4章习题集(2)--填空题答案版

沈师PTA--JAVA程序设计-第4章习题集(2)--填空题答案版,第1张

沈师PTA--JAVA程序设计-第4章习题集(2)--填空题答案版

R4-1

用 final关键字修饰的类不能被继承。

R4-2

下列代码运行结果是什么?

public class Main{
  static{
     System.out.print("Hi here,");
  }
  public void print(){
     System.out.print("Hello");
  }
  public static void main(String args[]){
     Main m1=new Main();
     m1.print();
     Main m2=new Main();
     m2.print();
  }
}

上述代码的运行结果为:
Hi here,HelloHello

R4-3

给出以下代码:

class Number {
    public int i;
    public Number(int ii) {i=ii;};
}
public class Main {
    static void f(Number n) {
        n.i = 9;
    }
    static void g(Integer n) {
        n=9;
    }
    public static void main(String[] args) {
        Number k = new Number(10);
        f(k);
        Integer m = new Integer(10);
        g(m);
        System.out.println(k.i+":"+m);
    }
}

程序运行后输出结果是:
9:10

R4-4

若要使一个方法能不依赖与该类对象而存在,则需使用的关键字static

R4-5

请写出以下程序运行结果:

class Window {
    Window(int marker) { System.out.println("Window(" + marker + ")"); }
}
class House {
    Window w1 = new Window(1); 
    House() {
        System.out.println("House()");
        w3 = new Window(33); 
    }
    Window w2 = new Window(2); 
    void f() {
        System.out.println("f()");
    }
    static Window w3 = new Window(3); 
}

public class Est {
    public static void main(String[] args) {
        House h = new House();
        h.f(); 
    }
}

Window(3)
Window(1)
Window(2)
House()
Window(33)
f()

R4-6

运行下列代码,运行结果是什么?

public class Main{
  int i=2;
  static int is;
  static{
    System.out.println("in static block");
    is=5;
    System.out.println("static variable is="+is);
  }
  {
    System.out.println("in non-static block");
    i=8;
  }
  Main(){
    i=10;
  }
  public static void main(String args[]){
    System.out.println("in main()");
    Main m1=new Main();
    System.out.println(m1.i);
  }
}

运行上述代码,则运行结果为:
in static block
static variable is=5
in main()
in non-static block
10

R4-7

请写出以下程序运行结果:

class Letter {
    char c;
}
public class Main {
    static void f(Letter y) {
        y.c = 'z';
    }

    public static void main(String[] args) {
        Letter x = new Letter();
        x.c = 'a';
        f(x);
        System.out.println(x.c);
    }
}

z

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5574926.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存