尾部都有答案
第六章练习题(抽象类与接口)
(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)现有
interface Animal {
void eat();
}
// insert code here
public class HouseCat implements Feline {
public void eat() { }
}
//和以下三个接口声明:
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)现自:
interface Color { }
interface Weight { }
//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)现有
abstract class Color {
protected abstract String getRGB();
}
public class Blue extends Color {
//insert code here
}
//和四个声明
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)现有
abstract class Color2 {
//insert code here
}
public class Blue2 extends Color2 {
public String getRGB() { return "blue"; }
}
//和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)现有
class Top {
static int X=l;
public Top() { x*=3; )
}
class Middle extends Top {
public Middle() {x+=l; }
public static void main(String [] args) {
Middle m=new Middle();
System.out.println (x)j
}
}
结果是什么?
A. 2
B. 3
C. 4
D.编译失败
(10)现有两个文件
package X;
public class X {
public static void doX() {System.out.print ("doX"); }
}
//和
import x.X;
class Find {
publiC static void main (String [] args) {
X myX = new X();
myX.doX();
X.doX();
x.X.aoX():
x.X myX2=new x.X();
myx2 .doX();
}
}
结果为
A. Find类中第4行出现一个错误,编译失败。
B. Find类第5行出现一个错误,编译失败。
C. Find类第6行出现一个错误,编译失败。
D. doX doX doX doX
(11)现有
class Tree {
private static String tree = "tree ";
String getTree () { return tree; }
}
class Elm extends Tree {
private static String tree = "elm ";
public static void main (String [] args) {
new Elm() .go (new Tree()) ;
}
void go (Tree t) {
String s = t.getTree () +Elm.tree + tree + (new Elm() .getTree ()) ;
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)现有
interface Animal {
void eat () ;
}
//insert code here
public class HouseCat extends Feline {
public void eat() { }
}
//和五个申明
abstract class Feline implements Animal { }
abstract class Feline implements Animal { void eat () ; }
abstract class Feline implements Animal { public void eat();}
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)现有
interface I { void go(); }
abstract class A implements I { }
class C extends A {
void go(){ )
}
结果是什么?
A. 代码通过编译
B. 由于第1行的错误导致编译失败
C. 由于笫3行的错误导致编译失败
D. 由于第6行的错误导致编译失败
(14)现有
interface Data {public void load();}
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 {
public 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*/}
}
参考答案
1、B
2、BD
3、D
4、C
5、C
6、B
7、C
8、D
9、C
10、D
11、C
12、C
13、D
14、B
15、C