【17-06-19】Java进阶自测:面向对象基础知识掌握了吗?(附答案及个人解析)...

本文集解答了多个Java编程选择题,涉及编译错误、运行结果预测、方法调用及字符串操作等内容,帮助读者理解Java语言特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述

题目和答案来自于阿里云大学 - 知乎专栏

题目

  1. 现在有如下一段代码
    java public class Test { public int aMethod() { static int i=0; i++; return i; } public static void main(String args[]) { Test test = new Test(); test.aMethod(); int j = test.aMethod(); System.out.println(j); } }
    将产生哪种结果:

    A. Compilation will fail

    B. Compilation will succeed and the program will print“0”

    C. Compilation will succeed and the program will print“1”

    D. Compilation will succeed and the program will print“2”

  2. 如要在字符串s(内容为“welcome to mldn !! ”),中,发现字符't'的位置,应该使用下面哪种方法?

    A.mid(2,s);

    B. charAt(2);

    C. s.indexOf('t');

    D. indexOf(s,'v');

  3. 编译和运行下面代码可能会发生什么?
    java class Base { private void amethod(int iBase) { System.out.println("Base.amethod"); } } class Over extends Base { public static void main(String args[]) { Over o = new Over(); int iBase = 0 ; o.amethod(iBase) ; } public void amethod(int iOver) { System.out.println("Over.amethod"); } }
    A. Compile time error complaining that Base.amethod is private

    B. Runntime error complaining that Base.amethod is private

    C. Output of Base amethod

    D. Output of Over.amethod

  4. 现在有如下一段程序
    java class super { String name ; public super(String name) { this.name = name ; } public void fun1() { System.out.println("this is class super !"+name); } } class sub extends super { public void fun1() { System.out.println("this is class sub !"+name); } } class Test { public static void main(String args[]) { super s = new sub(); } }
    运行上面的程序可能会出现的结果?

    A. this is class super !

    B. this is class sub !

    C. 编译时出错

    D. 运行时出错

  5. 现在有如下一段程序
    java class Happy { public static void main(String args[]) { float [][] f1 = {{1.2f,2.3f},{4.5f,5.6f}} ; Object oo = f1 ; f1[1] = oo ; System.out.println("Best Wishes "+f1[1]); } }
    该程序会出现何种效果?

    A. {4.5,5.6}

    B. 4.5

    C. compilation error in line NO.5

    D. exception

  6. 在一个类文件中,导入包、类和打包是怎样的排列顺序?

    A. package、import、class;

    B. class、import、package

    C. import、package、class

    D. package、class、import

  7. 如果你试图编译并运行下列代码时可能会打印输出什么?
    java int i = 9 ; switch(i) { default: System.out.println("default"); case 0 : System.out.println("zero"); break ; case 1 : System.out.println("one"); case 2 : System.out.println("two"); }
    A. default

    B. default , zero

    C. error default clause not defined

    D. no output displayed

  8. 当你编译下列代码可能会输出什么?
    java class Test { static int i ; public static void main(String args[]) { System.out.println(i); } }
    A. Error Variable i may not have been initialized

    B. null

    C. 1

    D. 0

  9. 下面代码会存在什么问题?
    java public class MyClass { public static void main(String arguments[]) { amethod(arguments); } public void amethod(String[] arguments){ System.out.println(arguments); System.out.println(arguments[1]); } }
    A. 错误,void amethod()不是static类型

    B. 错误,main()方法不正确

    C. 错误,数组必须导入参数

    D. 方法amethod()必须用String类型描述

  10. 为Demo类的一个无形式参数无返回值的方法method书写方法头,使得使用类名Demo作为前缀就可以调用它,该方法头的形式为?

    A. static void method( )

    B. public void method( )

    C. final void method( )

    D. abstract void method( )

答案

ACDCC ABDAA

个人解析

  1. 在方法体内声明的变量是“局部变量”,而局部变量是不能用static修饰的,private、protected、public也是不能用的。

  2. indexOf是String类的一个方法,作用是查找第一次出现参数的位置,没有则返回-1。

  3. 无论amethod方法是不是private,结果都是执行子类的amethod方法。区别是,如果不是private,子类的amethod方法是重写了父类的方法;如果是private,子类的amethod方法并没有重写父类的方法。

  4. Java中,如果类里没有写构造方法,那么会默认有一个无参的构造方法。但是一旦手动写了构造方法,那么默认的无参构造方法就没有了。这道题是因为父类只有一个有参的构造方法,但是子类却没有,所以编译出错。

  5. Java中的数组是对象,所以第四行没有问题。而f1[1]需要的是一个数组并且是一维数组,所以第五行编译出错。

  6. 在default中进入,在case 0中因为break跳出。

  7. 基本数据类型都有相应的默认值,其中int是0,char为‘\u0000’,boolean为false。

  8. 静态方法无法调用非静态方法。

  9. 静态方法可以用类名.方法名直接调用。

转载于:https://www.cnblogs.com/baka-sky/p/7061133.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值