【转载】Java 经典笔试题

文章转载自:http://blog.csdn.net/hdwt/article/details/1294558

这些题目对我的笔试帮助很大,有需要的朋友都可以来看看,在笔试中能遇到的题目基本上下面都会出现,虽然形式不同,当考察的基本的知识点还是相同的。

Simulated Test of SCJP for JAVA2 PlatFORM (only for training))

网上可以找到很多,因为我是转载ICXO网站的,但是上面的有很多可能有由于页面原因,每个题目我都做了测试,出现错误的我就稍微做了下修正,希望和大家一起研究和探讨,在分析中肯定有不足和谬误的地方还请大虾们能够给予及时的纠正,特此感谢。

1.

public class ReturnIt{
      returnType methodA(byte x, double y){   //line 2
            return (short)x/y*2;
      }
}

what is valid returnType for methodA in line 2?

答案:返回double类型,因为(short)x将byte类型强制转换为short类型,与double类型运算,将会提升为double类型.

2.
1)  class Super{
2)          public float getNum(){return 3.0f;}
3)  }
4)
5)  public class Sub extends Super{
6)
7)  }
which method, placed at line 6, will cause a compiler error?
A. public float getNum(){return 4.0f;}
B. public void getNum(){}
C. public void getNum(double d){}
D. public double getNum(float d){return 4.0d;}
Answer:B

A属于方法的重写(重写只存在于继承关系中),因为修饰符和参数列表都一样.B出现编译错误,如下:

Sub.java:6: Sub 中的 getNum() 无法覆盖 Super 中的 getNum();正在尝试使用不
兼容的返回类型
找到: void
需要: float
   public void getNum(){}
               ^
1 错误

B既不是重写也不是重载,重写需要一样的返回值类型和参数列表,访问修饰符的限制一定要大于被重写方法的访问修饰符(public>protected>default>private);

重载:必须具有不同的参数列表;
  可以有不同的返回类型,只要参数列表不同就可以了;
  可以有不同的访问修饰符;

把其看做是重载,那么在java中是不能以返回值来区分重载方法的,所以b不对.

3.

public class IfTest{
    public static void main(String args[]){
        int x=3;
        int y=1;
        if(x=y)
            System.out.println("Not equal");
        else
            System.out.println("Equal");
    }
}
what is the result?
Answer:compile error 错误在与if(x=y) 中,应该是x==y;  =是赋值符号,==是比较操作符

4. public class Foo{
  public static void main(String args[]){
  try{return;}
   finally{ System.out.println("Finally");}
   }
    }
what is the result?
A. print out nothing
B. print out "Finally"
C. compile error
Answer:B   java的finally块会在return之前执行,无论是否抛出异常且一定执行.

5.public class Test{
   public static String output="";
   public static void foo(int i){
     try {
       if(i==1){
         throw new Exception();
       }
       output +="1";
     }
     catch(Exception e){
       output+="2";
       return;
     }
     finally{
       output+="3";
     }
     output+="4";
   }
   public static void main(String args[]){
     foo(0);
     foo(1);
     24)  
   }
}

what is the value of output at line 24? Answer:13423 如果你想出的答案是134234,那么说明对return的理解有了混淆,return是强制函数返回,本题就是针对foo(),那么当执行到return的话,output+="4"; 就不再执行拉,这个函数就算结束拉.

6. public class IfElse{
        public static void main(String args[]){
            if(odd(5))
                System.out.println("odd");
            else
                System.out.println("even");
         }
         public static int odd(int x){return x%2;}  
     }
     what is output?
     Answer:Compile Error

7. class ExceptionTest{
           public static void main(String args[]){
                 try{
                       methodA();
                 }
                 catch(IOException e){
                       System.out.println("caught IOException");
                 }
                 catch(Exception e){
                        System.out.println("caught Exception");
                 }
            }
      }
If methodA() throws a IOException, what is the result? (其实还应该加上:import java.io.*;)
Answer:caught IOException 异常的匹配问题,如果2个catch语句换个位置,那就会报错,catch只能是越来越大,意思就是说:catch的从上到下的顺序应该是:孙子异常->孩子异常->父亲异常->老祖先异常.这么个顺序.

8. int i=1,j=10;
    do{
           if(i++>--j) continue;
     }while(i可以得出答案,不过还有几个类似的题目,用该文章解释不通,因为本人对java传递参数也一直没有弄明白,所以,还请大虾多多指教.

21. public class ConstOver{
     public ConstOver(int x, int y, int z){}
   }
  which two overload the ConstOver constructor?
  A.ConstOver(){}
  B.protected int ConstOver(){}  //not overload ,but no a error
  C.private ConstOver(int z, int y, byte x){}
  D.public void ConstOver(byte x, byte y, byte z){}
  E.public Object ConstOver(int x, int y, int z){}
Answer:A,C

分析:测试不通过的首先是B,E,因为要求有返回值,这2个选项没有,要想通过编译那么需要加上返回值,请注意如果加上返回值,单纯看选项是没有问题拉,可以针对题目来说,那就是错之又错拉,对于构造器来说是一种特殊类型的方法,因为它没有返回值.对于D选项在91页有详细的介绍,空返回值,经管方法本身不会自动返回什么,但可以选择返回别的东西的,而构造器是不会返回任何东西的,否则就不叫构造器拉.

22. public class MethodOver{
           public void setVar(int a, int b, float c){}
   }
  which overload the setVar?
  A.private void setVar(int a, float c, int b){}
  B.protected void setVar(int a, int b, float c){}
  C.public int setVar(int a, float c, int b){return a;}
  D.public int setVar(int a, float c){return a;}
Answer:A,C,D 分析:方法的重载,根据概念选择,B是错误的,因为他们有相同的参数列表,所以不属于重载范围.

23. class EnclosingOne{
             public class InsideOne{}
    }
       public class InnerTest{
          public static void main(String args[]){
          EnclosingOne eo=new EnclosingOne();
          //insert code here
          }
        }
        A.InsideOne ei=eo.new InsideOne();
        B.eo.InsideOne ei=eo.new InsideOne();
        C.InsideOne ei=EnclosingOne.new InsideOne();
        D.InsideOne ei=eo.new InsideOne();
        E.EnclosingOne.InsideOne ei=eo.new InsideOne();
Answer:E

24. What is "is a" relation?
  A.public interface Color{}
   public class Shape{private Color color;}
  B.interface Component{}
   class Container implements Component{ private Component[] children; }
  C.public class Species{}
  public class Animal{private Species species;} 
  D.interface A{}
      interface B{}
      interface C implements A,B{}  //syntex error
Answer:B 我没有明白这个题目的意思,有人能告诉我嘛?

25. 1)package foo;
      2)
      3)public class Outer{
      4)    public static class Inner{
      5)    }
      6)}
  which is true to instantiated Inner class inside Outer?
  A. new Outer.Inner()
  B. new Inner()
Answer:B
if out of outerclass A is correct  分析:在Outer内部,B方式实例化内部类的方法是正确的,如果在Outer外部进行inner的实例化,那么A方法是正确的.

26. class BaseClass{
         private float x=1.0f;
         private float getVar(){return x;}
    }
      class SubClass extends BaseClass{
         private float x=2.0f;
         //insert code
  }
  what are true to override getVar()?
  A.float getVar(){
  B.public float getVar(){
  C.public double getVar(){
  D.protected float getVar(){
  E.public float getVar(float f){
Answer:A,B,D 分析:返回类型和参数列表必须完全一致,且访问修饰符必须大于被重写方法的访问修饰符.

27. public class SychTest{
          private int x;
          private int y;
          public void setX(int i){ x=i;}
          public void setY(int i){y=i;}
          public Synchronized void setXY(int i){
               setX(i);
               setY(i);
          }
          public Synchronized boolean check(){
               return x!=y;  
          }
      }
Under which conditions will  check() return true when called from a different class?
A.check() can never return true.
B.check() can return true when setXY is callled by multiple threads.
C.check() can return true when multiple threads call setX and setY separately.
D.check() can only return true if SychTest is changed allow x and y to be set separately.
Answer:C

分析:答案是C,但是我想不出来一个测试程序来验证C答案.希望高手们给我一个测试的例子吧,万分感谢..........

28. 1) public class X implements Runnable{
       2)              private int x;
       3)              private int y;
       4)              public static void main(String[] args){
       5)                               X that =new X();
       6)                              (new Thread(that)).start();
       7)                              (new Thread(that)).start();
                         }
       9)              public synchronized void run(){
     10)                              for(;;){
     11)                                          x++;
     12)                                          y++;
     13)                                          System.out.println("x="+x+",y="+y);
     14)                               }
     15)                 }
     16) }  
      what is the result?
      A.compile error at line 6
      B.the program prints pairs of values for x and y that are always the same on the same time
      Answer:B 分析:我感觉会出现不相等的情况,但是我说不出为什么会相等。线程方面,还有好多路要走啊,咳

29. class A implements Runnable{
              int i;
              public void run(){
                  try{
                        Thread.sleep(5000);
                         i=10;
                  }catch(InterruptedException e){}
              }
              public static void main(String[] args){
                  try{
                       A a=new A();
                      Thread t=new Thread(a);
                       t.start();
                       17)
                        int j=a.i;
                        19)
              }catch(Exception e){}
        }
    }
what be added at line line 17, ensure j=10 at line 19?
A. a.wait();   B.  t.wait();   C. t.join();   D.t.yield();    E.t.notify();    F. a.notify();     G.t.interrupt();
Answer:C

30. Given an ActionEvent, how to indentify the affected component?
    A.getTarget();
    B.getClass();
    C.getSource();   //public object
    D.getActionCommand();
Answer:C

31. import java.awt.*;
public class X extends Frame{
    public static void main(String[] args){
      X x=new X();
      x.pack();
      x.setVisible(true);
    }
    public X(){
      setLayout(new GridLayout(2,2));
     
      Panel p1=new Panel();
      add(p1);
      Button b1=new Button("One");
      p1.add(b1);
     
      Panel p2=new Panel();
      add(p2);
      Button b2=new Button("Two");
      p2.add(b2);
      
      Button b3=new Button("Three");
      p2.add(b3);
     
      Button b4=new Button("Four");
      add(b4);
   }
}
when the frame is resized,
A.all change height    B.all change width   C.Button "One" change height
D.Button "Two" change height  E.Button "Three" change width
F.Button "Four" change height and width
Answer:F

32. 1)public class X{
  2)    public static void main(String[] args){
  3)     String foo="ABCDE";
  4)     foo.substring(3);
  5)     foo.concat("XYZ");
  6)    }
  7)   }
  what is the value of foo at line 6?
Answer:ABCDE

33. How to calculate cosine 42 degree?
  A.double d=Math.cos(42);
  B.double d=Math.cosine(42);
  C.double d=Math.cos(Math.toRadians(42));
  D.double d=Math.cos(Math.toDegrees(42));
  E.double d=Math.toRadious(42);
Answer:C

34. public class Test{
   public static void main(String[] args){
   StringBuffer a=new StringBuffer("A");
   StringBuffer b=new StringBuffer("B");
   operate(a,b);
   System.out.pintln(a+","+b);
    }
   public static void operate(StringBuffer x, StringBuffer y){
    x.append(y);
    y=x;
   }
   }
   what is the output?
Answer:AB,B 分析:这道题的答案是AB,B,网上有很多答案给错啦,大家注意啊。

35.  1) public class Test{
       2)     public static void main(String[] args){
       3)       class Foo{
       4)            public int i=3;
       5)        }
       6)        Object o=(Object)new Foo();
       7)        Foo foo=(Foo)o;
                   System.out.println(foo.i);
       9)     }
     10) }
  what is result?
  A.compile error at line 6
  B.compile error at line 7
  C.print out 3
Answer:C



36. public class FooBar{
   public static void main(String[] args){
    int i=0,j=5;
  4) tp:  for(;;i++){
         for(;;--j)
        if(i>j)break tp;
       }
   System.out.println("i="+i+",j="+j);
   }
   }
  what is the result?
  A.i=1,j=-1    B. i=0,j=-1  C.i=1,j=4    D.i=0,j=4  
  E.compile error at line 4
Answer:B

37. public class Foo{
             public static void main(String[] args){
                         try{System.exit(0);}
                         finally{System.out.println("Finally");}
             }
       }
   what is the result?
   A.print out nothing
   B.print out "Finally"
Answer:A
system.exit(0) has exit

38. which four types of objects can be thrown use "throws"?
  A.Error
  B.Event
  C.Object
  D.Excption
  E.Throwable
  F.RuntimeException
Answer:A,D,E,F

分析:throw,例如:throw new IllegalAccessException("demo");是一个动作。
而throws则是异常块儿的声明。所以感觉题目应该是“throw”

39. 1)public class Test{
       2)     public static void main(String[] args){
       3)         unsigned byte b=0;
       4)         b--;
       5)
       6)     }
       7) }
what is the value of b at line 5?
A.-1   B.255  C.127  D.compile fail  E.compile succeeded but run error
Answer:D

40. public class ExceptionTest{
            class TestException extends Exception{}
            public void runTest() throws TestException{}
            public void test() /* point x */ {
                    runTest();
            }
       }
At point x, which code can be add on to make the code compile?
A.throws Exception   B.catch (Exception e)
Answer:A

41. String foo="blue";
       boolean[] bar=new boolean[1];
       if(bar[0]){
          foo="green";
       }
what is the value of foo?
A.""  B.null  C.blue   D.green
Answer:C

42. public class X{
           public static void main(String args[]){
                 Object o1=new Object();
                 Object o2=o1;
                 if(o1.equals(o2)){
                      System.out.prinln("Equal");
                  }
            }
       }
what is result?
Answer:Equal

 

猜你喜欢

转载自luowei925.iteye.com/blog/2268728