一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

JAVA 面试题

时间:2008-04-28 编辑:简简单单 来源:一聚教程网

1. 不允许访问 super.super.xxx 为什么?

super是当前类的私有成员,代表着父类,
super.super的意思是要访问父类中的私有成员,这个是不可能访问得到的

 

2.



public   class   pp 

  
int   x=5,y=10
  
void   setv(int   a,int   b) 
  

    x
=a; 
    y
=b; 
  }
 
    
int   get_1() 
  

    
return   x+y; 
}
   
  
int   get_2() 
  

    
return   x-y; 
  }
 
  
public   class   test   extends   pp 
  

      
int   y; 
test(
int   a)
    y
=a; 
}
 
  sety(
int   a,int   b) 

  
int   get_2()
  
return   y; 
}
 
  }
 
/*
1.   用pp   a1=new   pp()后,a1.get_1()的内容为_________ 
2.   用pp   a1=new   pp()后,a1.setv(10.10)后a1.get_2()内容_________ 
3.   用test   a1=new   test(1)后,a1.get_1()的内容为____________ 
4.   用test   a1=new   test(-1),a1.setv(5.5)后,a1.get_2内容____________
*/
3. Object类中有clone方法,但是Object又没有实现cloneable接口,这是为什么?对于一个没有实现cloneable的类来说,还是可以用从Object类继承而来的clone方法实现一些基本的值复制操作,那是不是可以说clone方法并没有对对象是否属于cloneable类型进行检验?

1、15
2、0
3、6
4、6

 

> > > Object类中有clone方法,但是Object又没有实现cloneable接口,这是为什么?
Object类中的clone方法是protected的,它本省不是cloneable的,只是希望继承的class能够方便实现cloneable接口

> > > 对于一个没有实现cloneable的类来说,还是可以用从Object类继承而来的clone方法实现一些基本的值复制操作,那是不是可以说clone方法并没有对对象是否属于cloneable类型进行检验?
是的,实现了clone方法并不代表这个类就是cloneable类型的

protected的   clone方法,只能在类内调用,但是实现了cloneable接口的话,就可以不仅仅在类内来使用这个拷贝方法了

 

4.Which   of   the   following   statements   about   declaration   are   true?

A.   Declaration   of   primitive   types   such   as   boolean,   byte   and   so   on   does   not   allocate   memory   space   for   the   variable.

B.   Declaration   of   primitive   types   such   as   boolean,   byte   and   so   on   allocates   memory   space   for   the   variable.

C.   Declaration   of   nonprimitive   types   such   as   String,   Vector   and   so   on   does   not   allocate   memory   space   for   the   object.

D.   Declaration   of   nonprimitive   types   such   as   String,   Vector   ans   so   on   allocates   memory   space   for   the   object.

1,全局变量:
      声明简单变量的时候系统会在stack(堆栈)中给它分配空间并给一个默认值的;
      声明对象的时候,系统也会在stack(堆栈)中给他分配一个对象的指针,但只是指向null,而不是该对象的内存空间。
2,局部变量:
    声明的时候都会在stack中分配空间(只不过对象的给分配的仍然是对象的指针),并且默认系统都不给初始化,因此只能先初始化才能使用。

由以上两点可以看出,BC是正确地。

 

 

5.一个文本文件中约有10万多行的数据,每个数据占一行(数据为一个整数)。要求:统计出总行数,并找出出现次数最多的那个整数。

大家帮忙,关键是怎样找出出现次数最多的那个数

 



package   test; 

import   java.io.*
import   java.util.ArrayList; 

public   class   TestArray   

/* 
  *   public   Object   []getList(){//获得数组!   
  *   try{   ArrayList   list=new   ArrayList(); 
  *   BufferedReader   in   = 
  *     new   BufferedReader(new   FileReader( "d:test ")); 
  *   String   str;   
  *   if((str=in.readLine())!=null){   
  *   list.add(str); 
  *     } 
  *       return   list.toArray(); 
  *   
  *   }catch(Exception   e){ 
  *     e.printStackTrace();   
  *     return   null;   } 
  *       } 
  
*/
 
public   Object[]   getList()   {//   测试数组 
String[]   bb   =   new   String[200]; 
for   (int   i   =   0;   i   <   bb.length;   i++)   
bb[i]   
=   String.valueOf((int)   (Math.random()   *   50)); 
}
 
return   bb; 
}
 

public   int   getSize()   {//   统计出总行数 
Object[]   o   =   getList(); 
return   o.length; 
}
 

public   int[]   getMaxSameNo()   {//   找出出现次数最多的那个数组 

Object   o[]   
=   getList(); 

int   no[]   =   new   int[o.length]; 
for   (int   i   =   0;   i   <   o.length;   i++)   
no[i]   
=   Integer.parseInt((String)   o[i]); 

}
 
java.util.Arrays.sort(no);
//   数组排序 
for   (int   i   =   0;   i   <   no.length;   i++)   
System.out.println( 
"array[ "   +   i   +   "]= "   +   no[i]); 
}
 

int   maxsum   =   getSum(no);//   出现次数最多的数到底出现了几次 
return   getInt(no,   maxsum);//   找出出现次数最多的那个数 

}
 

public   int   getSum(int[]   no)   {//   此方法返回出现次数最多的数到底出现了几次! 
ArrayList   sumlist   =   new   ArrayList(); 
int   sum   =   1
for   (int   i   =   0;   i   <   no.length   -   1;   i++)   
if   (no[i]   ==   no[i   +   1])   
sum
++;//   相临两个相等计数器+1 
}
   else   
//   不相等向集合里加入 
sumlist.add(sum); 
//   计数器复位继续比较 
sum   =   1
continue
}
 
}
 
int   max   =   0
for   (int   i   =   0;   i   <   sumlist.size();   i++)   {//   此循环取出集合里最大的数! 
if   (Integer.parseInt(sumlist.get(i).toString())   >   max)   
max   
=   Integer.parseInt(sumlist.get(i).toString()); 
}
 
}
 
return   max; 
}
 

public   int[]   getInt(int[]   no,   int   a)   {//   此方法返回出现次数为a的数组,可能有多个数字出现相同的次数的情况,所以返回的是数组 
ArrayList   sumlist   =   new   ArrayList(); 
int   sum   =   1
for   (int   i   =   0;   i   <   no.length   -   1;   i++)   
if   (no[i]   ==   no[i   +   1])   
sum
++
}
   else   
if   (sum   ==   a)   
sumlist.add(no[i]); 
System.out.println(no[i]   
+   "一共出现了 "   +   a   +   "次! "); 
sum
=1
continue
}
   else   
sum   
=   1
continue
}
 

}
 
}
 
int   aa[]   =   new   int[sumlist.size()]; 
for   (int   i   =   0;   i   <   aa.length;   i++)   
aa[i]   
=   Integer.parseInt((sumlist.get(i).toString())); 
}
 
return   aa; 

}
 

/** 
  *   
@param   args 
  
*/
 
public   static   void   main(String[]   args)   
//   TODO   自动生成方法存根 
TestArray   test   =   new   TestArray(); 
int   count   =   test.getSize(); 
System.out.println( 
"一共有 "   +   count   +   "条记录! "); 
int[]   max   =   test.getMaxSameNo(); 
System.out.println( 
"出现次数最多的数为: "); 
for   (int   i   =   0;   i   <   max.length;   i++)   
System.out.print(max[i]   
+   ""); 
}
 

}
 

}
 


<