1、java面试题资料仅供参考代码与编程题2135、写一个Singleton出来2138、Java 的通信编程,编程题(或问 ),用JAVA SOCKET编程,读服务器几个字符,再写入本地显示?4139、用JAVA实现一种排序,JAVA类实现序列化的方法(二种)? 如在COLLECTION框架中,实现比较要实现什么样的接口?6140、编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 可是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。8141、JAVA多线程编程。 用J
2、AVA写一个多线程程序,如写四个线程,二个加1,二个对一个变量减一,输出。9142、可能会让你写一段Jdbc连Oracle的程序,并实现数据查询.9143、ORACLE大数据量下的分页解决方法。一般用截取ID方法,还有是三层嵌套方法。11145、编程用JAVA解析XML的方式.13146、EJB的基本架构15147、如何校验数字型?15148、将一个键盘输入的数字转化成中文输出16149、JAVA代码查错1928、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。22代码与编程题135、写一个Singleton出来Singleton模式主要作用是保证在Java应用
3、程序中,一个类Class只有一个实例存在。一般Singleton模式一般有几种种形式:第一种形式: 定义一个类,它的构造函数为private的,它有一个static的private的该类变量,在类初始化时实例话,经过一个public的getInstance方法获取对它的引用,继而调用其中的方法。public class Singleton private Singleton() /在自己内部定义自己一个实例,是不是很奇怪? /注意这是private 只供内部调用 private static Singleton instance = new Singleton(); /这里提供了一个供外部访问
4、本class的静态方法,能够直接访问 public static Singleton getInstance() return instance; 第二种形式:public class Singleton private static Singleton instance = null;public static synchronized Singleton getInstance() /这个方法比上面有所改进,不用每次都进行生成对象,只是第一次 /使用时生成实例,提高了效率!if (instance=null)instancenew Singleton();return instance;
5、其它形式:定义一个类,它的构造函数为private的,所有方法为static的。一般认为第一种形式要更加安全些136、继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么?:父类:package test;public class FatherClasspublic FatherClass()System.out.println(FatherClass Create);子类:package test;import test.FatherClass;public class ChildClass extends FatherClasspublic ChildClass()System.o
6、ut.println(ChildClass Create);public static void main(String args)FatherClass fc = new FatherClass();ChildClass cc = new ChildClass();输出结果:C:java test.ChildClassFatherClass CreateFatherClass CreateChildClass Create137、内部类的实现方式?示例代码如下:package test;public class OuterClassprivate class InterClasspublic
7、 InterClass()System.out.println(InterClass Create);public OuterClass()InterClass ic = new InterClass();System.out.println(OuterClass Create);public static void main(String args)OuterClass oc = new OuterClass();输出结果:C:java test/OuterClassInterClass CreateOuterClass Create再一个例题:public class OuterClass
8、 private double d1 = 1.0;/insert code hereYou need to insert an inner class declaration at line 3. Which two inner class declarations arevalid?(Choose two.)A. class InnerOnepublic static double methoda() return d1;B. public class InnerOnestatic double methoda() return d1;C. private class InnerOnedou
9、ble methoda() return d1;D. static class InnerOneprotected double methoda() return d1;E. abstract class InnerOnepublic abstract double methoda();说明如下:一.静态内部类能够有静态成员,而非静态内部类则不能有静态成员。 故 A、B 错二.静态内部类的非静态成员能够访问外部类的静态变量,而不可访问外部类的非静态变量;return d1 出错。故 D 错三.非静态内部类的非静态成员能够访问外部类的非静态变量。 故 C 正确四. 案为C、E138、Java 的
10、通信编程,编程题(或问 ),用JAVA SOCKET编程,读服务器几个字符,再写入本地显示?Server端程序:package test;import .*;import java.io.*;public class Serverprivate ServerSocket ss;private Socket socket;private BufferedReader in;private PrintWriter out;public Server()tryss=new ServerSocket(10000);while(true)socket = ss.accept();String Remot
11、eIP = socket.getInetAddress().getHostAddress();String RemotePort = :+socket.getLocalPort();System.out.println(A client come in!IP:+RemoteIP+RemotePort);in = new BufferedReader(newInputStreamReader(socket.getInputStream();String line = in.readLine();System.out.println(Cleint send is : + line);out = n
12、ew PrintWriter(socket.getOutputStream(),true);out.println(Your Message Received!);out.close();in.close();socket.close();catch (IOException e)out.println(wrong);public static void main(String args)new Server();Client端程序:package test;import java.io.*;import .*;public class ClientSocket socket;Buffered
13、Reader in;PrintWriter out;public Client()trySystem.out.println(Try to Connect to 127.0.0.1:10000);socket = new Socket(127.0.0.1,10000);System.out.println(The Server Connected!);System.out.println(Please enter some Character:);BufferedReader line = new BufferedReader(newInputStreamReader(System.in);o
14、ut = 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(Wrong);public static void main(String args)new
15、Client();139、用JAVA实现一种排序,JAVA类实现序列化的方法(二种)? 如在COLLECTION框架中,实现比较要实现什么样的接口?:用插入法进行排序代码如下package test;import java.util.*;class InsertSortArrayList al;public InsertSort(int num,int mod)al = new ArrayList(num);Random rand = new Random();System.out.println(The ArrayList Sort Before:);for (int i=0;inum ;i
16、+ )al.add(new Integer(Math.abs(rand.nextInt() % mod + 1);System.out.println(al+i+=+al.get(i);public void SortIt()Integer tempInt;int MaxSize=1;for(int i=1;i=(Integer)al.get(MaxSize-1).intValue()al.add(MaxSize,tempInt);MaxSize+;System.out.println(al.toString(); else for (int j=0;j=tempInt.intValue()a
17、l.add(j,tempInt);MaxSize+;System.out.println(al.toString();break;System.out.println(The ArrayList Sort After:);for(int i=0;ial.size();i+)System.out.println(al+i+=+al.get(i);public static void main(String args)InsertSort is = new InsertSort(10,100);is.SortIt();140、编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截
18、取的字符串。 可是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。代码如下:package test;class SplitStringString SplitStr;int SplitByte;public SplitString(String str,int bytes)SplitStr=str;SplitByte=bytes;System.out.println(The String is:+SplitStr+;SplitBytes=+SplitByte);public void SplitIt
19、()int loopCount;loopCount=(SplitStr.length()%SplitByte=0)?(SplitStr.length()/SplitByte):(SplitStr.length()/SplitByte+1);System.out.println(Will Split into +loopCount);for (int i=1;i=loopCount ;i+ )if (i=loopCount)System.out.println(SplitStr.substring(i-1)*SplitByte,SplitStr.length(); else System.out
20、.println(SplitStr.substring(i-1)*SplitByte,(i*SplitByte);public static void main(String args)SplitString ss = new SplitString(test中dd文dsaf中男大3443n中国43中国人0ewldfls=103,4);ss.SplitIt();141、JAVA多线程编程。 用JAVA写一个多线程程序,如写四个线程,二个加1,二个对一个变量减一,输出。希望大家补上,谢谢142、可能会让你写一段Jdbc连Oracle的程序,并实现数据查询.:程序如下:package hello.
21、ant;import java.sql.*;public class jdbcString dbUrl=jdbc:oracle:thin:127.0.0.1:1521:orcl;String theUser=admin;String thePw=manager;Connection c=null;Statement conn;ResultSet rs=null;public jdbc()tryClass.forName(oracle.jdbc.driver.OracleDriver).newInstance();c = DriverManager.getConnection(dbUrl,the
22、User,thePw);conn=c.createStatement();catch(Exception e)e.printStackTrace();public boolean executeUpdate(String sql)tryconn.executeUpdate(sql);return true;catch (SQLException e)e.printStackTrace();return false;public ResultSet executeQuery(String sql)rs=null;tryrs=conn.executeQuery(sql);catch (SQLExc
23、eption e)e.printStackTrace();return rs;public void close()tryconn.close();c.close();catch (Exception e)e.printStackTrace();public static void main(String args)ResultSet rs;jdbc conn = new jdbc();rs=conn.executeQuery(select * from test);trywhile (rs.next()System.out.println(rs.getString(id);System.ou
24、t.println(rs.getString(name);catch(Exception e)e.printStackTrace();143、ORACLE大数据量下的分页解决方法。一般用截取ID方法,还有是三层嵌套方法。:一种分页方法=intPageCount) nextPage=intPageCount;upPage = currentPage-1;if (upPage=1) upPage=1;rs.close();sql=select * from tables;rs=DBLink.executeQuery(sql);i=0;while(i/输出内容/输出翻页连接合计:/第一页ahref=
25、List.jsp?page=上一页%for(int j=1;ja href=list.jsp?page=a href=List.jsp?page=下一页a href=List.jsp?page=最后页144、用jdom解析xml文件时如何解决中文问题?如何解析?:看如下代码,用编码方式加以解决package test;import java.io.*;public class DOMTestprivate String inFile = c:people.xml;private String outFile = c:people.xml;public static void main(Stri
26、ng args)new DOMTest();public DOMTest()tryjavax.xml.parsers.DocumentBuilder builder =javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();org.w3c.dom.Document doc = builder.newDocument();org.w3c.dom.Element root = doc.createElement(老师);org.w3c.dom.Element wang = doc.createEleme
27、nt(王);org.w3c.dom.Element liu = doc.createElement(刘);wang.appendChild(doc.createTextNode(我是王老师);root.appendChild(wang);doc.appendChild(root);javax.xml.transform.Transformer transformer =javax.xml.transform.TransformerFactory.newInstance().newTransformer();transformer.setOutputProperty(javax.xml.tran
28、sform.OutputKeys.ENCODING, gb2312);transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, yes);transformer.transform(new javax.xml.transform.dom.DOMSource(doc),newjavax.xml.transform.stream.StreamResult(outFile);catch (Exception e)System.out.println (e.getMessage();145、编程用JAVA解析XML的方式.
29、:用SAX方式解析XML,XML文件如下:王小明信息学院6258113男,1955年生,博士,95年调入海南大学事件回调类SAXHandler.javaimport java.io.*;import java.util.Hashtable;import org.xml.sax.*;public class SAXHandler extends HandlerBaseprivate Hashtable table = new Hashtable();private String currentElement = null;private String currentValue = null;pu
30、blic void setTable(Hashtable table)this.table = table;public Hashtable getTable()return table;public void startElement(String tag, AttributeList attrs)throws SAXExceptioncurrentElement = tag;public void characters(char ch, int start, int length)throws SAXExceptioncurrentValue = new String(ch, start,
31、 length);public void endElement(String name) throws SAXExceptionif (currentElement.equals(name)table.put(currentElement, currentValue);JSP内容显示源码,SaxXml.jsp:剖析XML文件people.xml%File file = new File(c:people.xml);FileReader reader = new FileReader(file);Parser parser;SAXParserFactory spf = SAXParserFact
32、ory.newInstance();SAXParser sp = spf.newSAXParser();SAXHandler handler = new SAXHandler();sp.parse(new InputSource(reader), handler);Hashtable hashTable = handler.getTable();out.println(教师信息表);out.println(姓名 + +(String)hashTable.get(new String(name) + );out.println(学院 + +(String)hashTable.get(new St
33、ring(college)+);out.println(电话 + +(String)hashTable.get(new String(telephone) + );out.println(备注 + +(String)hashTable.get(new String(notes) + );out.println();%146、EJB的基本架构:一个EJB包括三个部分:Remote Interface 接口的代码package Beans;import javax.ejb.EJBObject;import java.rmi.RemoteException;public interface Add
34、extends EJBObject/some method declareHome Interface 接口的代码package Beans;import java.rmi.RemoteException;import jaax.ejb.CreateException;import javax.ejb.EJBHome;public interface AddHome extends EJBHome/some method declareEJB类的代码package Beans;import java.rmi.RemoteException;import javax.ejb.SessionBea
35、n;import javx.ejb.SessionContext;public class AddBean Implements SessionBean/some method declare147、如何校验数字型?var re=/d1,8$|.d1,2$/;var str=document.form1.all(i).value;var r=str.match(re);if (r=null)sign=-4;break;elsedocument.form1.all(i).value=parseFloat(str);148、将一个键盘输入的数字转化成中文输出(例如:输入:1234567 输出:一百
36、二拾三万四千五百六拾七)用java语言实现,请编一段程序实现!public class Reader private String strNum;private String strNumChFormat;private String strNumTemp;private int intNumLen;private String strBegin;public Reader(String strNum) this.strNum = strNum;public boolean check(String strNum) boolean valid = false;if (strNum.substr
37、ing(0,1).equals(0)this.strNum = strNum.substring(1);try new Double(strNum);valid = true;catch (NumberFormatException ex) System.out.println(Bad number format!);return valid;public void init() strNumChFormat = ;intNumLen = strNum.length();strNumTemp = strNum;strNumTemp = strNumTemp.replace(1, 一);strN
38、umTemp = strNumTemp.replace(2, 二);strNumTemp = strNumTemp.replace(3, 三);strNumTemp = strNumTemp.replace(4, 四);strNumTemp = strNumTemp.replace(5, 五);strNumTemp = strNumTemp.replace(6, 六);strNumTemp = strNumTemp.replace(7, 七);strNumTemp = strNumTemp.replace(8, 八);strNumTemp = strNumTemp.replace(9, 九);strNumTemp = strNumTemp.replace(0, 零);strNumTemp = strNumTemp.replace(., 点);strBegin = strNumTemp.substring(0, 1);public String readNum() if (check(strNum) init();try for (int i = 1, j = 1, k = 1; i intNumLen; i+) if (strNumTemp.charAt(intNumLen - 1) = 零 & i = 1) strNumChFormat = 位