|
总结的不错,新手老鸟都能获益。
http://blog.csdn.net/psyl/archive/2010/01/24/5250562.aspx
1.下面代码运行结果是?
view plaincopy to clipboardprint?public class Proba { [/li][li] public static void main(String[] args) { [/li] final int num1 = 0; [/li][li] final int num2 = Integer.parseInt(args[0]); [/li] assert num1 > num2 : \"The value must be negative\"; [/li][li] System.out.println(\"You specified \" + num2); [/li] } [/li][li]} [/li] public class Proba { public static void main(String[] args) { final int num1 = 0; final int num2 = Integer.parseInt(args[0]); assert num1 > num2 : \"The value must be negative\"; System.out.println(\"You specified \" + num2); } }
输入结果为 You specified 4.
理由: assertions 默认状态是关闭.
2. 下面代码会不会运行?
int [] array = new int [Integer.MAX_VALUE + 1];
答案: 会,但会出现 NegativeArraySizeException, 因为JAVA数组索引数值范围是 【0,2,147,483,647】,而 Integer.MAX_VALUE + 1,是负数。
3. 一个已经启动的线程能够被再次start 吗?
答案:不能,start() 只能被一次使用,否则会抛出 IllegalThreadStartException 异常。
4. 考一个for的执行机制,下面代码输入结果是?
view plaincopy to clipboardprint?public class Test{ [/li][li] static boolean foo(char c) { [/li] System.out.print(c); [/li][li] return true; [/li] } [/li][li] public static void main( String[] argv ) { [/li] int i =0; [/li][li] for (foo('A'); foo('B')&&(i<2); foo('C')){ [/li] i++ ; [/li][li] foo('D'); [/li] } [/li][li] } [/li] } [/li] public class Test{ static boolean foo(char c) { System.out.print(c); return true; } public static void main( String[] argv ) { int i =0; for (foo('A'); foo('B')&&(i<2); foo('C')){ i++ ; foo('D'); } } }
答案:
ABDCBDCB ,答错的多半是不仔细思考的缘故。
5. 下面代码哪有问题?
view plaincopy to clipboardprint?class Test implements Runnable{ [/li][li] int i=0; [/li] public int run(){ [/li][li] while(true){ [/li] i++; [/li][li] System.out.println(\"i=\"+i); [/li] } [/li][li] return 1; [/li] } [/li][li] } [/li] class Test implements Runnable{ int i=0; public int run(){ while(true){ i++; System.out.println(\"i=\"+i); } return 1; } }
答:run返回类型应该是void..
5. 值比较 ,下面代码输出结果是?
view plaincopy to clipboardprint? public class G6 { [/li][li] public static void main(String[] args) { [/li] double d = 0.0, d1 = -0.0; [/li][li] [/li] if (d == d1) { [/li][li] System.out.println(true); [/li] } else { [/li][li] System.out.println(false); [/li] } [/li][li] [/li] if (0.0 == -0.0) { [/li][li] System.out.println(true); [/li] } else { [/li][li] System.out.println(false); [/li] } [/li][li] [/li]17: } [/li][li]18: } [/li] public class G6 { public static void main(String[] args) { double d = 0.0, d1 = -0.0; if (d == d1) { System.out.println(true); } else { System.out.println(false); } if (0.0 == -0.0) { System.out.println(true); } else { System.out.println(false); } 17: }18: }
答: True, True
6. & 位操作
int a = 128;
int b = 255;
int c;
c = a & b
c是多少?
答: 128.
00000000 00000000 00000000 10000000
00000000 00000000 00000000 11111111
= 00000000 00000000 00000000 10000000
7. 就近匹配
下面代码输出结果是?
view plaincopy to clipboardprint?public class Test { [/li][li] public static void method(Object o) { [/li] System.out.println(\"Object:\" + o); [/li][li] } [/li] [/li][li] public static void method(String s) { [/li] System.out.println(\"String: \" + s); [/li][li] } [/li] [/li][li] public static void main(String[] args) { [/li] method(null); [/li][li] } [/li] } [/li] public class Test { public static void method(Object o) { System.out.println(\"Object:\" + o); } public static void method(String s) { System.out.println(\"String: \" + s); } public static void main(String[] args) { method(null); } }
答:结果是 String:null
String是object子类它比object更优先匹配null, 如果这里的method(null) 改成method(123) ,结果就应该是object:123.
8. null
下面判断是否正确
class A
boolean b = null instanceof A
答: 错, null本身没有类
9 下面代码能正常运行?
view plaincopy to clipboardprint?public class Test { [/li][li] public static void main(String[] args) { [/li] char b = 127; [/li][li] [/li] while (false) { [/li][li] System.out.println(b); [/li] } [/li][li] } [/li] } [/li] public class Test { public static void main(String[] args) { char b = 127; while (false) { System.out.println(b); } } }
答: 不能,编译错误, System.out.println(b); 不能被执行 |
|