abstract void increment(); } d) interface D implements I { void increment();} (e) class E implements I { int value;
public void setValue(int val) { value = val; }} 7.以下代码能否编译通过? interface A{ int x;
void A(int s){ x = s;}}
[答案] 编译出错。必须为常量x赋值;方法A(int s)应该是抽象方法。 第9章 异常处理
1.throw和throws关键字有什么区别?
[答案] throw用于在程序代码中抛出异常对象;throws用于声明方法可能抛出的异常。 2.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? import java.io.*; class Base{
public static void amethod()throws FileNotFoundException{} } public class ExcepDemo extends Base{ public static void main(String argv[]){ ExcepDemo e = new ExcepDemo(); } public static void amethod(){}
protected ExcepDemo() throws IOException{
DataInputStream din = new DataInputStream(System.in); System.out.println(\ din.readChar();
System.out.println(\ this.amethod(); } } [答案] 编译出错。ExcepDemo()构造方法声明抛出IOException,因此在main()方法中调用ExcepDemo()构造方法时,要么捕获IOException,要么在方法声明中声明抛出该异常。 3.try 代码块后面可以只跟finally代码块,这句话对吗? [答案] 正确 4.对于以下程序,运行―java Rethrow‖,将得到什么打印结果? public class Rethrow {
public static void g() throws Exception { System.out.println(\ throw new Exception(\ } public static void main(String []args) { try { g();
}catch(Exception e) {
System.out.println(\ e.printStackTrace();
throw new NullPointerException(\ [答案] 打印如下内容:
16
Originates from g() Caught in main
java.lang.Exception: thrown from g() at Rethrow.g(Rethrow.java:4) at Rethrow.main(Rethrow.java:8)
Exception in thread \ at Rethrow.main(Rethrow.java:12)
5.假定方法f()可能会抛出Exception,以下哪些代码是合法的? a) public static void g(){ try { f();
}catch(Exception e) {
System.out.println(\
throw new Exception(\ b) public static void g(){ try { f();
}catch(Exception e) {
System.out.println(\
throw new NullPointerException(\ c) public static void g() throws Throwable{ try { f();
}catch(Exception e) {
System.out.println(\ throw e.fillInStackTrace(); } }
public static void main(String []args) { try { g();
} catch(Exception e) {
System.out.println(\ e.printStackTrace(); }}
6.在Java中,Throwable是所有异常类的祖先,这句话对吗 [答案] 正确
7.在执行trythis()方法时,如果problem()方法抛出Exception,程序将打印什么结果? public void trythis() { try {
System.out.println(\ problem();
} catch (RuntimeException x) { System.out.println(\ return;
} catch (Exception x) { System.out.println(\ return;
17
} finally {
System.out.println(\ System.out.println(\ [答案] 打印: 1 3 4 8.以下代码能否编译通过,假如能编译通过,运行―java Qb4ab‖时得到什么打印结果? class MyException extends Exception {} public class Qb4ab { public void foo() { try { bar(); } finally { baz();
} catch (MyException e) {} }
public void bar() throws MyException { throw new MyException();}
public void baz() throws RuntimeException { throw new RuntimeException();} }
[答案] 编译出错。foo()方法中,catch代码块应该紧跟在try代码块后面。9.对于以下代码: import java.io.*; public class Th{
public static void main(String argv[]){ Th t = new Th(); t.amethod();}
public void amethod(){ try{ ioCall();
}catch(IOException ioe){} } }
以下哪些是合理的ioCall()方法的定义? a) public void ioCall () throws IOException{
DataInputStream din = new DataInputStream(System.in); din.readChar();}
b) public void ioCall () throw IOException{
DataInputStream din = new DataInputStream(System.in); din.readChar(); } c) public void ioCall (){
DataInputStream din = new DataInputStream(System.in); din.readChar();}
d) public void ioCall throws IOException(){
DataInputStream din = new DataInputStream(System.in); din.readChar();}
第10章 类的生命周期 1.类的初始化有哪些时机? [答案] 类的初始化有以下时机:
18
(1)创建类的实例。创建类的实例的途径包括:用new语句创建实例,或者通过反射、克隆以及反序列化手段来创建实例。 (2)调用类的静态方法。
(3)访问某个类或接口的的静态变量,或者对该静态变量赋值。 (4)调用Java API中某些反射方法,比如调用Class.forName(\方法,假如Worker类还没有被初始化,那么forName()方法就会初始化Worker类,然后返回代表这个Worker类的Class实例。forName()方法是java.lang.Class类的静态方法。
(5)初始化一个类的子类。例如对Sub类的初始化,可看作是对它父类Base类的主动使用,因此会先初始化Base类。
(6)Java虚拟机启动时被标明为启动类的类。例如对于―java Sample‖命令,Sample类就是启动类,Java虚拟机会先初始化它。
2.类的加载采用父亲委托机制,它有什么优点?
[答案] 父亲委托机制的优点是能使提高软件系统的安全性。因为在此机制下,用户自 定义的类加载器不可能加载应该由父加载器加载的可靠类,从而防止不可靠甚至恶意的 代码代替由父加载器加载的可靠代码。 3.类在什么情况下被卸载?
[答案] 当代表一个类(以Sample类为例)的java.lang.Class对象不再被引用,即不可 触及,那么Class对象就会结束生命周期,Sample类在方法区内的数据也会被卸载,从 而结束Sample类的生命周期。由此可见,一个类何时被卸载,取决于代表它的Class 对象何时结束生命周期。
4.什么叫运行时包?它有什么优点?
[答案] 由同一类加载器加载的属于相同包的类组成了运行时包。运行时包的优点在于, 只有属于同一运行时包的类才能互相访问包可见(即默认访问级别)的类和类成员。这 样的限制能避免用户自定义的类冒充核心类库的类,去访问核心类库的包可见成员。 5.对于以下代码,运行―java Sample‖,将得到什么打印结果? public class Sample{ static int a=1; static{ a=2; } static{ a=4; }
public static void main(String args[]){ a++;
System.out.println(\ } [答案] 打印: a=5
第 11章 对象的生命周期
1.以下类是否具有默认构造方法? public class Counter { int current, step;
public Counter(int startValue, int stepValue) { set(startValue);
setStepValue(stepValue); } public int get() { return current; }
public void set(int value) { current = value; }
public void setStepValue(int stepValue) { step = stepValue; } } [答案] 没有默认构造方法
19
2.构造方法可以被哪些修饰符修饰?
a) final b) static c) synchronized d) native e) private
3.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? public class Hope{
public static void main(String argv[]){ Hope h = new Hope(); } protected Hope(){
for(int i =0; i <10; i ++){ System.out.println(i);} } }
[答案] 打印:0 1 2 3 4 5 6 7 8 9
4.以下代码能否编译通过,假如能编译通过,运行―java B‖时得到什么打印结果? class A { int i; A(int i) {
this.i = i * 2; }}
public class B extends A {
public static void main(String[] args) { B b = new B(2); }
B(int i) { System.out.println(i); } }
[答案] 编译出错。类B的B(int i)要求父类A提供默认的构造方法。5.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? public class Mystery { String s;
public static void main(String[] args) { Mystery m = new Mystery(); m.go(); }
void Mystery() {
s = \ } void go() {
System.out.println(s); } } [答案] 打印:null 6.对于以下代码: class Base{ Base(int i){
System.out.println(\ Base(){} }
public class Sup extends Base{
public static void main(String argv[]){ Sup s= new Sup(); //One } Sup(){ //Two}
public void derived(){ //Three } }
20