第六章练习题(抽象类与接口)
1.下列有关抽象类的叙述正确的是哪项?
A.抽象类中一定含有抽象方法
B.抽象类的声明必须包含abstract关键字
C.抽象类既能被实例化也能被继承
D.抽象类中不能有构造方法
2.下列有关抽象方法叙述正确的是哪项?(选两项)
A. 抽象方法和普通方法一样,只是前面多加一个修饰符asbtract
B.抽象方法没有方法体
c.抽象方法可以包含存任何类中
D.包含抽象方法的类的具体子类必须提供具体的覆盖方法
3.下列有关接口的叙述错误的是哪项?
A.接口中只能包含抽象方法和常量
B.一个类可以实现多个接口
C.类实现接口时必须实现其中的方法
D.接口不能被继承
4.下列关于接口的定义哪项是正确的?
A. interface C{int a;)
B. public interface A implements B {)
C. public interface A {int a(); )
D. abstract interface D {)
5.现有:
1. interface Animal f
2. void eat();
3. }
4.
5. // insert code here
6.
7. public class HouseCat implements Feline {
8. public void eat() { }
9. }
和以下三个接口声明:
interface Feline extends Animal ( )
interface Feline extends Animal {void eat(); }
interface Feline extends Animal {void eat() { } }
分别插入到第5行,有多少行可以编译?
A. 0
B. 1
C. 2
D. 3
6.现自:
1. interface Color { }
2. interface Weight { }
3. //insert code here
和以下足六个声明:
class Boat extends Color, extends Weight { }
class Boat extends Color and Weight { }
class Boat extends Color, Weight { }
class Boat implements Color, implements Weight { }
class Boat implements Color and Weight { }
class Boat implements Color, Weight { }
分别插入到第3行,有多少行可以编译?
A. 0
B. 1
C. 2
D. 3
7.现有:
1. abstract class Color {
2.protected abstract String getRGB();
3. }
4.
5. public class Blue extends Color {
6. //insert code here
7. }
和四个声明:
public String getRGB() { return "blue"; }
String getRGB() { return "blue"; )
private String getRGB() { return "blue"; }
protected String getRGB() { return "blue"; )
分别插入到第6行,有几个可以通过编译?
A. 0
B. 1
C. 2
D. 3
8.现有:
1. abstract class Color2 {
2. //insert code here
3. }
4.
5. public class Blue2 extends Color2 {
6.public String getRGB() { return "blue"; }
7. }
和4个声明:
public abstract String getRGB();
abstract String getRGB();
private abstract String getRGB();
protected abstract String getRGB();
分别插入到第2行,有多少行可以编译?
A. O
B. 1
C. 2
D 3
9.现有:
1. class Top {
2. static int X=l;
3. public Top() { x*=3; )
4. }
5. class Middle extends Top {
6.public Middle() {x+=l; }
7.public static void main(String [] args) {
8.Middle m=new Middle();
9.System.out.println (x)j
10. }
11. }
结果是什么?
A. 2
B. 3
C. 4
D.编译失败
10.现有两个文件:
1. package X;
2. public class X {
3. public static void doX() {System.out.print ("doX"); }
4. }
和:
1.import x.X;
2. class Find {
3. publiC static void main (String [] args) {
4. X myX=new X(); myX.doX();
5. X.doX();
6. x.X.aoX():
7. x.X myX2=new x.X(); myx2 .doX();
8. } ’
9. }
结果为:
A. Find类中第4行出现一个错误,编译失败。
B. Find类第5行出现一个错误,编译失败。
C. Find类第6行出现一个错误,编译失败。
D. doX doX doX doX
11.现有:
1. class Tree {
2. private static String tree = "tree ";
3. String getTree () { return tree; }
4. }
5. class Elm extends Tree {
6. private static String tree = "elm ";
7. public static void main (String [] args) {
8. new Elm() .go (new Tree()) ;
9. }
10. void go (Tree t) {
11. String s = t.getTree () +Elm.tree + tree + (new Elm() .getTree ()) ;
12. System.out.println (s) ;
结果为:
A. elm elm elm elm
B. tree elm elm elm
C. tree elm elm tree
D. tree elm tree elm
12.现有:
1. interface Animal {
2. void eat () ;
3. }
4.
5. //insert code here
6.
7. public class HouseCat extends Feline {
8. public void eat() { }
9. }
和五个申明
abstract class Feline implements Animal { }√
abstract class Feline implements Animal { void eat () ; } ×eat方法需要abstract修饰
abstract class Feline implements Animal { public void eat();}
×eat方法需要abstract修饰
abstract class Feline implements Animal { public void eat() {} }√
abstract class Feline implements Animal { abstract public void eat();}√
A. 1
B. 2
C. 3
D.4
· 13.现有:
1. interface I { void go(); }
2.
3. abstract class A implements I { }
4.
5. class C extends A {
6. void go(){ )
7. }
结果是什么?
A.代码通过编译
B.由于第1行的错误导致编译失败
C.由于笫3行的错误导致编译失败
D.由于第6行的错误导致编译失败//前面应该加public
14.现有:
1. interface Data {public void load();}
2. abstract class Info {public abstract void load();}
下列类定义中正确使用Data和Info的是哪项?
A. public class Employee implements Info extends Data {
public void load() {/*do something*/)
)
B.public class Employee extends Inf.implements Data{
public void load() {/*do something*/}
}
c.public class Empl.yee implements Inf extends Data{
public void Data.1oad() {* do something*/}
public void load(){/*do something*/}
)
D.public class Employee extends Inf implements Data {
public void Data.1oad() {/*do something*/)
public void info.1oad(){/*do something*/)
)
15. 下列代码正确的是哪项?
A. public class Session implements Runnable, Clonable{
public void run ();
public Object clone () ;
}
B. public class Session extends Runnable, Cloneable {
public void run() {/*do something*/}
public Object clone() {/*make a copy*/}
}
C. public abstract class Session
implements Runnable, Clonable {
pu)olic void run() {/*do something*/}
public Object clone() {/*make a copy*/}
}
D. public class Session
implements Runnable, implements Clonable {
public void run() {/*do something*/}
public Object clone() {/*make a copy*/}
}
参考答案
1B
2BD
3D
4C
5C
6B
7C
8D
9C
10 D
11 C
12 C
13 D
14 B
15 C