日期:2014-05-20  浏览次数:20735 次

一些Java面试题,求答案
试题1
下面各选项可以在A的子类中使用的是()
class A {
protected int method (int a, int b) {
return 0;
}
}
  A.  
  public int method (int a, int b) { return 0; }  
  B.  
  private int method(int a, int b) { return 0; }  
  C.  
  private int method(int a, long b) { return 0; }  
  D.  
  public short method(int a, int b) { return 0; }  


试题2
下列代码执行的结果是:
class Base
{
void test() { 
System.out.println("Base.test()");
}
}
public class Child extends Base {
void test() {
System.out.println("Child.test()");
}
public static void main(String[] a) {
Child anObj = new Child();
Base baseObj = anObj;
baseObj.test();

}
  A.  
  Child.test()
  Base.test()  
  B.  
  Base.test() 
  Child.test()  
  C.  
  Base.test()  
  D.  
  Child.test()  


试题3
关于以下程序代码的说明正确的是( ): 
1.class HasStatic{ 
2. private static int x=100; 
3. public static void main(String args[ ]){ 
4. HasStatic hs1=new HasStatic( ); 
5. hs1.x++; 
6. HasStatic hs2=new HasStatic( ); 
7. hs2.x++; 
8. hs1=new HasStatic( ); 
9. hs1.x++; 
10. HasStatic.x--; 
11. System.out.println(“x=”+x); 
12. } 
13.} 
  A.  
  5行不能通过编译,因为引用了私有静态变量  
  B.  
  10行不能通过编译,因为x是私有静态变量  
  C.  
  程序通过编译,输出结果为:x=103  
  D.  
  程序通过编译,输出结果为:x=102  


试题4
关于java.lang.String类,以下描述正确的一项是( )
  A.  
  String类是final类故不可以继承;  
  B.  
  String类是final类故可以继承;  
  C.  
  String类不是final类故不可以继承;  
  D.  
  String类不是final类故可以继承;  


试题5
下列关于interface的说法正确的是:( ) 
  A.  
  interface中可以有private方法  
  B.  
  interface中可以有final方法  
  C.  
  interface中可以有方法实现  
  D.  
  interface可以继承其他interface  


试题6
以下程序正确的输出是( )
public class FatherClass {
public FatherClass() {
System.out.println("FatherClass Create");
}
}
public class ChildClass extends FatherClass {
public ChildClass() {
System.out.println("ChildClass Create");
}
public static void main(String[] args) {
FatherClass fc = new FatherClass();
ChildClass cc = new ChildClass();
}
}
  A.  
  FatherClass Create
  FatherClass Create
  ChildClass Create  
  B.  
  FatherClass Create
  ChildClass Create
  FatherClass Create  
  C.  
  ChildClass Create
  ChildClass Create
  FatherClass Create  
  D.  
  ChildClass Create
  FatherClass Create
  FatherClass Create  


试题7
请看如下代码 
  class Person { 
  private int a; 
  public int change(int m){return m;} 
  } 
  public class Teacher extends Person{ 
  public int b; 
  public static void main(String arg[]){