本学校计算机系java程序设计教学使用的教案
C、public void native test(); D、public native test(){} 答:native方法没有方法体,关键字native不能放在方法的返回值前面。A正确。
27、 已知如下类说明: public class Test { private float f = 1.0; int m = 12; static int n=1;
public static void main(String arg[]) { Test t = new Test(); // some code... } }
如下哪个使用是正确的? A、 t.f B、this.n C、Test.m D、Test.n
答:f和m是变量实例.它们必须通过对象来使用。n是类变量,必须通过类名使用。A,D正确。
28、 已知如下代码: 1) class Example{ 2) String str;
3) public Example(){ 4) str= "example"; 5) }
6) public Example(String s){ 7) str=s; 8) } 9) }
10) class Demo extends Example{ 11) }
12) public class Test{ 13) public void f () {
14) Example ex = new Example("Good");
15) Demo d = new Demo("Good"); 16) }
哪句语句会导致错误? A、 line 3 B、line 6 C、line 10 D、line 14 E、line 15
答:代码中没有以一个String作为参数的构造语句,故第15行会出错。E正确。
29、 已知如下类定义: class Base {
public Base (){ //... }
public Base ( int m ){ //... }
protected void fun( int n ){ //... } }
public class Child extends Base{ // member methods }
如下哪句可以正确地加入子类中? A、 private void fun( int n ){ //...} B、void fun ( int n ){ //... }
C、protected void fun ( int n ) { //... } D、public void fun ( int n ) { //... }
答:子类中重载函数的访问级别不能低于父类函数。C,D正确。
30、 如下哪个语句是正确的?
A、 In Java single inheritance is allowed, which makes code more reliable.
B、A subclass inherits all methods ( including the constructor ) from the superclass.
C、 A class can implement as many interfaces as needed.
D、When a class implements an interface, it can define as many methods of the interface as needed.