尾部都有答案
第六章練習(xí)題(抽象類與接口)
(1)下列有關(guān)抽象類的敘述正確的是哪項(xiàng)券犁?
A. 抽象類中一定含有抽象方法
B. 抽象類的聲明必須包含abstract關(guān)鍵字
C. 抽象類既能被實(shí)例化也能被繼承
D. 抽象類中不能有構(gòu)造方法
(2)下列有關(guān)抽象方法敘述正確的是哪項(xiàng)钻注?(選兩項(xiàng))
A. 抽象方法和普通方法一樣缓艳,只是前面多加一個修飾符asbtract
B. 抽象方法沒有方法體
C. 抽象方法可以包含存任何類中
D. 包含抽象方法的類的具體子類必須提供具體的覆蓋方法
(3)下列有關(guān)接口的敘述錯誤的是哪項(xiàng)令漂?
A. 接口中只能包含抽象方法和常量
B. 一個類可以實(shí)現(xiàn)多個接口
C. 類實(shí)現(xiàn)接口時(shí)必須實(shí)現(xiàn)其中的方法
D. 接口不能被繼承
(4)下列關(guān)于接口的定義哪項(xiàng)是正確的旗闽?
A. interface C{int a玄括;)
B. public interface A implements B {)
C. public interface A {int a()赵辕; )
D. abstract interface D {)
(5)現(xiàn)有
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)現(xiàn)自:
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)現(xiàn)有
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)現(xiàn)有
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)現(xiàn)有
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
}
}
結(jié)果是什么召衔?
A. 2
B. 3
C. 4
D.編譯失敗
(10)現(xiàn)有兩個文件
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()苍凛;
}
}
結(jié)果為
A. Find類中第4行出現(xiàn)一個錯誤,編譯失敗兵志。
B. Find類第5行出現(xiàn)一個錯誤醇蝴,編譯失敗。
C. Find類第6行出現(xiàn)一個錯誤想罕,編譯失敗悠栓。
D. doX doX doX doX
(11)現(xiàn)有
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) ;
}
}
結(jié)果為
A. elm elm elm elm
B. tree elm elm elm
C. tree elm elm tree
D. tree elm tree elm
(12)現(xiàn)有
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)現(xiàn)有
interface I { void go(); }
abstract class A implements I { }
class C extends A {
void go(){ )
}
結(jié)果是什么?
A. 代碼通過編譯
B. 由于第1行的錯誤導(dǎo)致編譯失敗
C. 由于笫3行的錯誤導(dǎo)致編譯失敗
D. 由于第6行的錯誤導(dǎo)致編譯失敗
(14)現(xiàn)有
interface Data {public void load();}
abstract class Info {public abstract void load();}
下列類定義中正確使用Data和Info的是哪項(xiàng)按价?
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)下列代碼正確的是哪項(xiàng)?
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