华为应届生面试题库 – Java方向 包含答案及评分标准(二)

2014-11-24 00:54:38 · 作者: · 浏览: 55
other;


}


运行结果是?


【参考答案】10020


【题号】10


【级别】中级


【验证】正确


【题目】下面是一段读取d盘下test.txt文件内容,并打印其内容指出下面


代码中的错误并改正。


import jav aio.*;


public class FileTest


{


public staticvoid main (String[]args)


{



File f=new File(“d:/test.txt”)


Byte[]b=new byte[1024;


Int point=0


While((poin=fi.read(b))!=-1)


{


for(inti=0;i《point;i++)


{


System.out.pnint(b[i]);


}


}


}





【参考答案】


1、文件输入流使用错误,不应该使用File,应该使用Filelnputstream。


2、文件名格式错误,应该是d://test.txt。


3、没有捕获I0异常。


4、输入流没有关闭。


修改后的代码为:


import java.io.*;


public class FileTest


{


public tatic void main (String{} args){


FileinputStream fi =null;


try{


fi =new FileinputStream(”d://test.txt“);


. byte[] b=new byte[1024];


int point = 0


while((point=fi.read(b))!=-1){


for(int i=0;i〈point:i++〉{


System.out.print(b[i]);


}


}


fi.close();


}catch(IOException e){}


finally{


try{


if (fi != null){


fi.close();


}


}catch(IOException ioe){}


}


}


这里不太好吧,文件后缀名为txt,但是直接打印字节不太好。建议把est.txt换成tet.dat。【题号】11


【级别】中级


【验证】正确


【题目】写一段代码建立一个直角在左下角的等腰直角三角形的二维数组,腰的长度是10,数组元素是String,需要赋初始值为(x,y),其中x,y为坐标的位置,从左下角为顶点(0,0),依次向上向右增加1。程序片断如下,请填空:


String[][]trg = 1


for(int i= 2 ; i)=0;i-)


{


trg[i]= 3 ;


for(int j=0; j〈 4 ; j++)



{


trg[i][j]= 5


}


}


for(int i=9;i)0;i–){


for(int j=0;j《trg[i],length;j++){


System.out.print(trg[i][j]);


}




【参考答案】


1、new String[10][]


2、9


3、new STRING[10-I]


4、trg[i].length


5、“(”+|i+”.”+j+”) ”








【题号】12


【级别】中级


【验证】正确


【题目】描述一下LinkedList.MrrayList,Vector的区别,例如,


数据存储,特性,性能等。


【参考答案】


LinkedList 使用双链表来实现,插入性能好,方法不是同步的,查找比


ArrayList慢,适用于数据频繁改变的场合.


Vector实现方式同ArrayList,方法是同步的


【题号】13


【级别】中级


【验证】正确


【题目】写一段代码应用BorderLayout管理器,在东南西北中四个方位各加入


一个按钮。


【参考答案】


setLayout(new Borderlayout();


add(new Button(“东”),BorderLayout.EAST ;


add(new Button(“西”),BorderLayout.WEST ;


add(new Button(“中”),BorderLayout. CENTER;


add(new Button(“北”),BorderLayout.NORTH ;


add(new Button(“南”),BorderLayout. SOUTH;







【题号】14


【级别】中级


【验证】正确


【题目】考虑用自己的类实现StringBuffer的字符串相加功能,要求越接近StringBufferr的性能越好,请说出其中数据存储和扩充长度的思路。


【参考答案】


思路1:使用char[]来保存字符串,扩充长度的时候使用System.arraycopy()把老的数组中的数据自制到新的数组中。


思路2:使用List/Vector来保存字符串,自动扩充长充。




【题号】15


【级别】中级


【验证】正确


【题目】填空:实现一个Socket服务端,(10。76。149。158:1800)接收客户端发送的消息并显示。


MyServertest.java


import java.net*;


import java .io*


public class MyServerTest


{


public static void main(String[] args) throws IOException{


ServerSocket myserversocket=null


try{


myserversocket= 1


}catch(IOException e){


System.err.println(”Count not listen on port.”);


System.exit(1);


}


Soxket myclientSocket = null


try {


myclientSocket = 2


}catch(I0Execption e){


System.err.println(”Accept failed”);


System.exit(1)


}


DataInputStream myinstream =new DataInputStream(new


BufferedInputStream( 3 ))


; String inputline = myinstream.readling()EAST)


System.out.println(inputline)


4


myclientSocket.close()


myserversocket.close();





























【评分标准:每题10分,多选题和多个填空题全对得满分,如果部分答对:填空题按照填对的个数得部分分数,多选题中的答案如果其中有选错的选项别不得分,选对了但是不全面得3分,总分为50分合格,80分优秀】。


一、填空题:


1、Java的基本数据类型:


【答案】int,long,char,byte,short,boolean,float


2、有一个类定议如下:


package com.huawei.test;


public clasxs Definition


{


proctected void test1()


{


System.out.println(”This is Definition.test1()”)


}