栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

沈师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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/583717.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号