java面试资料(1)
1、作用域public,private,protected,以及不写时的区别
答:区别如下:
package test; public class FatherClass { public FatherClass() { System.out.println("FatherClass Create"); } }
子类:
package test; import test.FatherClass; public class ChildClass extends FatherClass { public ChildClass() { System.out.println("ChildClass Create"); } public static void main(String[] args) { FatherClass fc = new FatherClass(); ChildClass cc = new ChildClass(); } }
package test; public class OuterClass { private class InterClass { public 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(); } }
private double d1 = 1.0; //insert code here } You need to insert an inner class declaration at line 3. Which two inner class declarations arevalid?(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(); }
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("The ArrayList Sort Before:"); for (int i=0;i<num ;i++ ) { 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<al.size();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<MaxSize ;j++ ) { if(((Integer)al.get(j)).intValue()>=tempInt.intValue()) { al.add(j,tempInt); MaxSize++; System.out.println(al.toString()); break; } } } } System.out.println("The ArrayList Sort After:"); for(int i=0;i<al.size();i++) { System.out.println("al["+i+"]="+al.get(i)); } } public static void main(String[] args) { InsertSort is = new InsertSort(10,100); is.SortIt(); } }
public class ServletName extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
package hello.ant; import java.sql.*; public class jdbc { String 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() { try{ Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); c = DriverManager.getConnection(dbUrl,theUser,thePw); conn=c.createStatement(); }catch(Exception e){ e.printStackTrace(); } } public boolean executeUpdate(String sql) { try { conn.executeUpdate(sql); return true; } catch (SQLException e) { e.printStackTrace(); return false; } } public ResultSet executeQuery(String sql) { rs=null; try { rs=conn.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public void close() { try { conn.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"); try{ while (rs.next()) { System.out.println(rs.getString("id")); System.out.println(rs.getString("name")); } }catch(Exception e) { e.printStackTrace(); } } }
package test; import java.io.*; public class DOMTest { private String inFile = "c:\\people.xml" private String outFile = "c:\\people.xml" public static void main(String args[]) { new DOMTest(); } public DOMTest() { try { javax.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.createElement("王"); 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.transform.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()); } } }
<?xml version="1.0" encoding="gb2312"?> <person> <name>王小明</name> <college>信息学院</college> <telephone>6258113</telephone> <notes>男,1955年生,博士,95年调入海南大学</notes> </person>
import java.io.*; import java.util.Hashtable; import org.xml.sax.*; public class SAXHandler extends HandlerBase { private Hashtable table = new Hashtable(); private String currentElement = null; private String currentValue = null; public void setTable(Hashtable table) { this.table = table; } public Hashtable getTable() { return table; } public void startElement(String tag, AttributeList attrs) throws SAXException { currentElement = tag; } public void characters(char[] ch, int start, int length) throws SAXException { currentValue = new String(ch, start, length); } public void endElement(String name) throws SAXException { if (currentElement.equals(name)) table.put(currentElement, currentValue); } }
<HTML> <HEAD> <TITLE>剖析XML文件people.xml</TITLE> </HEAD> <BODY> <%@ page errorPage="ErrPage.jsp" contentType="text/html;charset=GB2312" %> <%@ page import="java.io.*" %> <%@ page import="java.util.Hashtable" %> <%@ page import="org.w3c.dom.*" %> <%@ page import="org.xml.sax.*" %> <%@ page import="javax.xml.parsers.SAXParserFactory" %> <%@ page import="javax.xml.parsers.SAXParser" %> <%@ page import="SAXHandler" %> <% File file = new File("c:\\people.xml"); FileReader reader = new FileReader(file); Parser parser; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); SAXHandler handler = new SAXHandler(); sp.parse(new InputSource(reader), handler); Hashtable hashTable = handler.getTable(); out.println("<TABLE BORDER=2><CAPTION>教师信息表</CAPTION>"); out.println("<TR><TD>姓名</TD>" + "<TD>" + (String)hashTable.get(new String("name")) + "</TD></TR>"); out.println("<TR><TD>学院</TD>" + "<TD>" + (String)hashTable.get(new String("college"))+"</TD></TR>"); out.println("<TR><TD>电话</TD>" + "<TD>" + (String)hashTable.get(new String("telephone")) + "</TD></TR>"); out.println("<TR><TD>备注</TD>" + "<TD>" + (String)hashTable.get(new String("notes")) + "</TD></TR>"); out.println("</TABLE>"); %> </BODY> </HTML>