1、给定以下代码,如何调用base类的构造方法输出字符串 class base{ base(int i){ system.out.println("base constructor"); } base(){ } } public class sup extends base { public static void main(string[] args) { sup s = new sup(); //位置1 } sup(){ //位置2 } public void derived(){ //位置3 } } a、在位置1调用base(10) b、在位置1调用super(10) c、在位置2调用super(10) d、在位置3调用super(10)
2、给定以下代码,请问哪一个方法将会执行? class happy{ public int getlength(){ system.out.println("int version"); } } class life extends happy { public long getlength(){ system.out.println("long version"); } public static void main(string args[]){ happy e = new life(); e.getlength(); } } a、int version b、long version c、编译错误 d、运行错误 e、编译正确,没有输出
3、以下代码,哪一行会报错 class example { string str; public example(){ str = "example"; } public example(string s){ str = s; } } class demo extends example{ } public class test{ public void f(){ example ex = new example("good"); demo d = new demo("good"); } } a、行4 b、行8 c、行13 d、行17 e、行18
4、以下代码运行结果是 class exsuper{ string name; string nick_name; public exsuper(string s,string t){ name = s; nick_name = t; } public string tostring(){ return name; } } public class example extends exsuper { public example(string s,string t){ super(s,t); } public string tostring(){ return name " a.k.a " nick_name; } public static void main(string[] args) { exsuper a = new exsuper("first","1"); example b = new example("second","2"); system.out.println("a is " a.tostring()); system.out.println("b is " b.tostring()); } } a、编译错误 b、classcastexception异常 c、输出 a is first b is second d、输出 a is first a.k.a 1 b is second a.k.a 2 e、输出 a is first b is second a.k.a 2
5、尝试编译并运行一下代码时会发生什么 class sub extends base{} class sub2 extends base{} public class cex { public static void main(string[] args) { base b = new base(); sub s= (sub)b; } } a、编译和运行都没有错误 b、编译错误 c、运行异常 d、运行结果错误
6、编译并运行b的main()方法时会发生什么 class a{ int i; a(int i){ this.i = i*2; } } public class b extends a{ public static void main(string[] args) { b b = new b(2); } b(int i){ system.out.println(i); } } a、变量被设置为4 b、变量被设置为2 c、变量被设置为0 d、编译不通过
7、尝试编译并运行以下代码时会发生什么 class base{ private void amethod(int ibase){ system.out.println("base.amethod"); } } class over extends base{ public static void main(string argv[]){ over o=new over(); int ibase=0; o.amethod(ibase); } public void amethod(int iover){ system.out.println("over.amethod"); } } a、编译时错误 b、运行时错误 c、输出base.amethod d、输出over.amethod