被空白代替B能编译
被private代替B不能编译 被protected代替B能编译 11、 下面代码中A类和B类不在同一个包中,如果问号分别被空白代替、private代替、
protected代替,B类能编译吗?
package p1; public class A{ ? int i; }
被空白代替B不能编译
被private代替B不能编译
被protected代替B不能编译 12、 为什么下面四行代码可以编译,但会导致运行时错误?
(1) Number numberRef=new Integer(0);
(2) Double doubleRef=(Double)numberRef; (3) Number[] numberArray=new Integer[2];
(4) numberArray=new Double(1.5);
(1)(2)因为Integer和Double不是基本数据类型。强制转换只能转换包含继承关系的类或基本类型数据。
(3)(4)因为整数数组里不能放入非整数 13、 (1) (2)
(3)
(4)
class A{ abstract void unfinished(); }
下面抽象类和接口的定义中,哪些是合法的?
class A{ abstract void unfinished(){ } } ? void m(){ ....................... } } package p2; public classBextends A{ public void m1(String[] args){ System.out.println(i); m(); } public class abstract A{ abstract void unfinished(); }
abstract class A{ protected void unfinished(); } (5) (6)
(7)
(8)
(9)
(10)
interface A{ void print(); }
interface A{ void print(){ } ; }
abstract class A{ protected int unfinished(); }
abstract class A{ abstract void unfinished(); } abstrace interface A extends I1,I2{ abstract void print(){ } ; }
abstract interface A{ print(); } 合法的是(4),(5),(6),(9),(10) 14、 下面程序有什么错误?
public class Test{
public static void main(String[] args){ nPrintln(“Welcome to Java!”,5);
}
public static void nPrintln(String message,int n){ int n=1;
for (int i=0;i System.out.println(message); } } 定义了两个同名变量n 15、 下面程序有什么错误? Public class Test{ public static void method(int x){ } public static void method(int y){ return y; } } Method方法重复定义了,没有达到重载的条件。 16、 下面程序有什么错误? Public class Test{ public static method1(int n,m){ n+=m; method2(3.4); } public static int method2(int n){ if(n>0) return 1; else if(n==0) return 0; else if(n<0) return -1; } } 没有定义method1函数的返回类型。 17、 下面程序有什么错误? (1) public class ShowErrors{ public static void main(String[] args){ ShowErrors t=new ShowErrors(5); } } ShowErrors(int)没有定义,应该是ShowErrors(). (2) public class ShowErrors{ public static void main(String[] args){ ShowErrors t=new ShowErrors(); t.x(); } } X()没有定义,实例不能引用。 (3) public class ShowErrors{ public void method1(){ Circle c; System.out.println(“what is radius”+c.getRadius()); c=new Circle(); ShowErrors t=new ShowErrors(5); } } ShowErrors(int)没有定义,应该是ShowErrors(). (4) public class ShowErrors{ public static void main(String[] args){ C c=new C(5.0); System.out.println(c.value); } } class C{ int value=2; } C(int)没有定义,应该是C() 18、 下面代码中的错误是什么? public class Test{ public static void main(String[] args){ Object fruit=new Fruit(); Object apple=(Apple)fruit; } } class Apple extends Fruit{ } class Fruit{ } fruit不能被强制转换成apple 19、 下面的代码有什么错误? public class Test{ public static void main(String[] args){ Number x=new Integer(3); System.out.println(x.intValue()); System.out.println((x.compareTo(new Integer(4))); } } compareTo方法是字符类方法,Number里没有定义。 20、 下面的代码有什么错误? public class Test{ public static void main(String[] args){ Number x=new Integer(3); System.out.println(x.intValue()); System.out.println(((Integer)x.compareTo(new Integer(4))); } } compareTo方法是字符类方法,Number里没有定义,且不能被强制转换。 21、
相关推荐: