1、Thread.sleep()是否会抛出checked exception?
答:会。checked exception是必检异常,经过编译器验证。对于声明抛出异常的任何方法,编译器将强制处理或声明规则。unchecked exception是免检异常,如RuntimeException。它是运行时异常。因为此类异常通常由特定操作引发,这些操作在内存中经常出现。Thread.sleep()抛出InterruptedException异常,属于checked exception。
2、Math.floor(-8.5)=?
答:(double)-9.0。Math.floor():不大于该数的最大整数,返回double类型。Math.round():返回int,四舍五入,算法为Math.floor(x+0.5),表示原来的数加上0.5向下取整。Math.round(-11.5)的结果为:-11
3、下面代码输出结果是?
public class TestDemo { public static String output =""; public static void foo(int i){ try{ if(i == 1){ throw new Exception(); } }catch(Exception e){ output += "2"; return ; }finally{ output += "3"; } output += "4"; } public static void main(String[] args) { foo(0); foo(1); System.out.println(output); } }
答:3423。try-catch执行结束后,程序继续向下执行。throw new Exception(),发生了异常,程序执行完finally块后,就结束,因为前面有return。
4、程序输出的结果:
public class HelloB extends HelloA { public HelloB() { } { System.out.println("I’m B class"); } static { System.out.println("static B"); } public static void main(String[] args) { new HelloB(); } } class HelloA { public HelloA() { } { System.out.println("I’m A class"); } static { System.out.println("static A"); } }
答:
static A static B I’m A class I’m B class
该题主要考察了java的执行顺序。父类静态块--子类静态块--父类块--父类构造器--子类块--子类构造器
参考http://java-mzd.iteye.com/blog/838683
5、AWT 是基于本地方法的C/C++程序,其运行速度比较快;Swing是基于AWT 的Java程序,其运行速度比较慢。
6、
类方法不可以用this来调用;在类方法中调用本类的类方法可以直接调用;在类方法中可以调用其他类的类方法;在类方法中可以调用实例方法
类方法是静态方法,this是调用当前对象的属性和方法。所以不能用this调用类方法。
7、如下代码的输出是
package Test; public class Test { private static void test(int[]arr) { for (int i = 0; i < arr.length; i++) { try { if (arr[i] % 2 == 0) { throw new NullPointerException(); } else { System.out.print(i); } } finally { System.out.print("e"); } } } public static void main(String[]args) { try { test(new int[] {0, 1, 2, 3, 4, 5}); } catch (Exception e) { System.out.print("E"); } } }
答:eE
8、list是一个ArrayList的对象,哪个选项的代码填到//todo delete处,可以在Iterator遍历的过程中正确并安全的删除一个list中保存的对象?
Iterator it = list.iterator(); int index = 0; while (it.hasNext()) { Object obj = it.next(); if (needDelete(obj)) //needDelete返回boolean,决定是否要删除 { //todo delete } index ++; }
答:it.remove() Iterator接口有三个方法,hasNext()如果有元素可以迭代,返回true next()返回迭代的下一个元素 remove()从迭代器指向的Collection中移除元素。迭代器可以安全的删除集合中的元素。不能使用集合本身的方法删除元素,在集合遍历的过程中。可参见http://www.cnblogs.com/nick-huang/p/4635670.html
9、在java7中,下列不能做switch()的参数类型是?
switch(expr1)中,expr1是一个整数表达式,整数表达式可以是int基本类型或Integer包装类型,由于,byte,short,char都可以隐含转换为int,所以,这些类型以及这些类型的包装类型也是可以的。因此传递给 switch 和case 语句的参数应该是 int、 short、 char 或者 byte,还有enum。 long,string 都不能作用于swtich。
在jdk 1.7中switch的参数类型可以是字符串类型。https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
10、1Java类Demo中存在方法func0、func1、func2、func3和func4,请问该方法中,哪些是不合法的定义?
public class Demo{ float func0() { byte i=1; return i; } float func1() { int i=1; return; } float func2() { short i=2; return i; } float func3() { long i=3; return i; } float func4() { double i=4; return i; } }
答:正确定义的有fun0(),fun2(),fun()3。fun1()没有返回值,fun4()double类型转成float要强制转换。
从左到右-隐式转换 byte--short--int--long--float--double,反之,显示转换
public
class
Demo{
float
func0()
{
byte
i=
1
;
return
i;
}
float
func1()
{
int
i=
1
;
return
;
}
float
func2()
{
short
i=
2
;
return
i;
}
float
func3()
{
long
i=
3
;
return
i;
}
float
func4()
{
double
i=
4
;
return
i;
}
}