1. 程式人生 > >程式設計師面試中遇到問題

程式設計師面試中遇到問題

 面試時候經常會問的一些問題(不斷補充中)

面試必備基礎題目(雖然不一定常用, 僅用於面試, 面試就是把人搞的都不會然後砍價, 當然您可以講我可以查資料完成, 但是面試的時候就是沒道理的, 起碼我是經常看到這些題).

如何把一段逗號分割的字串轉換成一個數組?
request.getAttribute() 和 request.getParameter() 有何區別?
response.sendRedirect() 和 forward() 區別?
<%@include file="xxx.jsp"%>
和 <jsp:include> 區別?
List 和 Map 區別?
Struts 和 Spring 自動填充表單引數到 Bean 的大致原理?
說一下你用的 Spring + Hibernate 的方框圖? 您都用到了哪些部分? 

請用英文簡單介紹一下自己.

Apache Tomcat is the servlet container that is used in the official Reference Implementation for the Java Servlet and JavaServer Pages technologies. The Java Servlet and JavaServer Pages specifications are developed by Sun under the Java Community Process

Apache Tomcat is developed in an open and participatory environment and released under the 

Apache Software License. Apache Tomcat is intended to be a collaboration of the best-of-breed developers from around the world. We invite you to participate in this open development project. To learn more about getting involved, click here

Apache Tomcat powers numerous large-scale, mission-critical web applications across a diverse range of industries and organizations. Some of these users and their stories are listed on the 

PoweredBy wiki page.

try {
Connection conn = ...;
Statement stmt = ...;

ResultSet rs = stmt.executeQuery("select * from table1");

while(rs.next()) {

}
} catch(Exception ex) {

這段程式碼有什麼不足之處? 

HTML 的 form 提交之前如何驗證數值不為空? 為空的話提示使用者並終止提交? 

為什麼要用 ORM?  和 JDBC 有何不一樣? 

Weblogic 的 Server, Machine, Node, Domain 都有何區別? 

簡要講一下 EJB 的 7 個 Transaction Level? 

最常見的: 用 JDBC 查詢學生成績單, 把主要程式碼寫出來. 

Tomcat 5 用 query.jsp?name=中文, 怎麼把這個變數無亂碼的取出來? 

前幾天有一個朋友去面試,被問到這樣一個問題,如下 

 1 public class  smallT
 2 {
 3 public static void  main(String args[])
 4 {
 5         smallT t  = new  smallT();
 6 int  b  =  t.get();
 7         System.out.println(b);
 8     }
 9 public int  get()
10 {
11 try
12 {
13 return 2 ;
14         }
15 catch (Exception e)
16 {
17 return 3 ;
18         }
19 finally
20 {
21 return 4 ;
22         }
23     }
24 }
25 

輸出:
4
我記得學習的時候說有返回值的方法,執行完return語句後就會停止了;另一方面我還記得在try……catch語句中finally語句也是一定要執行的。於是在這裡就產生了麻煩。試了一下,發現果然是執行了finally中的值。正好這兩天公司在培訓,我便把這個問題提給java很厲害的一個講師,他用斷點測試了一下,說是兩個值都返回了……
我又改了一小下,在get方法中逐個添上了輸出資訊到控制檯的語句,發現居然能執行。如下 

 1public class test
 2{
 3 public static void main(String args[])
 4{
 5        test t = new test();
 6 int b = t.get();
 7        System.out.println("Third: a = "+b);
 8    }
 9 public int get()
10{
11 try
12{
13         System.out.println("First: a = "+2);
14 return 2;
15        }
16 catch(Exception e)
17{
18         System.out.println(3);
19        }
20 finally
21{
22            System.out.println("Second: a = "+4);
23 return 4;
24        }
25    }
26}
27

輸出:
First: a = 2
Second: a = 4
Third: a = 4
可這明明是一個需要返回一個int整數的方法啊。 

一個整數,大於0,不用迴圈和本地變數,按照n,2n,4n,8n的順序遞增,當值大於5000時,把值按照指定順序輸出來。
例:n=1237
則輸出為:
1237,
2474,
4948,
9896,
9896,
4948,
2474,
1237, 

1. Talk about overriding, overloading.
2. Talk about JAVA design patterns you known.
3. Talk about the difference between LinkList, ArrayList and Victor.
4. Talk about the difference between an Abstract class and an Interface.
5. Class a = new Class(); Class b = new Class();
 if(a == b) returns true or false, why?
6. Why we use StringBuffer when concatenating strings?
7. Try to explain Singleton to us? Is it thread safe? If no, how to make it thread safe?
8. Try to explain Ioc?
9. How to set many-to-many relationship in Hibernate?
10. Talk about the difference between INNER JOIN and LFET JOIN.
11. Why we use index in database? How many indexes is the maximum in one table as your suggestion?
12. When ‘Final’ is used in class, method and property, what dose it mean?
13. Do you have any experience on XML? Talk about any XML tool you used ,e.g. JAXB, JAXG.
14. Do you have any experience on Linux?
15. In OOD what is the reason when you create a Sequence diagram? 

補一個內部培訓用的 PPT:  SQL Tuning in Sybase.zip 17KB, 英文, Sybase 調優, 大部分道理是通用的.

資料庫三正規化是什麼? 別看問題簡單, 我也經常答不上來被面試的人寫上資料庫不行.

還有就是 Sun Java 認證時候會考的一些英文選擇題, 填空題, 模擬題也成.

也是上個星期五的上午,按照與獵頭的約定,接受了香港某軟體供應商的面試。工作是分析程式設計師,需赴港工作,以下是面試過程以及題目(大概記下了90%),記下來與大家分享。
第一部分:例行公事的英文自我介紹;
以下部分必須以粵語回答,本人非廣東人,粵語會講,但是不標準。
第二部分:專案經驗介紹,著重介紹專案背景,開發流程以及本人在專案開發過程中的角色;
第三部分:面試官根據簡歷提問:
(1)說出Abstract class與interface的不同?
(2)Oracle中如何進行錯誤處理?如果使用者反應速度慢,你如何著手解決問題?
(3)圖畫板上列了兩個table,問查詢結果,主要是考inner join與left join的。
(4)union和union all有什麼不同?
(5)你用什麼軟體做設計?
(6)是否用過Websphere?
(7)iBatis與Hibernate有什麼不同?
(8)談談Struts中的Action servlet。
(9)是否開發過IBM portal專案。
(10)是否介意加班?
(11)如果你去香港工作,你認為你最大的困難是什麼?
第四部分:筆試,三個英文考試題目選一個作答,內容都是寫一份email。
出來後問了獵頭,他說最遲一個星期內就有結果。
結果,晚上就接到電話說通過了,並收到了合同和赴港申請的電子檔案。工作地點是九龍,月薪18K(中等偏低),合同期一年。獵頭催我儘快籤合同,然後他們馬上就去辦工作簽證。
猶豫ing。

對於這個系列裡的問題,每個學Java的人都應該搞懂。當然,如果只是學Java玩玩就無所謂了。如果你認為自己已經超越初學者了,卻不很懂這些問題,請將你自己重歸初學者行列。內容均來自於CSDN的經典老貼。
問題一:我聲明瞭什麼!
String s = "Hello world!";
許多人都做過這樣的事情,但是,我們到底聲明瞭什麼?回答通常是:一個String,內容是“Hello world!”。這樣模糊的回答通常是概念不清的根源。如果要準確的回答,一半的人大概會回答錯誤。
這個語句宣告的是一個指向物件的引用,名為“s”,可以指向型別為String的任何物件,目前指向"Hello world!"這個String型別的物件。這就是真正發生的事情。我們並沒有宣告一個String物件,我們只是聲明瞭一個只能指向String物件的引用變數。所以,如果在剛才那句語句後面,如果再執行一句:
String string = s;
我們是聲明瞭另外一個只能指向String物件的引用,名為string,並沒有第二個物件產生,string還是指向原來那個物件,也就是,和s指向同一個物件。
問題二:"=="和equals方法究竟有什麼區別?
==操作符專門用來比較變數的值是否相等。比較好理解的一點是:
int a=10;
int b=10;
則a==b將是true。
但不好理解的地方是:
String a=new String("foo");
String b=new String("foo");
則a==b將返回false。
根據前一帖說過,物件變數其實是一個引用,它們的值是指向物件所在的記憶體地址,而不是物件本身。a和b都使用了new操作符,意味著將在記憶體中產生兩個內容為"foo"的字串,既然是“兩個”,它們自然位於不同的記憶體地址。a和b的值其實是兩個不同的記憶體地址的值,所以使用"=="操作符,結果會是 false。誠然,a和b所指的物件,它們的內容都是"foo",應該是“相等”,但是==操作符並不涉及到物件內容的比較。
物件內容的比較,正是equals方法做的事。
看一下Object物件的equals方法是如何實現的:
boolean equals(Object o){
return this==o;
}
Object 物件預設使用了==操作符。所以如果你自創的類沒有覆蓋equals方法,那你的類使用equals和使用==會得到同樣的結果。同樣也可以看出, Object的equals方法沒有達到equals方法應該達到的目標:比較兩個物件內容是否相等。因為答案應該由類的建立者決定,所以Object把這個任務留給了類的建立者。
看一下一個極端的類:
Class Monster{
private String content;
...
boolean equals(Object another){ return true;}
}
我覆蓋了equals方法。這個實現會導致無論Monster例項內容如何,它們之間的比較永遠返回true。
所以當你是用equals方法判斷物件的內容是否相等,請不要想當然。因為可能你認為相等,而這個類的作者不這樣認為,而類的equals方法的實現是由他掌握的。如果你需要使用equals方法,或者使用任何基於雜湊碼的集合(HashSet,HashMap,HashTable),請察看一下 java doc以確認這個類的equals邏輯是如何實現的。
問題三:String到底變了沒有?
沒有。因為String被設計成不可變(immutable)類,所以它的所有物件都是不可變物件。請看下列程式碼:
String s = "Hello";
s = s + " world!";
s 所指向的物件是否改變了呢?從本系列第一篇的結論很容易匯出這個結論。我們來看看發生了什麼事情。在這段程式碼中,s原先指向一個String物件,內容是 "Hello",然後我們對s進行了+操作,那麼s所指向的那個物件是否發生了改變呢?答案是沒有。這時,s不指向原來那個物件了,而指向了另一個 String物件,內容為"Hello world!",原來那個物件還存在於記憶體之中,只是s這個引用變數不再指向它了。
通過上面的說明,我們很容易匯出另一個結論,如果經常對字串進行各種各樣的修改,或者說,不可預見的修改,那麼使用String來代表字串的話會引起很大的記憶體開銷。因為 String物件建立之後不能再改變,所以對於每一個不同的字串,都需要一個String物件來表示。這時,應該考慮使用StringBuffer類,它允許修改,而不是每個不同的字串都要生成一個新的物件。並且,這兩種類的物件轉換十分容易。
同時,我們還可以知道,如果要使用內容相同的字串,不必每次都new一個String。例如我們要在構造器中對一個名叫s的String引用變數進行初始化,把它設定為初始值,應當這樣做:
public class Demo {
private String s;
...
public Demo {
s = "Initial Value";
}
...
}
而非
s = new String("Initial Value");
後者每次都會呼叫構造器,生成新物件,效能低下且記憶體開銷大,並且沒有意義,因為String物件不可改變,所以對於內容相同的字串,只要一個String物件來表示就可以了。也就說,多次呼叫上面的構造器建立多個物件,他們的String型別屬性s都指向同一個物件。
上面的結論還基於這樣一個事實:對於字串常量,如果內容相同,Java認為它們代表同一個String物件。而用關鍵字new呼叫構造器,總是會建立一個新的物件,無論內容是否相同。
至於為什麼要把String類設計成不可變類,是它的用途決定的。其實不只String,很多Java標準類庫中的類都是不可變的。在開發一個系統的時候,我們有時候也需要設計不可變類,來傳遞一組相關的值,這也是面向物件思想的體現。不可變類有一些優點,比如因為它的物件是隻讀的,所以多執行緒併發訪問也不會有任何問題。當然也有一些缺點,比如每個不同的狀態都要一個物件來代表,可能會造成效能上的問題。所以Java標準類庫還提供了一個可變版本,即 StringBuffer。
問題四:final關鍵字到底修飾了什麼?
final使得被修飾的變數"不變",但是由於物件型變數的本質是“引用”,使得“不變”也有了兩種含義:引用本身的不變,和引用指向的物件不變。
引用本身的不變:
final StringBuffer a=new StringBuffer("immutable");
final StringBuffer b=new StringBuffer("not immutable");
a=b;//編譯期錯誤
引用指向的物件不變:
final StringBuffer a=new StringBuffer("immutable");
a.append(" broken!"); //編譯通過
可見,final只對引用的“值”(也即它所指向的那個物件的記憶體地址)有效,它迫使引用只能指向初始指向的那個物件,改變它的指向會導致編譯期錯誤。至於它所指向的物件的變化,final是不負責的。這很類似==操作符:==操作符只負責引用的“值”相等,至於這個地址所指向的物件內容是否相等,==操作符是不管的。
理解final問題有很重要的含義。許多程式漏洞都基於此----final只能保證引用永遠指向固定物件,不能保證那個物件的狀態不變。在多執行緒的操作中,一個物件會被多個執行緒共享或修改,一個執行緒對物件無意識的修改可能會導致另一個使用此物件的執行緒崩潰。一個錯誤的解決方法就是在此物件新建的時候把它宣告為final,意圖使得它“永遠不變”。其實那是徒勞的。
問題五:到底要怎麼樣初始化!
本問題討論變數的初始化,所以先來看一下Java中有哪些種類的變數。
1. 類的屬性,或者叫值域
2. 方法裡的區域性變數
3. 方法的引數
對於第一種變數,Java虛擬機器會自動進行初始化。如果給出了初始值,則初始化為該初始值。如果沒有給出,則把它初始化為該型別變數的預設初始值。
int型別變數預設初始值為0
float型別變數預設初始值為0.0f
double型別變數預設初始值為0.0
boolean型別變數預設初始值為false
char型別變數預設初始值為0(ASCII碼)
long型別變數預設初始值為0
所有物件引用型別變數預設初始值為null,即不指向任何物件。注意陣列本身也是物件,所以沒有初始化的陣列引用在自動初始化後其值也是null。
對於兩種不同的類屬性,static屬性與instance屬性,初始化的時機是不同的。instance屬性在建立例項的時候初始化,static屬性在類載入,也就是第一次用到這個類的時候初始化,對於後來的例項的建立,不再次進行初始化。這個問題會在以後的系列中進行詳細討論。
對於第二種變數,必須明確地進行初始化。如果再沒有初始化之前就試圖使用它,編譯器會抗議。如果初始化的語句在try塊中或if塊中,也必須要讓它在第一次使用前一定能夠得到賦值。也就是說,把初始化語句放在只有if塊的條件判斷語句中編譯器也會抗議,因為執行的時候可能不符合if後面的判斷條件,如此一來初始化語句就不會被執行了,這就違反了區域性變數使用前必須初始化的規定。但如果在else塊中也有初始化語句,就可以通過編譯,因為無論如何,總有至少一條初始化語句會被執行,不會發生使用前未被初始化的事情。對於try-catch也是一樣,如果只有在try塊裡才有初始化語句,編譯部通過。如果在 catch或finally裡也有,則可以通過編譯。總之,要保證區域性變數在使用之前一定被初始化了。所以,一個好的做法是在宣告他們的時候就初始化他們,如果不知道要出事化成什麼值好,就用上面的預設值吧!
其實第三種變數和第二種本質上是一樣的,都是方法中的區域性變數。只不過作為引數,肯定是被初始化過的,傳入的值就是初始值,所以不需要初始化。
問題六:instanceof是什麼東東?
instanceof是Java的一個二元操作符,和==,>,<是同一類東東。由於它是由字母組成的,所以也是Java的保留關鍵字。它的作用是測試它左邊的物件是否是它右邊的類的例項,返回boolean型別的資料。舉個例子:
String s = "I AM an Object!";
boolean isObject = s instanceof Object;
我們聲明瞭一個String物件引用,指向一個String物件,然後用instancof來測試它所指向的物件是否是Object類的一個例項,顯然,這是真的,所以返回true,也就是isObject的值為True。
instanceof有一些用處。比如我們寫了一個處理賬單的系統,其中有這樣三個類:
public class Bill {//省略細節}
public class PhoneBill extends Bill {//省略細節}
public class GasBill extends Bill {//省略細節}
在處理程式裡有一個方法,接受一個Bill型別的物件,計算金額。假設兩種賬單計算方法不同,而傳入的Bill物件可能是兩種中的任何一種,所以要用instanceof來判斷:
public double calculate(Bill bill) {
if (bill instanceof PhoneBill) {
//計算電話賬單
}
if (bill instanceof GasBill) {
//計算燃氣賬單
}
...
}
這樣就可以用一個方法處理兩種子類。
然而,這種做法通常被認為是沒有好好利用面向物件中的多型性。其實上面的功能要求用方法過載完全可以實現,這是面向物件變成應有的做法,避免回到結構化程式設計模式。只要提供兩個名字和返回值都相同,接受引數型別不同的方法就可以了:
public double calculate(PhoneBill bill) {
//計算電話賬單
}
public double calculate(GasBill bill) {
//計算燃氣賬單
}
所以,使用instanceof在絕大多數情況下並不是推薦的做法,應當好好利用多型。

BeanSoft 參加過的一次面試:

2004年8月18日 星期三 〖農曆 甲申 猴年 七月初三〗 

筆試題目
1. 寫出常用的 Linux 命令
a) 列出當前目錄
b) 列出所有系統變數
c) 重新命名檔案
d) etc... 記不住了. 

2. Write a Java application use the Singleton pattern, it should have one instance per class. It should have two integer variable, one is count, on is inita. There should be two methods in this class, and the method should be thread safely in multi-thread environment.
a) a count() method, after each call the count should be added on by 1;
b) a reset() method, after each call the count should be set to the value of inita. 

3. Write a JavaBean and a JSP file. The page should output a date string in this pattern "今天是2004年8月15日上午10:00", the value is taken from the bean. 

4. 以下三個題目, 任選其一或多個:
1) 忘了..., 是關於 Java 的.
2) 寫一個應用程式, 讀出 STUDENT 表中的資料並打印出所有名稱. 資料庫系統任選.
3) 寫一個 Servlet, 讀取名為 url 的引數, 並連線到此字串指定的地址上, 讀取所有內容後顯示給客戶. 

5. 6. 7. 都是關於 SQL 的, 例如 SELECT, UPDATE, DELETE 之類的, 還有的有子查詢. 資料庫系統沒有限制. 

8. Write a html file, it shoud has follow functions:
1) check whether the user's name is empty;
2) the email address should has a '@';
3) telephone number must be '1'-'9', '-', ' '(space).
Page picture:
Please input your name:
[____________________] [Check input]
Please input your address:
[[email protected]________________] [Check input]
Please input your telephone number:
[____123a____________] [Check input] 

Java 面試題及其答案 

前段時間因為要參加一個筆試,在準備期間在網上找到了兩條關於筆試題目的文章,其中一篇為<<有感:應聘Java筆試時可能出現問題>>,還有一篇忘了名字,讀後深受啟發。 
在尋找這些答案的過程中,我將相關答案記錄下來,就形成了以下這些東西。需要說明的是以下答案肯定有很多不完整甚至錯誤的地方,需要各位來更正與完善它,千萬不要扔我的雞蛋啊。 
希望本文能夠給即將奔赴筆試考場的同仁些許幫助,更希望更多的人加入到收集整理筆試題與完善答案的這些工作中來,為大家更好的獲得工作機會做一點貢獻。 
在此感謝前面兩文的作者的對筆試題目的收集與整理。 
如有任何意見與建議請通過QQ:6045306,Mail:[email protected]與我聯絡。 
Java基礎方面: 

1、作用域public,private,protected,以及不寫時的區別 
答:區別如下: 
作用域 當前類 同一package 子孫類 其他package 
public √ √ √ √ 
protected √ √ √ × 
friendly √ √ × × 
private √ × × × 
不寫時預設為friendly 

2、ArrayList和Vector的區別,HashMap和Hashtable的區別 
答:就ArrayList與Vector主要從二方面來說. 
一.同步性:Vector是執行緒安全的,也就是說是同步的,而ArrayList是執行緒序不安全的,不是同步的 
二.資料增長:當需要增長時,Vector預設增長為原來一培,而ArrayList卻是原來的一半 
就HashMap與HashTable主要從三方面來說。 
一.歷史原因:Hashtable是基於陳舊的Dictionary類的,HashMap是Java 1.2引進的Map介面的一個實現 
二.同步性:Hashtable是執行緒安全的,也就是說是同步的,而HashMap是執行緒序不安全的,不是同步的 
三.值:只有HashMap可以讓你將空值作為一個表的條目的key或value 

3、char型變數中能不能存貯一箇中文漢字?為什麼? 
答:是能夠定義成為一箇中文的,因為java中以unicode編碼,一個char佔16個位元組,所以放一箇中文是沒問題的 

4、多執行緒有幾種實現方法,都是什麼?同步有幾種實現方法,都是什麼? 
答:多執行緒有兩種實現方法,分別是繼承Thread類與實現Runnable介面 
同步的實現方面有兩種,分別是synchronized,wait與notify 

5、繼承時候類的執行順序問題,一般都是選擇題,問你將會打印出什麼? 
答:父類: 
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(); 


輸出結果: 
C:\>java test.ChildClass 
FatherClass Create 
FatherClass Create 
ChildClass Create 

6、內部類的實現方式? 
答:示例程式碼如下: 
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(); 


輸出結果: 
C:\>java test/OuterClass 
InterClass Create 
OuterClass Create 
再一個例題: 
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 

7、垃圾回收機制,如何優化程式? 
希望大家補上,謝謝 

8、float型float f=3.4是否正確? 
答:不正確。精度不準確,應該用強制型別轉換,如下所示:float f=(float)3.4 

9、介紹JAVA中的Collection FrameWork(包括如何寫自己的資料結構)? 
答:Collection FrameWork如下: 
Collection 
├List 
│├LinkedList 
│├ArrayList 
│└Vector 
│ └Stack 
└Set 
Map 
├Hashtable 
├HashMap 
└WeakHashMap 
Collection是最基本的集合介面,一個Collection代表一組Object,即Collection的元素(Elements) 
Map提供key到value的對映 

10、Java中異常處理機制,事件機制? 

11、JAVA中的多形與繼承? 
希望大家補上,謝謝 

12、抽象類與介面? 
答:抽象類與介面都用於抽象,但是抽象類(JAVA中)可以有自己的部分實現,而介面則完全是一個標識(同時有多重繼承的功能)。 

13、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 = ":"+socket.getLocalPort(); 
System.out.println("A client come in!IP:"+RemoteIP+RemotePort); 
in = new BufferedReader(new 

InputStreamReader(socket.getInputStream())); 
String line = in.readLine(); 
System.out.println("Cleint send is :" + line); 
out = new 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 java.net.*; 

public class Client 

Socket socket; 
BufferedReader in; 
PrintWriter out; 
public Client() 

try 

System.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(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("Wrong"); 


public static void main(String[] args) 

new Client(); 

}; 

14、用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("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(); 


JAVA類實現序例化的方法是實現java.io.Serializable介面 
Collection框架中實現比較要實現Comparable 介面和 Comparator 介面 

15、程式設計:編寫一個擷取字串的函式,輸入為一個字串和位元組數,輸出為按位元組擷取的字串。 但是要保證漢字不被截半個,如“我ABC”4,應該截為“我AB”,輸入“我ABC漢DEF”,6,應該輸出為“我ABC”而不是“我ABC+漢的半個”。 
答:程式碼如下: 
package test; 

class SplitString 

String 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() 

int loopCount; 

loopCount=(SplitStr.length()%SplitByte==0)?(SplitStr.length()/SplitByte):(SplitStr.length()/Split 

Byte+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.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(); 

16、JAVA多執行緒程式設計。 用JAVA寫一個多執行緒程式,如寫四個執行緒,二個加1,二個對一個變數減一,輸出。 
希望大家補上,謝謝 

17、STRING與STRINGBUFFER的區別。 
答:STRING的長度是不可變的,STRINGBUFFER的長度是可變的。如果你對字串中的內容經常進行操作,特別是內容要修改時,那麼使用StringBuffer,如果最後需要String,那麼使用StringBuffer的toString()方法 

Jsp方面 

1、jsp有哪些內建物件?作用分別是什麼? 
答:JSP共有以下9種基本內建元件(可與ASP的6種內部元件相對應): 
 request 使用者端請求,此請求會包含來自GET/POST請求的引數 
response 網頁傳回使用者端的迴應 
pageContext 網頁的屬性是在這裡管理 
session 與請求有關的會話期 
application servlet 正在執行的內容 
out 用來傳送回應的輸出 
config servlet的構架部件 
page JSP網頁本身 
exception 針對錯誤網頁,未捕捉的例外 

2、jsp有哪些動作?作用分別是什麼? 
答:JSP共有以下6種基本動作 
jsp:include:在頁面被請求的時候引入一個檔案。 
jsp:useBean:尋找或者例項化一個JavaBean。 
jsp:setProperty:設定JavaBean的屬性。 
jsp:getProperty:輸出某個JavaBean的屬性。 
jsp:forward:把請求轉到一個新的頁面。 
jsp:plugin:根據瀏覽器型別為Java外掛生成OBJECT或EMBED標記 

3、JSP中動態INCLUDE與靜態INCLUDE的區別? 
答:動態INCLUDE用jsp:include動作實現 
<jsp:include page="included.jsp" flush="true" />它總是會檢查所含檔案中的變化,適合用於包含動態頁面,並且可以帶引數 
靜態INCLUDE用include偽碼實現,定不會檢查所含檔案的變化,適用於包含靜態頁面 
<%@ include file="included.htm" %> 

4、兩種跳轉方式分別是什麼?有什麼區別? 
答:有兩種,分別為: 
<jsp:include page="included.jsp" flush="true"> 
<jsp:forward page= "nextpage.jsp"/> 
前者頁面不會轉向include所指的頁面,只是顯示該頁的結果,主頁面還是原來的頁面。執行完後還會回來,相當於函式呼叫。並且可以帶引數.後者完全轉向新頁面,不會再回來。相當於go to 語句。 

Servlet方面 

1、說一說Servlet的生命週期? 
答:servlet有良好的生存期的定義,包括載入和例項化、初始化、處理請求以及服務結束。這個生存期由javax.servlet.Servlet介面的init,service和destroy方法表達。 

2、Servlet版本間(忘了問的是哪兩個版本了)的不同? 
希望大家補上,謝謝 

3、JAVA SERVLET API中forward() 與redirect()的區別? 
答:前者僅是容器中控制權的轉向,在客戶端瀏覽器位址列中不會顯示出轉向後的地址;後者則是完全的跳轉,瀏覽器將會得到跳轉的地址,並重新發送請求連結。這樣,從瀏覽器的位址列中可以看到跳轉後的連結地址。所以,前者更加高效,在前者可以滿足需要時,儘量使用forward()方法,並且,這樣也有助於隱藏實際的連結。在有些情況下,比如,需要跳轉到一個其它伺服器上的資源,則必須使用sendRedirect()方法。 

4、Servlet的基本架構 
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 { 

Jdbc、Jdo方面 

1、可能會讓你寫一段Jdbc連Oracle的程式,並實現資料查詢. 
答:程式如下: 
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(); 


2、Class.forName的作用?為什麼要用? 
答:呼叫該訪問返回一個以字串指定類名的類的物件。 

3、Jdo是什麼? 
答:JDO是Java物件持久化的新的規範,為java data object的簡稱,也是一個用於存取某種資料倉庫中的物件的標準化API。JDO提供了透明的物件儲存,因此對開發人員來說,儲存資料物件完全不需要額外的程式碼(如JDBC API的使用)。這些繁瑣的例行工作已經轉移到JDO產品提供商身上,使開發人員解脫出來,從而集中時間和精力在業務邏輯上。另外,JDO很靈活,因為它可以在任何資料底層上執行。JDBC只是面向關係資料庫(RDBMS)JDO更通用,提供到任何資料底層的儲存功能,比如關係資料庫、檔案、XML以及物件資料庫(ODBMS)等等,使得應用可移植性更強。 

4、在ORACLE大資料量下的分頁解決方法。一般用擷取ID方法,還有是三層巢狀方法。 
答:一種分頁方法 
<% 
int i=1; 
int numPages=14; 
String pages = request.getParameter("page") ; 
int currentPage = 1; 
currentPage=(pages==null)?(1):{Integer.parseInt(pages)} 
sql = "select count(*) from tables" 
ResultSet rs = DBLink.executeQuery(sql) ; 
while(rs.next()) i = rs.getInt(1) ; 
int intPageCount=1; 
intPageCount=(i%numPages==0)?(i/numPages):(i/numPages+1); 
int nextPage ; 
int upPage; 
nextPage = currentPage+1; 
if (nextPage>=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<numPages*(currentPage-1))&&rs.next()){i++;} 
%> 
//輸出內容 
//輸出翻頁連線 
合計:<%=currentPage%>/<%=intPageCount%><a href="List.jsp?page=1" temp_href="List.jsp?page=1">第一頁</a><a 

href="List.jsp?page=<%=upPage%>" temp_href="List.jsp?page=<%=upPage%>">上一頁</a> 
<% 
for(int j=1;j<=intPageCount;j++){ 
if(currentPage!=j){ 
%> 
<a href="list.jsp?page=<%=j%>" temp_href="list.jsp?page=<%=j%>">[<%=j%>]</a> 
<% 
}else{ 
out.println(j); 


%> 
<a href="List.jsp?page=<%=nextPage%>" temp_href="List.jsp?page=<%=nextPage%>">下一頁</a><a href="List.jsp?page=<%=intPageCount%>" temp_href="List.jsp?page=<%=intPageCount%>">最後頁 

</a> 

Xml方面 

1、xml有哪些解析技術?區別是什麼? 
答:有DOM,SAX,STAX等 
DOM:處理大型檔案時其效能下降的非常厲害。這個問題是由DOM的樹結構所造成的,這種結構佔用的記憶體較多,而且DOM必須在解析檔案之前把整個文件裝入記憶體,適合對XML的隨機訪問SAX:不現於DOM,SAX是事件驅動型的XML解析方式。它順序讀取XML檔案,不需要一次全部裝載整個檔案。當遇到像檔案開頭,文件結束,或者標籤開頭與標籤結束時,它會觸發一個事件,使用者通過在其回撥事件中寫入處理程式碼來處理XML檔案,適合對XML的順序訪問 
STAX:Streaming API for XML (StAX) 

2、你在專案中用到了xml技術的哪些方面?如何實現的? 
答:用到了資料存貯,資訊配置兩方面。在做資料交換平臺時,將不能資料來源的資料組裝成XML檔案,然後將XML檔案壓縮打包加密後通過網路傳送給接收者,接收解密與解壓縮後再同XML檔案中還原相關資訊進行處理。在做軟體配置時,利用XML可以很方便的進行,軟體的各種配置引數都存貯在XML檔案中。 

3、用jdom解析xml檔案時如何解決中文問題?如何解析? 
答:看如下程式碼,用編碼方式加以解決 
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), 
new 

javax.xml.transform.stream.StreamResult(outFile)); 

catch (Exception e) 

System.out.println (e.getMessage()); 


4、程式設計用JAVA解析XML的方式. 
答:用SAX方式解析XML,XML檔案如下: 
<?xml version="1.0" encoding="gb2312"?> 
<person> 
<name>王小明</name> 
<college>資訊學院</college> 
<telephone>6258113</telephone> 
<notes>男,1955年生,博士,95年調入海南大學</notes> 
</person> 
事件回撥類SAXHandler.java 
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); 


JSP內容顯示原始碼,SaxXml.jsp: 
<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> 

EJB方面 

1、EJB2.0有哪些內容?分別用在什麼場合? EJB2.0和EJB1.1的區別? 
答:規範內容包括Bean提供者,應用程式裝配者,EJB容器,EJB配置工具,EJB服務提供者,系統管理員。這裡面,EJB容器是EJB之所以能夠執行的核心。EJB容器管理著EJB的建立,撤消,啟用,去活,與資料庫的連線等等重要的核心工作。JSP,Servlet,EJB,JNDI,JDBC,JMS..... 

2、EJB與JAVA BEAN的區別? 
答:Java Bean 是可複用的元件,對Java Bean並沒有嚴格的規範,理論上講,任何一個Java類都可以是一個Bean。但通常情況下,由於Java Bean是被容器所建立(如Tomcat)的,所以Java Bean應具有一個無參的構造器,另外,通常Java Bean還要實現Serializable介面用於實現Bean的永續性。Java Bean實際上相當於微軟COM模型中的本地程序內COM元件,它是不能被跨程序訪問的。Enterprise Java Bean 相當於DCOM,即分散式元件。它是基於Java的遠端方法呼叫(RMI)技術的,所以EJB可以被遠端訪問(跨程序、跨計算機)。但EJB必須被佈署在諸如Webspere、WebLogic這樣的容器中,EJB客戶從不直接訪問真正的EJB元件,而是通過其容器訪問。EJB容器是EJB元件的代理,EJB元件由容器所建立和管理。客戶通過容器來訪問真正的EJB元件。 

3、EJB的基本架構 
答:一個EJB包括三個部分: 
Remote Interface 介面的程式碼 
package Beans; 
import javax.ejb.EJBObject; 
import java.rmi.RemoteException; 
public interface Add extends EJBObject 

//some method declare 

Home Interface 介面的程式碼 
package Beans; 
import java.rmi.RemoteException; 
import jaax.ejb.CreateException; 
import javax.ejb.EJBHome; 
public interface AddHome extends EJBHome 

//some method declare 

EJB類的程式碼 
package Beans; 
import java.rmi.RemoteException; 
import javax.ejb.SessionBean; 
import javx.ejb.SessionContext; 
public class AddBean Implements SessionBean 

//some method declare 

J2EE,MVC方面 

1、MVC的各個部分都有那些技術來實現?如何實現? 
答:MVC是Model-View-Controller的簡寫。"Model" 代表的是應用的業務邏輯(通過JavaBean,EJB元件實現), "View" 是應用的表示面(由JSP頁面產生),"Controller" 是提供應用的處理過程控制(一般是一個Servlet),通過這種設計模型把應用邏輯,處理過程和顯示邏輯分成不同的元件實現。這些元件可以進行互動和重用。 

2、應用伺服器與WEB SERVER的區別? 
希望大家補上,謝謝 

3、J2EE是什麼? 
答:Je22是Sun公司提出的多層(multi-diered),分散式(distributed),基於元件(component-base)的企業級應用模型(enterpriese application model).在這樣的一個應用系統中,可按照功能劃分為不同的元件,這些元件又可在不同計算機上,並且處於相應的層次(tier)中。所屬層次包括客戶層(clietn tier)元件,web層和元件,Business層和元件,企業資訊系統(EIS)層。 

4、WEB SERVICE名詞解釋。JSWDL開發包的介紹。JAXP、JAXM的解釋。SOAP、UDDI,WSDL解釋。 
答:Web Service描述語言WSDL 
SOAP即簡單物件訪問協議(Simple Object Access Protocol),它是用於交換XML編碼資訊的輕量級協議。 
UDDI 的目的是為電子商務建立標準;UDDI是一套基於Web的、分散式的、為Web Service提供的、資訊註冊中心的實現標準規範,同時也包含一組使企業能將自身提供的Web Service註冊,以使別的企業能夠發現的訪問協議的實現標準。 

5、BS與CS的聯絡與區別。 
希望大家補上,謝謝 

6、STRUTS的應用(如STRUTS架構) 
答:Struts是採用Java Servlet/JavaServer Pages技術,開發Web應用程式的開放原始碼的framework。 採用Struts能開發出基於MVC(Model-View-Controller)設計模式的應用構架。 Struts有如下的主要功能: 
一.包含一個controller servlet,能將使用者的請求傳送到相應的Action物件。 
二.JSP自由tag庫,並且在controller servlet中提供關聯支援,幫助開發員建立互動式表單應用。 
三.提供了一系列實用物件:XML處理、通過Java reflection APIs自動處理JavaBeans屬性、國際化的提示和訊息。 

設計模式方面 

1、開發中都用到了那些設計模式?用在什麼場合? 
答:每個模式都描述了一個在我們的環境中不斷出現的問題,然後描述了該問題的解決方案的核心。通過這種方式,你可以無數次地使用那些已有的解決方案,無需在重複相同的工作。主要用到了MVC的設計模式。用來開發JSP/Servlet或者J2EE的相關應用。簡單工廠模式等。 

2、UML方面 
答:標準建模語言UML。用例圖,靜態圖(包括類圖、物件圖和包圖),行為圖,互動圖(順序圖,合作圖),實現圖, 

JavaScript方面 

1、如何校驗數字型? 
var re=/^\d{1,8}$|\.\d{1,2}$/; 
var str=document.form1.all(i).value; 
var r=str.match(re); 
if (r==null) 

sign=-4; 
break; 

else{ 
document.form1.all(i).value=parseFloat(str); 

CORBA方面 

1、CORBA是什麼?用途是什麼? 
答:CORBA 標準是公共物件請求代理結構(Common Object Request Broker Architecture),由物件管理組織 (Object Management Group,縮寫為 OMG)標準化。它的組成是介面定義語言(IDL), 語言繫結(binding:也譯為聯編)和允許應用程式間互操作的協議。 其目的為: 
用不同的程式設計語言書寫 
在不同的程序中執行 
為不同的作業系統開發 

LINUX方面 

1、LINUX下執行緒,GDI類的解釋。 
答:LINUX實現的就是基於核心輕量級程序的"一對一"執行緒模型,一個執行緒實體對應一個核心輕量級程序,而執行緒之間的管理在核外函式庫中實現。 
GDI類為影象裝置程式設計介面類庫。

Hibernate: 簡述 Hibernate 和 JDBC 的優缺點? 如何書寫一個 one to many 配置檔案.

Spring 的依賴注入是什麼意思? 給一個 Bean 的 message 屬性, 字串型別, 注入值為 "Hello" 的 XML 配置檔案該怎麼寫?

SCJP 模擬題200道附答案

Q1 Which of the following statements are valid, given the following variable declarations: boolean a; boolean b; int c; 
1) (a | b) 
2)(a || a) 
3)(a ^ b) | c 
4)(a & c) 
5)(a && c) 

Q2 Which of the following can be applied to constructors: 
1) final 
2) static 
3) synchronized 
4) native 
5) None of these. 

Q3 Which of the following retain their preferred size (width and height) when added (individually) to the North section of a container with a BorderLayout (assume that no other components or containers are present in the North section). 
1) TextArea 
2) Button 
3) TextField 
4) Checkbox 
5) None. All of these mentioned components will only retain their preferred height. 

Q4 Which of the following are legal names for variables. 
1) _int 
2) %large 
3) $fred 
4) Integer 
5) 2much 

Q5 Which of the following are correct ways to create a font. 
1) Font f = new Font("Serif", Font.BOLD, 24); 
2) Font f = new Font(Font.SERIF, "Bold", 24); 
3) Font f = new Font("Serif", "Bold", 24); 
4) Font f = new Font(Font.SERIF, Font.BOLD, 24); 

Q6 Select the correct statements regarding the following piece of code. 
File f = new File("c:\\large.txt"); 
1) On execution, a file called "large.txt" will be created on the local harddisk. 
2) The code fails to compile on a UNIX machine, because the directory separator is not correct. 
3) A file is NOT created on the harddisk when this code is executed. 
4) An exception is thrown at runtime if the file "large.txt" already exists. 
5) The code fails to compile, since this is not a valid constructor for the File class. 

Q7 Which of the following statements are correct regarding the RandomAccessFile class? 
1) An IOException is thrown if the specified file doesn't exist when created using the "r" mode. 
2) This class has a method which allows a file to be deleted from the harddisk. 
3) It is possible to use this class in conjunction with the DataInputStream class. 
4) When used with the "rw" mode, the specified file is created on a diskdrive, if it doesn't already exist. 
5) There are methods to read and write primatives (eg, readInt(), writeInt(), etc). 

Q8 Consider the following piece of code and select the correct statement from the following. 
1.String s = new String("abcdefgh"); 
2.s.replace('d', 'q'); 
3.System.out.println(s); 

1) The code fails to compile, reporting an error at line 2. Strings are immutable, and therefore a replace() method is meaningless. 
2) The code compiles correctly, and displays the text "abcqefgh". 
3) The code compiles correctly, and displays the text "abcdefgh". 
4) The code compiles, but an exception is thrown when line 2 is executed. 
5) The code compiles, but an exception is thrown at line 3. 

Q9 Which of the following keywords can be applied to the variables or methods of an interface. 
1) static 
2) private 
3) synchronised 
4) protected 
5) public 

Q10 True or False. 
Only Frames can contain menu bars or pull-down menus. 
1) True 
2) False. 

Q11 Consider the following piece of code and select the correct statement(s): 
1. class A{ 
2. protected int method(){ 
3. } 
4. } 
5. 
6. class B extends A{ 
7. int method(){ 
8. } 
9. } 
1) The code fails to compile, because you can't override a method to be more private than its parent. 
2) The code fails to compile, because method() is declared as protected, and is therefore not available to any subclass. 
3) The code compiles correctly, but throws a NullPointerException at runtime. 
4) The code fails to compile. However, it can be made to compile correctly by prefixing line 7 with the access qualifier "public". 
5) The code fails to compile. However, it can be made to compile correctly by prefixing line 7 with the access qualifier "protected". 

Q12 True or False. 

The Throwable class is the superclass of all exceptions in the Java language. 
1) True 
2) False 

Q13 Consider the following piece of code (assume the Graphics context g is defined correctly): 
g.setBackground(Color.red); 
g.setForeground(Color.white); 
g.drawLine(10, 10, 50, 10); 
g.setForeground(Color.blue); 
g.drawRect(100, 100, 50, 50); 

What is displayed when this code is executed. 
1) A blue line from (10,10) to (50,10) and a blue rectangle with upper left corner at (100,100). 
2) A white line from (10,10) to (50,10) and a blue square with top left corner at (100,100). 
3) A white line from (10,10) to (10,50) and a blue square with lower left corner at (100,100). 
4) A red line from (10, 10) to (50,10) and a red square with upper left corner at (100,100). 
5) Nothing is displayed. You must first issue a repaint() command. 

Q14 Consider the following piece of code. 
class Test{ 
public static void main(String [] args){ 
System.out.println(args[3]); 


When the following is typed at the command line, what is displayed: 
java Test Metallica Justice For All 
1) All 
2) For 
3) Justice 
4) Nothing. 
5) Nothing. An ArrayIndexOutOfBoundsException is thrown 

Q15 Consider the following piece of code. 
1. String s = "abcd"; 
2. Integer x = new Integer(3); 
3. String s2 = s + 4; 
4. s2 = null; 
5. s = null; 

Following the execution of which line above, is the object referenced by s2 available for garbage collection. 
1) Line 5 
2) It is not possible to say when an object is available for garbage collection. 
3) Line 4 
4) The objects are not available until the executing thread is ended. 

Q16 What is displayed when the following piece of code is compiled and executed: 
class Test{ 
public static void main(String [] args){ 
Base b = new Subclass(); 
System.out.println(b.x); 
System.out.println(b.method()); 

class Base{ 
int x = 2; 
int method(){ 
return x; 

class Subclass extends Base{ 
int x = 3; 
int method(){ 
return x; 

1) Nothing. The code fails to compile because the object b is not created in a valid way. 
2) 2 

3) 2 

4) 3 

5) 3 

Q17 What is displayed when the following is executed: 
String s1 = "aaa"; 
s1.concat("bbb"); 
System.out.println(s1); 
1) The string "aaa". 
2) The string "aaabbb". 
3) Nothing. concat() is not a valid method in the String class. 
4) The string "bbbaaa". 
5) The string "bbb". 

Q18 True or False. 
The following is a valid way to construct a StringBuffer. 
StringBuffer sb1 = "abcd"; 
1) True 
2) False 

Q19 What is the output of the following piece of code: 
1. int x = 6; 
2. double d = 7.7; 
3. 
4. System.out.println((x > d) ? 99.9 : 9); 

1) 9 
2) 9.0 
3) 99.9 
4) Nothing. An ArithmeticException is t