《java面试题精选2》由会员分享,可在线阅读,更多相关《java面试题精选2(10页珍藏版)》请在金锄头文库上搜索。
1、95.有 2 个类 Cat 及 WhiteCat,代码如下: public class Cat protected static String color = “random”; public Cat() public void showCatColor() System.out.println(“Cat:” + color); public static void showColor() System.out.println(“Cat:” + color); public class WhiteCat extends Cat protected static String color = “
2、white”; public WhiteCat() super(); public void showCatColor() System.out.println(“WhiteCat:” + color); public static void showColor() System.out.println(“WhiteCat:” + color); 请分析下面各段程序的运行结果 AWhiteCat whiteCat = new WhiteCat(); Cat cat = whiteCat; cat.showColor(); cat.showCatColor(); BCat cat = new C
3、at(); WhiteCat whiteCat = (WhiteCat) cat; cat.showColor(); cat.showCatColor(); CCat cat = new WhiteCat(); WhiteCat whiteCat = (WhiteCat) cat; cat.showColor(); cat.showCatColor(); 解答:A 段执行的结果是: Cat:random WhiteCat:white B 段执行的结果是: 会抛出 java.lang.ClassCastException 异常 C 段执行的结果是: Cat:random WhiteCat:whi
4、te 96、说说下面语句是否有错误,或可能出现的缺陷,并指出错误,或缺陷在哪里? public class MyFile implements Runnable public void run() while (true) try FileReader fr=new FileReader(new File(“a.txt”) String line=fr.readLine(); System.out.println(line); catch(IOException err) Sleep(1000); 解答: 1.fr.readLine()没有这个方法 2.Sleep(1000)需要用 Threa
5、d.sleep(1000); 97、判断下列语句是否正确,如果有错误,请指出错误所在? List a = new ArrayList(); a.add(5); 解答:错误,默认封装 int 类型。 98、判断下列语句是否正确,如果有错误,请指出错误所在? void foo(final int arg) if (arg.length 1) arg0 = 5; 解答:正确 99、判断下列语句是否正确,如果有错误,请指出错误所在? interface A int add(final A a); class B implements A long add(final A a) return this.
6、hashCode() + a.hashCode(); 解答:返回值不是 long 类型 100、指出下面程序的运行结果: class A static System.out.print(“a”); public A () System.out.print(“x”); class B extends A static System.out.print(“b”); public B () System.out.print(“y”); public class Test public static void main(String args) A ab = new B (); ab = new B
7、(); 解答:abxyxy 101、下列代码的输出结果是什么? public class MyFor public static void main (String argv) int i; int j; outer:for(i=1;i3;i+) inner:for(j=1;j3;j+) if (j=2) continue outer; System.out .println(“Value for i=”+i+” Value for j=” +j); 解答:Value for i=1 Value for j=1 Value for i=2 Value for j=1 102、查看下面的代码,写
8、出可以使程序正常执行的修改方法 1.public class MyClass 2.static String s1; 3. String s2; 4. public static void main(String args) 5. String s3; 6. System.out.println(“s1 =” + s1); 7. System.out.println(“s2 =” + s2); 8. System.out.println(“s3 =” + s3); 9. 10. 解答:删除第 8 行或者将第 6 行改为 String s3 = “”; 103、为了显示 myStr = 23 这
9、样的结果,写出在控制台输入的命令 public class MyClass public static void main(String args) String s1 = args0; String s2 = args1; String myStr = args2; System.out.printin(“myStr =” + s2 + myStr); 解答:java MyClass 1 2 3 4 104、写出下面代码的执行结果 public class MyClass static void aMethod(StringBuffer sf1, StringBuffer sf2) sf1.
10、append(sf2); sf2 = sf1; public static void main(String args) StringBuffer sf1 = new StringBuffer(“A”); StringBuffer sf2 = new StringBuffer(“B”); aMethod(sf1,sf2); System.out .println(sf1+ “:”+sf2); 解答:AB:B 105、第 3 行中生成的 object 在第几行执行后成为 garbage collection 的对象? 1.public class MyClass 2. public String
11、Buffer aMethod() 3. StringBuffer sf = new StringBuffer(“Hello”); 4. StringBuffer sf_arr = new StringBuffer1; 5. sf_arr0 = sf; 6. sf = null; 7. sf_arr0 = null; 106、写出执行下面的代码后的结果 public class MyClass public static void main(String args) java.util.Vector v1 = new java.util.Vector(); v1.addElement(“Hell
12、o”); v1.addElement(new Float(3.14f); v1.addElement(10); System.out.println(v1.elementAt(0) + “:” + v1.elementAt(1) + “:”+ v1.elementAt(2); 解答:Hello : 3.14 : 10 107、写出执行下面代码后的正确结果 interface MyDB public void getConnection(); class MyDBDriver implements MyDB public void getConnection() System.out.print
13、ln(“getConnection()”); public class MyClass public static void aMethod(MyDB db) db.getConnection(); public static void main(String args) MyDBDriver db_driver = new MyDBDriver(); aMethod(db_driver); 解答:getConnection() 108、下列程序运行的结果是 class A class Dog private String name; private int age; private int
14、step; Dog(String s, int a) name = s; age = a; step = 0; public void run(Dog fast) fast.step+; public static void main(String args) A a = new A(); Dog d = a.new Dog(“Tom”, 3); d.step = 25; d.run(d); System.out.println(d.step); 解答:26 109、请看下列程序,运行结果是 class Super int i=10; Super() print(); i=20; void print() System.out.print(i); public class Sub extends Super int j=30; Sub() print(); j=40; void print() System.out.print(j); public static void main(String args) System.out.print(new Sub().j); 解答:03040 110、getSomething ()执行时发生 IllegalArgumentException 会出现什么样的结果? void makeConnection(