程序类笔试题目

文学乐 人气:1.45W

一:  写入如下程序的运行结果          

程序类笔试题目

1、 

 public class test {
 int i = 1;
 int j = 3;
 
 public test(int m, int n){
  this.i = m;
  this.j = n;
 }
 public test(){}

 public static void main(string[] args) {
  new test()();
 }
 
 public void test(){
  new test(4,2);
  tln("i="+i+",j="+j);
 }
}

  我写的是 i=2, j=4。 而实际上的运行结果为i=1, j=3。

  

2、 

public class a {
 public int avar;
 
 public a(){
  tln("aaa");
  dosomething();
 }
 
 public void dosomething(){
  avar = 1111;
  tln("mething()");
 }

}

public class b extends a {
 
 public int bvar = 2222;
 public b(){
  tln("bbbb");
  dosomething();
  tln("avar="+avar);
 }
 
 public void dosomething(){
  tln("bvar="+bvar);
 }
 
 public static void main(string[] args) {
  new b();

   }

}

我写的'运行结果为:

aaa

mething()

bbb

2222

1111

而实际运行结果为:

aaa
bvar=0
bbb
bvar=2222
avar=0

 

3、  interger integer;

        if(integer==42){   do  something  ...  }。 次代码片段在运行时抛 空指针异常。 因为integer为非基本类型的变量, 其默认值为null,  所以 ....     ... 

 

4、 

/**
  * 测试 string 的
  */
 public static void main(string[] args) {
  string str1 = new string("a");
  string str2 = new string("b");
  operatestring(str1, str2);
  tln("str1="+str1+",str2="+str2);
  int x=10;
  operateint(x);
  tln(x);
  
  stringbuffer str3 = new stringbuffer("a");
  stringbuffer str4 = new stringbuffer("b");
  operatestringbuffer(str3, str4);
  tln("str3="+str3+",str4="+str4);
  
  testoperator();
 }

 public static void operatestring(string a, string b){
  at(b);
  b=a;
 }
 
 public static void operateint(int x){
  x = x+100;
 }
 
 public static void operatestringbuffer(stringbuffer a, stringbuffer b){
  nd("b");
  b=a;
 }

输出结果为:  a, b, 10, ab, b.  此题给出了正解。

5、 string 类型的变量也支持重载符: “+=”。

 

6、  编程题, 实现 归并排序算法:

public class mergesort2 {

public int[] sort(int[] data) {
        int[] temp=new int[th];
        mergesort(data,temp,0,th-1);
        return data;
    }
   
    private void mergesort(int[] data,int[] temp,int l,int r){
        int mid=(l+r)/2;
        tln(l+", "+mid+", "+r);
        if(l==r) return ;
       
        mergesort(data,temp,l,mid);
        mergesort(data,temp,mid+1,r);
        for(int i=l;i<=r;i++){
        tln("i="+i);
            temp[i]=data[i];
        }
        int i1=l;
        int i2=mid+1;
        for(int cur=l;cur<=r;cur++){
            if(i1==mid+1)
                data[cur]=temp[i2++];
            else if(i2>r)
                data[cur]=temp[i1++];
            else if(temp[i1]<temp[i2])
                data[cur]=temp[i1++];
            else
                data[cur]=temp[i2++];           
        }
    }
   
/**
* @param args
*/
public static void main(string[] args) {
int[] datas={1,21,34,79,98,23,68,2,3,8,6,33,6,7,87,32,24,6,776};
mergesort2 mergesort=new mergesort2();
datas=(datas);
for(int i=0;i<th;i++){
t(datas[i]+",");
}
}
}

 

TAGS:题目 笔试