java实习生面试题!求助详细解释!

Please write down the output of the following java code: Question 1 class A { public void method(){ System.out.println("A"); } } class B extends A{ public void method(){ System.out.println("B"); } } public class Test { public static void main(String[] args) { A a = new A(); B b = new B(); ((A)b).method(); } } Question 2 public class Test2 { static String str1 = "Hello"; static String str2 = new String("Hello"); static String str3 = "Hello"; static String str4 = new String(str3); public static void main(String[] args) { System.out.println(str1==str2); System.out.println(str1==str3); System.out.println(str2=="Hello"); System.out.println(str3==str4); } } Question 3 public class Test3 { public static void exchange(int a, int b){ int temp = a; a = b; b = temp; } public static void main(String[] args) { int a = 1; int b = 2; exchange(a,b); System.out.println(a); } } Question 4 public class Test4 { public static void main(String[] args) { String hello = "Hello World"; hello.replaceAll("World", "China"); System.out.println(hello); } } Question 5 public class Test5 { public static void main(String[] args) { int a = 1, b=2; if(a > 1 & b-- ==1){ a++; } System.out.println("a = "+a); System.out.println("b = "+b); } }

工作一年了,这个题还真有点搞不定! 第一个是继承问题,B实例化的是自己,即使前面加了个对象,他输出的依然是B;
第二个,首先1是false,2是false,3是true,4是false。 记着实例化后对象就会变成一个新值,即使你看着相同,在计算机哪里也是不一样的,除非用.equals这个方法。具体的你可以去网络查下,很多解释。
第三个,作用域不同,输出的依然是1.
第四个,就算你再怎么变,你没赋值!hello这个值都不会变的!
第五个,&也是且的含义,a不变,依然是1,b变化,因为它减减了“--”,所以它是1。
大多数网上都有,但是好久不碰了,有的还是请教别人才清楚的了,上班上的越来越回旋!
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-08-28
你这娃儿 要当实习生这个题都搞不定 你还是再回学校学习学习吧!
相似回答