一. 填空题(共7题,70分)
1. (填空题)接口中是否能声明变量吗?回答是或者否______。
(1) 否
2. (填空题)接口中是否能定义非抽象方法吗?回答是或者否_______。
(1)
否
3. (填空题)可以把实现某一接口的类创建的对象的引用赋给该接口声明的_____变量中。那么该______变量就可以调用被类实现的接口中的方法。
(1)
接口
(2)
接口
4. (填空题)接口中的常量可以不指定初值吗?回答可以或者不可以________。
(1)
不可以
5. (填空题)
可以在接口中只声明常量,不声明抽象方法吗?回答可以或者不可以________。
(1)
可以
6. (填空题)
请说出E类中【代码1】,【代码2】的输出结果_________,__________。
interface A {
double f(double x,double y);
}
class B implements A {
public double f(double x,double y) {
return x*y;
}
int g(int a,int b) {
return a+b;
}
}
public class E {
public static void main(String args[]) {
A a = new B();
System.out.println(a.f(3,5)); //【代码1】
B b = (B)a;
System.out.println(b.g(3,5)); //【代码2】
}
}
(1) 15.0
(2) 8
7. (填空题)
请说出E类中【代码1】,【代码2】的输出结果_________,__________。
interface Com {
int add(int a,int b);
}
abstract class A {
abstract int add(int a,int b);
}
class B extends A implements Com{
public int add(int a,int b) {
return a+b;
}
}
public class E {
public static void main(String args[]) {
B b = new B();
Com com = b;
System.out.println(com.add(12,6)); //【代码1】
A a = b;
System.out.println(a.add(10,5)); //【代码2】
}
}
(1) 18
(2) 15
二. 单选题(共2题,20分)
8. (单选题)
下列哪个叙述是正确的?
- A. 一个类最多可以实现两个接口。
- B. 如果一个抽象类实现某个接口,那么它必须要重写接口中的全部方法。
- C. 如果一个非抽象类实现某个接口,那么它可以只重写接口中的部分方法。
- D. 允许接口中只有一个抽象方法。
D :允许接口中只有一个抽象方法。;
9. (单选题)
将下列(A,B,C,D)哪个代码替换下列程序中的【代码】不会导致编译错误。
interface Com {
int M = 200;
int f();
}
class ImpCom implements Com {
【代码】
}
- A. public int f(){return 100+M;}
- B. int f(){return 100;}
- C. public double f(){return 2.6;}
- D. public abstract int f();
A :public int f(){return 100+M;};
三. 多选题(共1题,10分)
10. (多选题)
下列接口中标注的(A,B,C,D)中,哪两个是错误的?
interface Takecare {
protected void speakHello(); //A
public abstract static void cry(); //B
int f(); //C
abstract float g(); //D
}
- A. protected void speakHello();
- B. public abstract static void cry();
- C. int f();
- D. abstract float g();
AB