1、以下变量s可能是哪种类型?
A.byte
B.long
C.float
D.double
switch (s) {
default:
System.out.println("default");
}
答案:byte
解析:switch(expr1)中,expr1是一个整数表达式,整数表达式可以是int基本类型或Integer包装类型,由于byte,short,char都可以隐含转换为int,所以这些类型以及这些类型的包装类型也是可以的。因此传递给 switch 和case 语句的参数应该是int、short、char、byte,还有enum。
*long,string 都不能作用于swtich。
但在jdk 1.7中switch的参数类型可以是字符串类型,C#也支持字符串类型作为switch参数。
2、以下程序的运行结果是:
public class Test {
public static void main(String[] args) {
try {
args = null;
args[0] = "test";
System.out.println(args[0]);
} catch (Exception e) {
System.out.println("Exception");
} catch (NullPointerException e) {
System.out.println("NullPointerException");
}
}
}
答案:编译错误。
解析:用try{}catch{}捕获多个异常时,是按catch从上往下的顺序进行匹配捕获。一旦匹配上一个catch,之后所有的catch全部跳过,如果有finally则执行finally。这题中抛出的异常实际是NullPointerException,但因为NullPointerException继承于Exception,所以和第一个catch就可以匹配成功,因此第二个catch永远不可能捕获成功,因此编译错误。所以,要对捕获异常时应该将小范围的异常写在前,而大范围在后,Exception要在最后。
*另外,try{}finally{}没有catch也可以。
3、以下程序运行结果是:
A.编译错误,main函数无法返回任何值
B.输出Exception
C.输出Finally
D.输出Exception Finally
public class Test {
public static void main(String[] args) {
try {
return;
} catch (Exception e) {
System.out.println("Exception");
} finally {
System.out.println("Finally");
}
}
}
答案:Finally
解析:未捕获错误,catch不执行。finally优先于return执行。
4、以下程序的运行结果是:
A.编译器错误
B.运行时错误
C.输出”try”
D.输出”catch”
E.输出”finally”
F.输出”try catch”
G.输出”catch try”
H.无输出
public class Test {
static void foo() throws Exception {
throw new Exception();
}
public static void main(String[] args) {
try {
foo();
System.out.println("try");
} catch (Exception e) {
System.out.println("catch");
System.exit(0);
} finally {
System.out.println("finally");
}
}
}
答案:输出catch
解析:try{}中出现异常之后的代码不会被执行,直接跳转执行catch。System.exit(0)语句跳出程序,连finally也不执行。
5、以下程序的运行结果是?
public class Test {
public static void main(String[] args) {
Double d1 = new Double("-5.5");
Double d2 = new Double("-5.5");
System.out.println(d1 == d2);
System.out.println(d1.equals(d2));
}
}
答案:false true
解析:d1、d2是对象,==比较对象地址,equals()方法比较值。
6、以下程序的运行结果是?
public class Test {
public static void main(String[] args) {
int iRand;
iRand = Math.random();
System.out.println(iRand);
}
}
答案:编译错误(类型转换错误)
解析:Math.random()返回[0,1)之间的为随机double值.
7、以下程序的运行结果是?
class Example {
private int i = 1;
public static void main(String[] args) {
Example a = new Example();
a.hallow();
}
abstract void hallow() {
System.out.println("Claines" + i);
}
}
答案:编译错误
解析:abstract抽象方法不能有方法体。
8、以下程序的运行结果是?
A.编译错误:构造函数不能声明为protected
B.运行时错误:构造函数不能声明为protected
C.输出0-10
D.输出0-9
class Example {
public static void main(String[] args) {
Example a = new Example();
}
protected Example() {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
答案:输出0-9
解析:类的内部调用。
9、以下程序的运行结果是?
class Example {
public static void main(String[] args) {
int t = 0;
while (1) {
if (t++ < 10) {
break;
}
}
}
}
答案:编译错误
解析:while()中的参数Type mismatch: cannot convert from int to boolean
但,C++中可以编译成功,非0值为true,while(1)为无限循环。
10、以下程序的运行结果是?
A.NullPointerException
B.NumberFormatException
C.输出null
class Example {
public static void main(String args[]) {
Integer i = null;
method(i);
}
static void method(int k) {
System.out.println(k);
}
}
答案:NullPointerException
解析:Integer是基础类型int的包装类型,所有数值可以自动对应转换。以上代码中i为其他任意数值时均可正常运行,但i对象为null时会出现空指针异常(并不对应int中的0)。
*若将method()方法参数类型改为Integer,运行结果是 输出null。
11、以下程序的运行结果是?
A.编译错误
B.编译通过,但运行时抛出类转换错误
C.成功运行
class OuterOne {
class InnerOne extends OuterOne {
}
static void thisMethod() {
Object o = (Object) new OuterOne();
OuterOne foo = (OuterOne) o;
}
public static void main(String args[]) {
thisMethod();
}
}
答案:成功运行
解析:装箱和拆箱。
12、以下程序的运行结果是?
A.Example
B.args
C.main
D.ArrayIndexOutOfBoundsExcetion
class Test {
public static void main(String args[]) {
System.out.println(args[2]);
}
}
答案:ArrayIndexOutOfBoundsExcetion
解析:main函数的参数args是java程序以命令行形式运行时的输入参数(以空格隔开),命令形如“java Test arg1 arg2 arg3”。该题中没有参数值,args数组长度为0。
13、以下程序的运行结果是?
A.StackOverflowError
B.IllegalStateException
C.ExcetionInInitializerError
D.ArrayIndexOutOfBoundsExcetion
public class Test {
static int a[];
static {
a[0] = 2;
}
public static void main(String args[]) {
//more code here
}
}
答案:ExcetionInInitializerError
解析:Exception in thread “main” java.lang.ExceptionInInitializerError.Caused by: java.lang.NullPointerException at Test.(Test.java:4)