- 相關(guān)推薦
J2EE經(jīng)典面試試題及答案
在現(xiàn)實(shí)的學(xué)習(xí)、工作中,我們最少不了的就是試題了,試題可以幫助參考者清楚地認(rèn)識(shí)自己的知識(shí)掌握程度。什么類型的試題才能有效幫助到我們呢?以下是小編幫大家整理的J2EE經(jīng)典面試試題及答案,供大家參考借鑒,希望可以幫助到有需要的朋友。
一、基礎(chǔ)問(wèn)答
1.下面哪些類可以被繼承?
java.lang.Thread (T)
java.lang.Number (T)
java.lang.Double (F)
java.lang.Math (F)
java.lang.Void (F)
java.lang.Class (F)
java.lang.ClassLoader(T)
2.抽象類和接口的區(qū)別
(1)接口可以被多重implements,抽象類只能被單一extends
(2)接口只有定義,抽象類可以有定義和實(shí)現(xiàn)
(3)接口的字段定義默認(rèn)為:public static final, 抽象類字段默認(rèn)是"friendly"(本包可見(jiàn))
3.Hashtable的原理,并說(shuō)出HashMap與Hashtable的區(qū)別
HashTable的原理:通過(guò)節(jié)點(diǎn)的關(guān)鍵碼確定節(jié)點(diǎn)的存儲(chǔ)位置,即給定節(jié)點(diǎn)的關(guān)鍵碼k,通過(guò)一定的函數(shù)關(guān)系H(散列函數(shù)),
得到函數(shù)值H(k), 將此值解釋為該節(jié)點(diǎn)的存儲(chǔ)地址.
HashMap與Hashtable很相似,但HashMap是非同步(unsynchronizded)和可以以null為關(guān)鍵碼的.
4.forward和redirect的區(qū)別
forward:請(qǐng)求轉(zhuǎn)發(fā),an internal transfer in servlet
redirect:重定向,有2次request,第2次request將丟失第一次的attributs/parameters等
5.什么是Web容器?
實(shí)現(xiàn)J2EE規(guī)范中web協(xié)議的應(yīng)用.該協(xié)議定義了web程序的運(yùn)行時(shí)環(huán)境,包括:并發(fā)性,安全性,生命周期管理等等.
6.解釋下面關(guān)于J2EE的名詞
(1)JNDI:Java Naming& Directory Interface,JAVA命名目錄服務(wù).
主要提供的功能是:提供一個(gè)目錄系統(tǒng),讓其它各地的應(yīng)用程序在其上面留下自己的索引,從而滿足快速查找和定位分布式應(yīng)用程序的功能.
(2)JMS:Java Message Service,JAVA消息服務(wù).主要實(shí)現(xiàn)各個(gè)應(yīng)用程序之間的通訊.包括點(diǎn)對(duì)點(diǎn)和廣播.
(3)JTA:Java Transaction API,JAVA事務(wù)服務(wù).提供各種分布式事務(wù)服務(wù).應(yīng)用程序只需調(diào)用其提供的接口即可.
(4)JAF: Java ActionFrameWork,JAVA安全認(rèn)證框架.提供一些安全控制方面的框架.讓開(kāi)發(fā)者通過(guò)各種部署和自定義實(shí)現(xiàn)自己的個(gè)性安全控制策略.
(5)RMI:Remote MethodInterface,遠(yuǎn)程方法調(diào)用
7.EJB是基于哪些技術(shù)實(shí)現(xiàn)的?并說(shuō)出SessionBean和EntityBean的區(qū)別,StatefulBean和StatelessBean的區(qū)別.
EJB包括Session Bean、Entity Bean、Message Driven Bean,基于JNDI、RMI、JAT等技術(shù)實(shí)現(xiàn).
SessionBean在J2EE應(yīng)用程序中被用來(lái)完成一些服務(wù)器端的業(yè)務(wù)操作,例如訪問(wèn)數(shù)據(jù)庫(kù)、調(diào)用其他EJB組件.
EntityBean被用來(lái)代表應(yīng)用系統(tǒng)中用到的數(shù)據(jù).
對(duì)于客戶機(jī),SessionBean是一種非持久性對(duì)象,它實(shí)現(xiàn)某些在服務(wù)器上運(yùn)行的業(yè)務(wù)邏輯;
EntityBean是一種持久性對(duì)象,它代表一個(gè)存儲(chǔ)在持久性存儲(chǔ)器中的實(shí)體的對(duì)象視圖,或是一個(gè)由現(xiàn)有企業(yè)應(yīng)用程序?qū)崿F(xiàn)的實(shí)體.
Session Bean 還可以再細(xì)分為 Stateful Session Bean 與 Stateless Session Bean.
這兩種的Session Bean都可以將系統(tǒng)邏輯放在method之中執(zhí)行,不同的是 StatefulSession Bean可以記錄使用者的狀態(tài)。
因此通常來(lái)說(shuō),一個(gè)使用者會(huì)有一個(gè)相對(duì)應(yīng)的 Stateful Session Bean 的實(shí)體.
Stateless Session Bean雖然也是邏輯組件,但是他卻不負(fù)責(zé)記錄使用者狀態(tài),
也就是說(shuō)當(dāng)使用者呼叫Stateless Session Bean的時(shí)候,EJBContainer并不會(huì)找尋特定的 StatelessSession Bean的實(shí)體來(lái)執(zhí)行這個(gè)method.
換言之,很可能數(shù)個(gè)使用者在執(zhí)行某個(gè) Stateless Session Bean的methods時(shí),會(huì)是同一個(gè)Bean的Instance在執(zhí)行.
從內(nèi)存方面來(lái)看,Stateful Session Bean與StatelessSession Bean比較,Stateful Session Bean會(huì)消耗J2EE Server較多的內(nèi)存,
然而Stateful Session Bean 的優(yōu)勢(shì)卻在于他可以維持使用者的狀態(tài).
8.XML的解析方法
StAX, SAX, DOM
9.什么是Web Service?
Web Service就是為了使原來(lái)各孤立的站點(diǎn)之間的信息能夠相互通信、共享而提出的一種接口。
Web Service所使用的是Internet上統(tǒng)一、開(kāi)放的標(biāo)準(zhǔn),如HTTP、XML、SOAP(簡(jiǎn)單對(duì)象訪問(wèn)協(xié)議)、WSDL等,
所以Web Service可以在任何支持這些標(biāo)準(zhǔn)的環(huán)境(Windows,Linux)中使用。
注:SOAP協(xié)議(Simple Object Access Protocal,簡(jiǎn)單對(duì)象訪問(wèn)協(xié)議)
它是一個(gè)用于分散和分布式環(huán)境下網(wǎng)絡(luò)信息交換的基于XML的通訊協(xié)議。
在此協(xié)議下,軟件組件或應(yīng)用程序能夠通過(guò)標(biāo)準(zhǔn)的HTTP協(xié)議進(jìn)行通訊。
它的設(shè)計(jì)目標(biāo)就是簡(jiǎn)單性和擴(kuò)展性,這有助于大量異構(gòu)程序和平臺(tái)之間的互操作性,從而使存在的應(yīng)用程序能夠被廣泛的用戶訪問(wèn)。
優(yōu)勢(shì):
(1)跨平臺(tái)。
(2)SOAP協(xié)議是基于XML和HTTP這些業(yè)界的標(biāo)準(zhǔn)的,得到了所有的重要公司的支持。
(3)由于使用了SOAP,數(shù)據(jù)是以ASCII文本的方式而非二進(jìn)制傳輸,調(diào)試很方便;
并且由于這樣,它的數(shù)據(jù)容易通過(guò)防火墻,不需要防火墻為了程序而單獨(dú)開(kāi)一個(gè)“漏洞”。
(4)此外,WebService實(shí)現(xiàn)的技術(shù)難度要比CORBA和DCOM小得多。
(5)要實(shí)現(xiàn)B2B集成,EDI比較完善與比較復(fù)雜;而用WebService則可以低成本的實(shí)現(xiàn),小公司也可以用上。
(6)在C/S的程序中,WebService可以實(shí)現(xiàn)網(wǎng)頁(yè)無(wú)整體刷新的與服務(wù)器打交道并取數(shù)。
缺點(diǎn):
(1)WebService使用了XML對(duì)數(shù)據(jù)封裝,會(huì)造成大量的數(shù)據(jù)要在網(wǎng)絡(luò)中傳輸。
(2)WebService規(guī)范沒(méi)有規(guī)定任何與實(shí)現(xiàn)相關(guān)的細(xì)節(jié),包括對(duì)象模型、編程語(yǔ)言,這一點(diǎn),它不如CORBA。
10.多線程有幾種實(shí)現(xiàn)方法,都是什么?同步有幾種實(shí)現(xiàn)方法,都是什么?
答:多線程有兩種實(shí)現(xiàn)方法,分別是繼承Thread類與實(shí)現(xiàn)Runnable接口
同步的實(shí)現(xiàn)方面有兩種,分別是synchronized,wait與notify
11.JSP中動(dòng)態(tài)INCLUDE與靜態(tài)INCLUDE的區(qū)別?
動(dòng)態(tài)INCLUDE用jsp:include動(dòng)作實(shí)現(xiàn)
它總是會(huì)檢查所含文件中的變化,適合用于包含動(dòng)態(tài)頁(yè)面,并且可以帶參數(shù)
靜態(tài)INCLUDE用include偽碼實(shí)現(xiàn),不會(huì)檢查所含文件的變化,適用于包含靜態(tài)頁(yè)面
。%@ includefile="included.htm" %>
二、Java編程與程序運(yùn)行結(jié)果
1.Java編程,打印昨天的當(dāng)前時(shí)刻
public class YesterdayCurrent{
public void main(String[] args){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println(cal.getTime());
}
}
2.文件讀寫(xiě),實(shí)現(xiàn)一個(gè)計(jì)數(shù)器
public int getNum(){
int i = -1;
try{
String stri="";
BufferedReader in = new BufferedReader(new FileReader(f));
while((stri=in.readLine())!=null){
i =Integer.parseInt(stri.trim());
}
in.close();
}catch(Exception e){
e.printStackTrace();
}
return i;
}
public void setNum(){
int i = getNum();
i++;
try{
PrintWriter out=new PrintWriter(new BufferedWriter(newFileWriter(f,false)));
out.write(String.valueOf(i));//可能是編碼的原因,如果直接寫(xiě)入int的話,將出現(xiàn)java編碼和windows編碼的混亂,因此此處寫(xiě)入的是String
out.close() ;
}catch(Exception e){
e.printStackTrace();
}
}
3. 指出下面程序的運(yùn)行結(jié)果:
class A{
static{
System.out.print("1");
}
public A(){
System.out.print("2");
}
}
class B extends A{
static{
System.out.print("a");
}
public B(){
System.out.print("b");
}
}
public class Hello{
public static void main(String[] ars){
A ab = new B(); //執(zhí)行到此處,結(jié)果: 1a2b
ab= new B(); //執(zhí)行到此處,結(jié)果: 1a2b2b
}
}
注:類的static 代碼段,可以看作是類首次加載(被虛擬機(jī)加載)執(zhí)行的代碼,而對(duì)于類的加載,首先要執(zhí)行其基類的構(gòu)造,再執(zhí)行其本身的構(gòu)造
4.寫(xiě)一個(gè)Singleton模式的例子
public class Singleton{
privatestatic Singleton single = new Singleton();
privateSingleton(){}
publicSingleton getInstance(){
return single;
}
}
三、數(shù)據(jù)庫(kù)
1.刪除表的重復(fù)記錄
如果記錄完全相同才算重復(fù)記錄,那么: (sql server2000下測(cè)試通過(guò))
select distinct * into #tmpp from tid
from tid
into tid select * from #tmpp
#tmpp
如果有id主鍵(數(shù)字,自增1的那種),那么:(sql server2000下測(cè)試通過(guò))
from tableA where id not in
(select id = min(id) from tableA group byname)
2. from tablea& truncate table tablea的區(qū)別
truncate 語(yǔ)句執(zhí)行速度快,占資源少,并且只記錄頁(yè)刪除的日志;
對(duì)每條記錄的刪除均需要記錄日志
JAVA編程題
1.現(xiàn)在輸入n個(gè)數(shù)字,以逗號(hào),分開(kāi);然后可選擇升或者降序排序;按提交鍵就在另一頁(yè)面顯示按什么排序,結(jié)果為,提供reset
import java.util.*;
public class bycomma{
public static String[] splitStringByComma(String source){
if(source==null||source.trim().equals(""))
return null;
StringTokenizer commaToker = newStringTokenizer(source,",");
String[] result = newString[commaToker.countTokens()];
int i=0;
while(commaToker.hasMoreTokens()){
result[i] =commaToker.nextToken();
i++;
}
return result;
}
public static void main(String args[]){
String[]s = splitStringByComma("5,8,7,4,3,9,1");
int[]ii = new int[s.length];
for(inti = 0;i ii[i]=Integer.parseInt(s[i]); } Arrays.sort(ii); //asc for(int i=0;i System.out.println(ii[i]); } //desc for(inti=(s.length-1);i>=0;i--){ System.out.println(ii[i]); } } } 2.金額轉(zhuǎn)換,阿拉伯?dāng)?shù)字的金額轉(zhuǎn)換成中國(guó)傳統(tǒng)的形式如:(¥1011)->(一千零一拾一元整)輸出。 package test.format; import java.text.NumberFormat; import java.util.HashMap; public class SimpleMoneyFormat { public static final String EMPTY = ""; public static final String ZERO = "零"; public static final String ONE = "壹"; public static final String TWO = "貳"; public static final String THREE = "叁"; public static final String FOUR = "肆"; public static final String FIVE = "伍"; public static final String SIX = "陸"; public static final String SEVEN = "柒"; public static final String EIGHT = "捌"; public static final String NINE = "玖"; public static final String TEN = "拾"; public static final String HUNDRED = "佰"; public static final String THOUSAND = "仟"; public static final String TEN_THOUSAND = "萬(wàn)"; public static final String HUNDRED_MILLION = "億"; public static final String YUAN = "元"; public static final String JIAO = "角"; public static final String FEN = "分"; public static final String DOT = "."; private static SimpleMoneyFormat formatter = null; private HashMap chineseNumberMap = new HashMap(); private HashMap chineseMoneyPattern = new HashMap(); private NumberFormat numberFormat = NumberFormat.getInstance(); private SimpleMoneyFormat() { numberFormat.setMaximumFractionDigits(4); numberFormat.setMinimumFractionDigits(2); numberFormat.setGroupingUsed(false); chineseNumberMap.put("0", ZERO); chineseNumberMap.put("1", ONE); chineseNumberMap.put("2", TWO); chineseNumberMap.put("3", THREE); chineseNumberMap.put("4", FOUR); chineseNumberMap.put("5", FIVE); chineseNumberMap.put("6", SIX); chineseNumberMap.put("7", SEVEN); chineseNumberMap.put("8", EIGHT); chineseNumberMap.put("9", NINE); chineseNumberMap.put(DOT, DOT); chineseMoneyPattern.put("1", TEN); chineseMoneyPattern.put("2", HUNDRED); chineseMoneyPattern.put("3", THOUSAND); chineseMoneyPattern.put("4", TEN_THOUSAND); chineseMoneyPattern.put("5", TEN); chineseMoneyPattern.put("6", HUNDRED); chineseMoneyPattern.put("7", THOUSAND); chineseMoneyPattern.put("8", HUNDRED_MILLION); } public static SimpleMoneyFormat getInstance() { if (formatter == null) formatter = new SimpleMoneyFormat(); return formatter; } public String format(String moneyStr) { checkPrecision(moneyStr); String result; result = convertToChineseNumber(moneyStr); result = addUnitsToChineseMoneyString(result); return result; } public String format(double moneyDouble) { return format(numberFormat.format(moneyDouble)); } public String format(int moneyInt) { return format(numberFormat.format(moneyInt)); } publicString format(long moneyLong) { return format(numberFormat.format(moneyLong)); } public String format(Number moneyNum) { return format(numberFormat.format(moneyNum)); } private String convertToChineseNumber(String moneyStr) { String result; StringBuffer cMoneyStringBuffer = new StringBuffer(); for (int i = 0; i < moneyStr.length(); i++) { cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(i, i +1))); } //拾佰仟萬(wàn)億等都是漢字里面才有的單位,加上它們 int indexOfDot = cMoneyStringBuffer.indexOf(DOT); int moneyPatternCursor = 1; for (int i = indexOfDot - 1; i > 0; i--) { cMoneyStringBuffer.(i, chineseMoneyPattern.get(EMPTY +moneyPatternCursor)); moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor +1; } String fractionPart =cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf(".")); cMoneyStringBuffer.(cMoneyStringBuffer.indexOf("."),cMoneyStringBuffer.length()); while (cMoneyStringBuffer.indexOf("零拾") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"),cMoneyStringBuffer.indexOf("零拾") + 2, ZERO); } while (cMoneyStringBuffer.indexOf("零佰") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零佰"),cMoneyStringBuffer.indexOf("零佰") + 2, ZERO); } while (cMoneyStringBuffer.indexOf("零仟") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零仟"),cMoneyStringBuffer.indexOf("零仟") + 2, ZERO); } while (cMoneyStringBuffer.indexOf("零萬(wàn)") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零萬(wàn)"),cMoneyStringBuffer.indexOf("零萬(wàn)") + 2, TEN_THOUSAND); } while (cMoneyStringBuffer.indexOf("零億") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零億"),cMoneyStringBuffer.indexOf("零億") + 2, HUNDRED_MILLION); } while (cMoneyStringBuffer.indexOf("零零") != -1) { cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零零"),cMoneyStringBuffer.indexOf("零零") + 2, ZERO); } if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length()- 1) cMoneyStringBuffer.(cMoneyStringBuffer.length() -1,cMoneyStringBuffer.length()); cMoneyStringBuffer.append(fractionPart); result = cMoneyStringBuffer.toString(); return result; } private String addUnitsToChineseMoneyString(String moneyStr) { String result; StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr); int indexOfDot = cMoneyStringBuffer.indexOf(DOT); cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN); } } ==================================== 6. class Something { int i; public void doSomething() { System.out.println("i = " + i); } } 有什么錯(cuò)呢? 看不出來(lái)啊。 答案: 正確。輸出的是"i = 0"。int i屬於instant variable (實(shí)例變量,或叫成員變量)。 instant variable有default value。int的default value是0。 7. class Something { final int i; public void doSomething() { System.out.println("i = " + i); } } 和上面一題只有一個(gè)地方不同,就是多了一個(gè)final。這難道就錯(cuò)了嗎? 答案: 錯(cuò)。final int i是個(gè)final的instant variable (實(shí)例變量,或叫成員變量)。 final的instant variable沒(méi)有default value,必須在constructor (構(gòu)造器)結(jié)束之前被賦予一個(gè)明確的值。 可以修改為"final int i =0;"。 8. public class Something { public static void main(String[] args) { Something s = new Something(); System.out.println("s.doSomething() returns " +doSomething()); } public String doSomething() { return "Do something ..."; } } 看上去很完美。 答案: 錯(cuò)?瓷先ピ趍ain里call doSomething沒(méi)有什么問(wèn)題,畢竟兩個(gè)methods都在同一個(gè)class里。 但仔細(xì)看,main是static的。static method不能直接call non-static methods。 可改成"System.out.println("s.doSomething()returns " + s.doSomething());"。 同理,static method不能訪問(wèn)non-static instant variable。//這句話不知道什么意思,linwf@20110124 9. 此處,Something類的文件名叫OtherThing.java class Something { private static void main(String[] something_to_do) { System.out.println("Do something ..."); } } 這個(gè)好像很明顯。 答案: 正確。從來(lái)沒(méi)有人說(shuō)過(guò)Java的Class名字必須和其文件名相同。但public class的名字必須和文件名相同。 10. interfaceA{ int x = 0; } class B{ int x =1; } class C extends B implements A { public void pX(){ System.out.println(x); } public static void main(String[] args) { new C().pX(); } } 答案:錯(cuò)誤。在編譯時(shí)會(huì)發(fā)生錯(cuò)誤 錯(cuò)誤描述:不同的JVM有不同的信息,意思就是未明確的x調(diào)用,兩個(gè)x都匹配 。ň拖笤谕瑫r(shí)import java.util和java.sql兩個(gè)包時(shí)直接聲明Date一樣)。 對(duì)于父類的變量,可以用super.x來(lái)明確,而接口的屬性默認(rèn)隱含為public static final.所以可以通過(guò)A.x來(lái)明確。 11. interface Playable { void play(); } interface Bounceable { void play(); } interface Rollable extends Playable,Bounceable { Ball ball = new Ball("PingPang"); } class Ball implements Rollable { private String name; public String getName() { return name; } public Ball(String name) { this.name = name; } public void play() { ball = new Ball("Football"); System.out.println(ball.getName()); } } 這個(gè)錯(cuò)誤不容易發(fā)現(xiàn)。 答案: 錯(cuò)。"interface Rollable extends Playable, Bounceable"沒(méi)有問(wèn)題。 interface可繼承多個(gè)interfaces,所以這里沒(méi)錯(cuò)。問(wèn)題出在interfaceRollable里的"Ball ball = newBall("PingPang");"。 任何在interface里聲明的interface variable(接口變量,也可稱成員變量),默認(rèn)為public static final。 也就是說(shuō)"Ball ball =new Ball("PingPang");"實(shí)際上是"public static final Ball ball = newBall("PingPang");"。 在Ball類的Play()方法中,"ball = newBall("Football");"改變了ball的reference, 而這里的ball來(lái)自Rollable interface,Rollableinterface里的ball是public static final的,final的object是不能被改變r(jià)eference的。 因此編譯器將在"ball = newBall("Football");"這里顯示有錯(cuò)。 【J2EE經(jīng)典面試試題及答案】相關(guān)文章: 電子技術(shù)面試試題及答案04-03 2023年電工面試題及答案02-07 質(zhì)量工程師面試試題及答案06-16 2017年中醫(yī)面試試題及答案02-26 photoshop試題「附答案」01-09 管理溝通試題及答案02-08 excel試題2023「附答案」09-12 情商測(cè)試題及答案02-03 iq測(cè)試題及答案08-07