JAVA面试题锦集

发布时间 : 星期四 文章JAVA面试题锦集更新完毕开始阅读

再一个例题:

public class OuterClass { private double d1 = 1.0。 //insert code here }

You need to insert an inner class declaration at line 3. Which two inner class declarations are valid?(Choose two.) A. class InnerOne{

public static double methoda() {return d1。} }

B. public class InnerOne{

static double methoda() {return d1。} }

C. private class InnerOne{ double methoda() {return d1。} }

D. static class InnerOne{

protected double methoda() {return d1。} }

E. abstract class InnerOne{ public abstract double methoda()。 }

说明如下:

一.静态内部类可以有静态成员,而非静态内部类则不能有静态成员。 故 A、B 错

二.静态内部类的非静态成员可以访问外部类的静态变量,而不可访问外部类的非静态变量;return d1 出错。 故 D 错

三.非静态内部类的非静态成员可以访问外部类的非静态变量。 故 C 正确 四.答案为C、E

5、Java 的通信编程,编程题(或问答),用JAVA SOCKET编程,读服务器几个字符,再写入本地显示? 答:Server端程序: package test。 import java.net.*。 import java.io.*。 public class Server {

private ServerSocket ss。 private Socket socket。 private BufferedReader in。 private PrintWriter out。 public Server() { try {

ss=new ServerSocket(10000)。

while(true) {

socket = ss.accept()。

String RemoteIP = socket.getInetAddress().getHostAddress()。 String RemotePort = \。

System.out.println(\。 in = new BufferedReader(new

InputStreamReader(socket.getInputStream()))。 String line = in.readLine()。

System.out.println(\。

out = new PrintWriter(socket.getOutputStream(),true)。 out.println(\。 out.close()。 in.close()。 socket.close()。 }

}catch (IOException e) {

out.println(\。 } }

public static void main(String[] args) {

new Server()。 } }。

Client端程序: package test。 import java.io.*。 import java.net.*。 public class Client {

Socket socket。 BufferedReader in。 PrintWriter out。 public Client() { try {

System.out.println(\。 socket = new Socket(\。 System.out.println(\。 System.out.println(\。 BufferedReader line = new BufferedReader(new

InputStreamReader(System.in))。

out = new PrintWriter(socket.getOutputStream(),true)。 out.println(line.readLine())。

in = new BufferedReader(new InputStreamReader(socket.getInputStream()))。 System.out.println(in.readLine())。 out.close()。 in.close()。 socket.close()。 }catch(IOException e) {

out.println(\。 } }

public static void main(String[] args) {

new Client()。 } }。

6、用JAVA实现一种排序,JAVA类实现序列化的方法(二种)? 如在COLLECTION框架中,实现比较要实现什么样的接口?

答:用插入法进行排序代码如下 package test。 import java.util.*。 class InsertSort {

ArrayList al。

public InsertSort(int num,int mod) {

al = new ArrayList(num)。 Random rand = new Random()。

System.out.println(\。 for (int i=0。i

al.add(new Integer(Math.abs(rand.nextInt()) % mod + 1))。 System.out.println(\。 } }

public void SortIt() {

Integer tempInt。 int MaxSize=1。

for(int i=1。i

tempInt = (Integer)al.remove(i)。

if(tempInt.intvalue()>=((Integer)al.get(MaxSize-1)).intvalue()) {

al.add(MaxSize,tempInt)。 MaxSize++。

System.out.println(al.toString())。 } else {

for (int j=0。j

(((Integer)al.get(j)).intvalue()>=tempInt.intvalue()) {

al.add(j,tempInt)。 MaxSize++。

System.out.println(al.toString())。 break。 } } } }

System.out.println(\。 for(int i=0。i

System.out.println(\。 } }

public static void main(String[] args) {

InsertSort is = new InsertSort(10,100)。 is.SortIt()。 } }

JAVA类实现序例化的方法是实现java.io.Serializable接口

Collection框架中实现比较要实现Comparable 接口和 Comparator 接口

7、编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。 答:代码如下: package test。 class SplitString {

String SplitStr。 int SplitByte。

public SplitString(String str,int bytes) {

联系合同范文客服:xxxxx#qq.com(#替换为@)