abstract public void myfunc(); public void another(){
System.out.println(\ public class Abs extends Base{
public static void main(String argv[]){ Abs a = new Abs(); a.amethod(); }
public void myfunc(){
System.out.println(\ } public void amethod(){ myfunc(); }}
[答案] 编译出错。Base类必须声明为抽象类,因为它包含myfunc()抽象方法。10.以下哪些代码是合法的?
a) public abstract method();
b) public abstract void method(); c) public abstract void method(){} d) public virtual void method();
e) public void method() implements abstract;
11.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? abstract class Base{
abstract public void myfunc(); public void another(){
System.out.println(\ public class Abs extends Base{
public static void main(String argv[]){ Abs a = new Abs(); a.amethod(); }
public void myfunc(){
System.out.println(\ public void amethod(){ myfunc(); } }
[答案] 打印“My func”
12.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? import java.io.FileNotFoundException; 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(){
System.out.println(\ amethod();
System.out.println(\ } }
11
[答案] 打印―Pausing Continuing‖ 13.对于以下代码: public class Tester {
public long sum(long a, long b) { return a + b; } // 此处插入一行}
下面哪些语句放到以上插入行,可以编译通过? a) public int sum(int a, int b) { return a + b; } b) public int sum(long a, long b) { return 0; } c) abstract int sum();
d) private long sum(long a, long b) { return a + b; } e) public long sum(long a, int b) { return a + b; } 第7章 Java语言中的修饰符 1.以下哪些是Java修饰符? a) public b) private c) friendly
d) transient e) vagrant
2.作为程序入口的main()方法可以用哪些修饰符来修饰? a) private b) final c) static d) int e) abstract
3.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? private class Base{} public class Vis{ transient int iVal;
public static void main(String elephant[]){ }} [答案] 编译出错。Base类不能用private修饰。
4.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? class A{
private int secret;} public class Test{
public int method(A a){ return a.secret++; }
public static void main(String args[]){ Test test=new Test(); A a=new A();
System.out.println(test.method(a)); } }
[答案] 编译出错。Test类的method()方法不能访问类A的secret变量。 5.哪个访问控制符的访问级别最大? a) private b) public c) protected d) 默认
6.如果一个方法只能被当前类以及同一个包中的类访问,这个方法采用什么访问级别?
12
[答案] 默认访问级别
7.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? private class Base{ Base(){ int i = 100;
System.out.println(i); } }
public class Pri extends Base{ static int i = 200;
public static void main(String argv[]){ Pri p = new Pri(); System.out.println(i); }
}[答案] 编译出错。Base类不能用private修饰。
8.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? public class MyAr{
public static void main(String argv[]) { MyAr m = new MyAr(); m.amethod(); }
public void amethod(){ static int i;
System.out.println(i); } }
[答案]编译出错。变量i是局部变量,不能用static修饰。
9.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? class Test{ int x = 5;
static String s = \ public static void method(){ System.out.println(s + x);
} }[答案] 编译出错。在静态method()方法中不能直接访问实例变量x。10.对于以下程序,运行―java StaticTest‖,得到什么打印结果? public class StaticTest { static {
System.out.println(\ } public void print() {
System.out.println(\ }
public static void main(String args []) { StaticTest st1 = new StaticTest(); st1.print();
StaticTest st2 = new StaticTest(); st2.print(); } } [答案] 打印以下内容:
13
Hi there Hello Hello
11.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? public class Test{ final int x = 0; Test(){ x = 1; }
final int aMethod(){ return x;} }
[答案] 编译出错。在Test()构造方法中不能再为常量x赋值。 12.以下代码能否编译通过? public class FinalTest{ final int q; final int w = 0; FinalTest(){ q = 1; }
FinalTest(int x){ q = x; } } [答案] 编译通过。
13.以下代码能否编译通过? public class FinalTest{ final int q; final int w = 0; FinalTest(){ this(0); q = 1; }
FinalTest(int x){ q = x; } }
[答案] 编译出错。―q=x;‖语句出错。FinalTest(int x)构造方法实际上为变量q赋了两次值。 第8章 接口
1.接口与抽象类有哪些异同点? [答案] 相同点:(1)代表系统的抽象层。 (2)都不能被实例化。
(3)都能包含抽象方法,这些抽象方法用于描述系统能提供哪些服务,但不必提供具体的实现。 不同点:
(1)在抽象类中可以为部分方法提供默认的实现,从而避免在子类中重复实现它们,这能提高代码的可重用性,这是抽象类的优势所在;而接口中只能包含抽象方法。
(2)一个类只能继承一个直接的父类,这个父类有可能是抽象类;但一个类可以实现多个接口,这是接口的优势所在。
2.区分接口与抽象类分别在什么场合使用? [答案]总的使用原则如下:
14
(1)用接口作为系统与外界(即令一个系统)交互的窗口。
(2)用抽象类来定制系统中的扩展点。可以把抽象类看作是介于―抽象‖和―实现‖之间的半成品,抽象类力所能及的完成了部分实现,但还有一些功能有待于它的子类去实现。 3.以下代码能否编译通过,假如能编译通过,运行时得到什么打印结果? import java.awt.event.*; import java.awt.*;
public class MyWc extends Frame implements WindowListener{ public static void main(String argv[]){ MyWc mwc = new MyWc(); }
public void windowClosing(WindowEvent we){ System.exit(0);
}//End of windowClosing public void MyWc(){ setSize(300,300);
setVisible(true); }}//End of class
[答案] 编译出错。MyWc类没有实现WindowListener接口中的所有方法,因此MyWc类应该声明为抽象类。
4.以下代码能否编译通过? interface A{ int x = 0; A(){ x= 5; } A(int s){ x = s;}}
[答案] 编译出错。接口中不能有构造方法。 5.接口中的方法可以使用哪些修饰符? a) static b) private
c) synchronised d) protected e) public
6.以下是接口I的定义: interface I {
void setValue(int val); int getValue(); }
以下哪些代码能编译通过? (a) class A extends I { int value;
void setValue(int val) { value = val; } int getValue() { return value; }} b) interface B extends I { void increment(); }
c) abstract class C implements I { int getValue() { return 0; }
15