题目和答案来自于阿里云大学 - 知乎专栏java
如今有以下一段代码
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 failapp
B. Compilation will succeed and the program will print“0”this
C. Compilation will succeed and the program will print“1”阿里云
D. Compilation will succeed and the program will print“2”code
如要在字符串s(内容为“welcome to mldn !! ”),中,发现字符't'的位置,应该使用下面哪一种方法?对象
A.mid(2,s);
字符串
B. charAt(2);
get
C. s.indexOf('t');
it
D. indexOf(s,'v');
编译和运行下面代码可能会发生什么?
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
如今有以下一段程序
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. 运行时出错
如今有以下一段程序
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
在一个类文件中,导入包、类和打包是怎样的排列顺序?
A. package、import、class;
B. class、import、package
C. import、package、class
D. package、class、import
若是你试图编译并运行下列代码时可能会打印输出什么?
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
当你编译下列代码可能会输出什么?
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
下面代码会存在什么问题?
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类型描述
为Demo类的一个无形式参数无返回值的方法method书写方法头,使得使用类名Demo做为前缀就能够调用它,该方法头的形式为?
A. static void method( )
B. public void method( )
C. final void method( )
D. abstract void method( )
ACDCC ABDAA
在方法体内声明的变量是“局部变量”,而局部变量是不能用static修饰的,private、protected、public也是不能用的。
indexOf是String类的一个方法,做用是查找第一次出现参数的位置,没有则返回-1。
不管amethod方法是否是private,结果都是执行子类的amethod方法。区别是,若是不是private,子类的amethod方法是重写了父类的方法;若是是private,子类的amethod方法并无重写父类的方法。
Java中,若是类里没有写构造方法,那么会默认有一个无参的构造方法。可是一旦手动写了构造方法,那么默认的无参构造方法就没有了。这道题是由于父类只有一个有参的构造方法,可是子类却没有,因此编译出错。
Java中的数组是对象,因此第四行没有问题。而f1[1]须要的是一个数组而且是一维数组,因此第五行编译出错。
无
在default中进入,在case 0中由于break跳出。
基本数据类型都有相应的默认值,其中int是0,char为‘\u0000’,boolean为false。
静态方法没法调用非静态方法。
静态方法能够用类名.方法名直接调用。