JavaSE軟件工程師認證考試試卷java
1、 填空題(共20個題目,總計20分)c++
2、 選擇題(共25個題目,總計25分)編程
1)數組 |
在Java中,源文件Test.java中包含以下代碼,則程序編譯運行的結果是(A )。(選擇一項)安全 |
|
|
public class Test {服務器 public static void main(String[ ] args) {網絡 system.out.println("Hello!");數據結構 }多線程 }框架 |
|
|
|
|
|
A |
輸出:Hello! |
|
B. |
編譯出錯,提示「沒法解析system」 |
|
C. |
運行正常,但沒有輸出任何內容 |
|
D. |
運行時出現異常 |
2) |
在Java中,下面( AB )語句能正確經過編譯。(選擇兩項) |
|
|
|
|
|
A |
System.out.println(1+1); |
|
B. |
char i =2+'2'; System.out.println(i); 52------對應的ASCII碼是4 |
|
C. |
String s="on"+'one'; |
|
D. |
int b=255.0; |
3) |
分析以下Java代碼,編譯運行的輸出結果是( A )。(選擇一項) |
|
|
public static void main(String[ ] args) { boolean a=true; boolean b=false; if (!(a&&b)) { System.out.print("!(a&&b)"); }else if (!(a||b)) { System.out.println("!(a||b)"); }else { System.out.println("ab"); } } |
|
|
|
|
|
A |
!(a&&b) |
|
B. |
!(a||b) |
|
C. |
ab |
|
D. |
!(a||b)ab |
4) |
在Java中有以下代碼,則編譯運行該類的輸出結果是( D )。(選擇一項) |
|
|
public static void main(String[ ] args) { for(int i=0;i<10;i++){ if (i%2!=0) return; System.out.print(i); } } |
|
|
|
|
|
A |
13578 |
|
B. |
02468 |
|
C. |
0123456789 |
|
D. |
0 |
5) |
在Java的程序類中以下方法定義正確的是( AD )。(選擇兩項) |
|
|
|
|
|
A |
public int ufTest(int num){ int sum=num+100; return sum; } |
|
B. |
public String ufTest(int num){ int sum=num+100; return sum; } |
|
C. |
public void ufTest(int num){ int sum=num+100; return sum; } |
|
D. |
public float ufTest(int num){ int sum=num+100; return sum; } |
6) |
分析下面的Java源程序,編譯後的運行結果是( B )。(選擇一項) |
|
|
import java.util.*; public class Test { public static void main(String[ ] args) { int [ ] numbers=new int[ ]{1,2,3}; 返回該數組對應的指定的索引 System.out.println(Arrays.binarySearch(numbers, 2)); } } |
|
|
|
輸出 |
|
A |
輸出:0 |
|
B. |
輸出:1 |
|
C. |
輸出:2 |
|
D. |
輸出:3 |
7) |
分析以下Java程序的代碼所示,則編譯運行後的輸出結果是( C )。(選擇一項) |
|
|
public class Test { int count=9; public void count1(){ count=10; System.out.println("count1="+count); } public void count2(){ System.out.println("count2="+count); } public static void main(String[ ] args) { Test t=new Test(); t.count1(); t.count2(); } } |
|
|
|
|
|
A |
count1=9; count2=9; |
|
B. |
count1=10; count2=9; |
|
C. |
count1=10; count2=10 c成員變量在整個類中使用。 |
|
D. |
count1=9; count2=10; |
8) |
在Java中,如下定義數組的語句正確的是( CD )。(選擇兩項) |
|
|
|
|
|
A |
int t[10]=new int[ ]; |
|
B. |
char [ ]a=」hello」; |
|
C. |
String [ ] s=new String [10]; |
|
D. |
double[ ] d [ ]=new double [4][ ]; |
9) |
分析以下Java代碼,該程序編譯後的運行結果是( D )。(選擇一項) |
|
|
public static void main(String[ ] args) { String str=null; Null就表名空指針,不在指向任何東西則會拋出NullPointerException str.concat("abc"); str.concat("def"); System.out.println(str); } |
|
|
|
|
|
A |
null |
|
B. |
abcdef |
|
C. |
編譯錯誤 |
|
D. |
運行時出現NullPointerException異常 |
10) |
給定以下Java代碼,編譯運行後,輸出結果是( CD )。(選擇一項) |
|
|
public class Test { public static void main(String[ ] args) { StringBuffer a=new StringBuffer("A"); StringBuffer b=new StringBuffer("B"); operate(a, b); System.out.println(a+","+b); } static void operate(StringBuffer x,StringBuffer y){ x=new StringBuffer("BBB"); y=x; } } |
|
|
|
|
|
A |
BBB,B |
|
B. |
BBB,BBB |
|
C. |
A,B |
|
D. |
A,A |
11) |
給定以下Java程序,Test類中的四個輸出語句輸出結果依次是( C )。(選擇一項) |
|||
|
class Person { String name="person"; public void shout(){ System.out.println(name); } } class Student extends Person{ String name="student"; String school="school"; } public class Test { public static void main(String[ ] args) { Person p=new Student(); System.out.println(p instanceof Student); System.out.println(p instanceof Person); System.out.println(p instanceof Object);; System.out.println(p instanceof System); } } |
|||
|
|
|
||
|
A |
true,false,true,false |
||
|
B. |
false,true,false,false |
||
|
C. |
true,true,true,編譯錯誤 |
||
|
D. |
true,true,false,編譯錯誤 |
||
12) |
在Java中,LinkedList類與ArrayList類同屬於集合框架類,下列(CD )選項中是LinkedList類有而ArrayList類沒有的方法。(選擇兩項) |
|||
|
|
|
||
|
A |
add(Object o) |
||
|
B. |
add(int index,Object o) |
||
|
C. |
getFirst() |
||
|
D. |
removeLast() |
||
13) |
以下Java代碼,輸出的運行結果是( A )。(選擇一項) |
|
|
public class Test { public static void main(String[ ] args) { List<String> list=new ArrayList<String>(); list.add("str1"); list.add(2, "str2"); String s=list.get(1); System.out.println(s); } } |
|
|
|
|
|
A |
運行時出現異常 |
|
B. |
正確運行,輸出str1 |
|
C. |
正確運行,輸出str2 |
|
D. |
編譯時出現異常 |
14) |
在Java中關於靜態方法,如下說法中正確的是( AC )。(選擇兩項) |
|
|
|
|
|
A |
靜態方法中不能直接調用非靜態方法——》對,經過建立對象的形式 |
|
B. |
非靜態方法中不能直接調用靜態方法 è錯,能夠訪問 |
|
C. |
靜態方法能夠用類名直接調用 對 |
|
D. |
靜態方法裏可使用this super |
15) |
以下Java代碼運行的結果是( B )。(選擇一項) |
|
|
public class Test { final int age; public Test(){ age=10; } public static void main(String[ ] args) { System.out.println(new Test().age); } } |
|
|
|
|
|
A |
輸出:0 |
|
B. |
輸出:10 |
|
C. |
輸出:null |
|
D. |
運行時出現異常 |
16) |
在Java中關於abstract關鍵字,如下說法正確的是( AB)。(選擇兩項) |
|
|
|
|
|
A |
abstract類中能夠沒有抽象方法 |
|
B. |
abstract類的子類也能夠是抽象類 |
|
C. |
abstract方法能夠有方法體 |
|
D. |
abstract類能夠建立對象 |
17) |
在Java中,下列關於讀寫文件的描述錯誤的是( B )。(選擇一項) |
|
|
|
|
|
A |
Reader類的read()方法用來從源中讀取一個字符的數據 |
|
B. |
Reader類的read(int n )方法用來從源中讀取一個字符的數據 |
|
C. |
Writer類的write(int n)方法用來向輸出流寫入單個字符 |
|
D. |
Writer類的write(String str)方法用來向輸出流寫入一個字符串 |
18) |
分析以下Java代碼,有標註的四行代碼中,有錯誤的是第( D )處。(選擇一項) |
|
|
import java.io.FileWriter; import java.io.IOException; public class Test { public static void main(String[ ] args) { String str = "Hello World"; FileWriter fw = null; try { fw = new FileWriter("c:\\hello.txt"); // 1 fw.write(str); // 2 } catch (IOException e) { e.printStackTrace(); // 3 } finally { fw.close(); // 4 } } } |
|
|
|
|
|
A |
1 |
|
B. |
2 |
|
C. |
3 |
|
D. |
4 |
19) |
在Java的IO操做中,( D )方法能夠用來刷新流的緩衝。(選擇兩項) |
|
|
|
|
|
A |
void release() |
|
B. |
void close() |
|
C. |
void remove() |
|
D. |
void flush() |
20) |
下列關於線程的優先級說法中,正確的是( BC )。(選擇兩項) |
|
|
|
|
|
A |
線程的優先級是不能改變的 |
|
B. |
線程的優先級是在建立線程時設置的 |
|
C. |
在建立線程後的任什麼時候候均可以從新設置 |
|
D. |
線程的優先級的範圍在1-100之間 1—10 之間 |
線程的優先級在建立線程時能夠設置,也能夠經過getPriority()方法來得到線程的優先級,經過setPriority()方法來設定線程的優先級。線程的優先級屬於考試重、
21) |
下列關於Java線程的說法正確的是( B )。(選擇一項) |
|
|
|
一個虛擬的CPU。(2)該CPU執行的代碼。(3)代碼所操做的數據 |
|
A |
每個Java線程能夠當作由代碼、一個真實的CPU以及數據三部分組成 |
|
B. |
建立線程的兩種方法中,從Thread類中繼承的建立方式能夠防止出現多父類的問題 |
|
C. |
Thread類屬於java.util程序包 lang包 |
|
D. |
使用new Thread(new X()).run();方法啓動一個線程 start() |
22) |
在Java網絡編程中,使用客戶端套接字Socket建立對象時,須要指定( A )。(選擇一項) |
|
|
|
|
|
A |
服務器主機名稱和端口 |
|
B. |
服務器端口和文件 |
|
C. |
服務器名稱和文件 |
|
D. |
服務器地址和文件 |
23) |
在Java的反射中關於代理說法正確的是( )。(選擇兩項) |
|
|
|
|
|
A |
動態代理類與靜態代理類同樣,必須由開發人員編寫源代碼,並進行編譯 |
|
B. |
代理類與被代理類具備一樣的接口 |
|
C. |
動態代理是public、final和abstract類型的 |
|
D. |
java.lang.reflect包中的Proxy類提供了建立動態代理類的方法 |
3、 判斷題(共20個題目,總計10分,正確√,錯誤×)
4、 簡答題(共5個題目,總計25分)
答: try…catch: 進行異常的捕獲。try :該代碼塊中編寫可能產生異常的代碼。Catch:實現對某種異常的捕獲。來處理異常
答: finally:有一些特定的代碼,不管是否發生異常,都須要去處理,則放在finally裏中的代碼,是必定會被執行到的。
答:throw: 用來拋出一個異常類對象,應用在方法體中。
答: throws: 用來聲明一個異常,應用在方法簽名的後面,關鍵字throws 用於方法上來聲明一個異常,用於表示當前的方法不處理異常,而是提醒該方法的調用者來處理異常。
答:sleep方法,能夠強制當前執行的線程休眠,以減慢線程,當咱們調用sleep方法時 就進入了休眠狀態。(計時等待),Wait方法: 一個線程在等待另外一個線程執行一個動做時,該線程進入Waiting狀態,進入該狀態是不能本身喚醒的,必須等待另外一個線程調用notify關鍵字來喚醒。
2 請你簡述HashMap和Hashtable的區別?(5分)
答: HashMap和Hashtable都實現了Map接口,HashMap:此集合中存儲的數據是無序且不重複的, HashMap能夠接受爲null的鍵值(key)和值(value),而Hashtable則不行
2HashMap是非synchronized,而Hashtable是synchronized,這意味着Hashtable是線程安全的,多個線程能夠共享一個Hashtable;而若是沒有正確的同步的話,多個線程是不能共享HashMap的.
3另外一個區別是HashMap的迭代器(Iterator)是fail-fast迭代器,而Hashtable的enumerator迭代器不是fail-fast的。因此當有其它線程改變了HashMap的結構(增長或者移除元素),將會拋出ConcurrentModificationException,但迭代器自己的remove()方法移除元素則不會拋出ConcurrentModificationException異常。但這並非一個必定發生的行爲,要看JVM。這條一樣也是Enumeration和Iterator的區別。
4. 因爲Hashtable是線程安全的也是synchronized,因此在單線程環境下它比HashMap要慢。若是你不須要同步,只須要單一線程,那麼使用HashMap性能要好過Hashtable。
3 TCP/IP協議棧中,TCP協議和UDP協議的聯繫和區別?(5分)
答:TCP:TCP:傳輸控制協議,TCP 協議是面向鏈接的通訊協議,即傳輸數據以前在發送端和接收端創建邏輯鏈接,而後再傳輸數據,它提供了兩臺計算機之間可靠無差錯的數據傳輸。
答:UDP: 用戶數據報協議,UDP 協議是一個面向無鏈接的協議,傳輸數據時,不須要創建鏈接,無論對方是否啓動,直接將數據,數據源和目的地都封裝在數據包中,直接發送
1、 編碼題(共3個題目,總計20分)
1. 編寫兩個線程,一個線程打印1-52的整數,另外一個線程打印字母A-Z。打印順序爲12A34B56C….5152Z。即按照整數和字母的順序從小到大打印,而且每打印兩個整數後,打印一個字母,交替循環打印,直到打印到整數52和字母Z結束。(8分)
要求:
(1) 編寫打印類Printer,聲明私有屬性index,初始值爲1,用來表示是第幾回打印。
(2) 在打印類Printer中編寫打印數字的方法print(int i),3的倍數就使用wait()方法等待,不然就輸出i,使用notifyAll()進行喚醒其它線程。
(3) 在打印類Printer中編寫打印字母的方法print(char c),不是3的倍數就等待,不然就打印輸出字母c,使用notifyAll()進行喚醒其它線程。
(4) 編寫打印數字的線程NumberPrinter繼承Thread類,聲明私有屬性private Printer p;在構造方法中進行賦值,實現父類的run方法,調用Printer類中的輸出數字的方法。
(5) 編寫打印字母的線程LetterPrinter繼承Thread類,聲明私有屬性private Printer p;在構造方法中進行賦值,實現父類的run方法,調用Printer類中的輸出字母的方法。
(6) 編寫測試類Test,建立打印類對象,建立兩個線程類對象,啓動線程。
public class Test09 { public static void main(String[] args) { //建立打印類的對象 Printer tt=new Printer(); //建立打印數字的方法 NumberPrinter ss=new NumberPrinter(tt); ss.start(); //建立打印字母的方法 LetterPrinter dd=new LetterPrinter(tt); dd.start(); } } //編寫一個打印類 class Printer{ //用來表示打印的次數 private int index=1; //2)在打印類Printer中編寫打印數字的方法print(int i),3的倍數就使用wait()方法等待,不然就輸出i,使用notifyAll()進行喚醒其它線程。 //打印數字的方法 public synchronized void print(int i){ //判斷indecx可否被3整除,不能整除的話就輸出數字,index++. // 能整除的話就等待,調用輸出字母的線程。 if(index%3==0){ try{ wait(); }catch (InterruptedException ce){ ce.printStackTrace(); }
}else{ System.out.println(i); index++; notifyAll();//喚醒字母的線程 } }
//編寫打印字母的方法 public synchronized void print(char c){ //若是index不能被3整除的話,就表示是是數字 //若是除以3等於零,則表示的是字母。 if(index%3!=0){ try{ wait(); }catch (InterruptedException ce){ ce.printStackTrace(); }
}else{ //輸出的是字母 System.out.println(c); index++; notifyAll();//喚醒數字的線程 } } } //編寫打印數字的線程 class NumberPrinter extends Thread{ private Printer p;
public NumberPrinter(Printer p){ this.p=p; }
public void run(){ // 調用Printer類中的輸出數字的方法*/ for(int i=1;i<=52;i++){ p.print(i); } } }
//編寫打印字母的方法 class LetterPrinter extends Thread{ private Printer p; public LetterPrinter(Printer p){ this.p=p; } public void run() { //調用Printer類中的輸出字母的方法,65(A)--122(z) for(char c='A';c<'z';c++){ p.print(c); } } } |
2. 使用IO包中的類讀取D盤上exam.txt文本文件的內容,每次讀取一行內容,將每行做爲一個輸入放入ArrayList的泛型集合中並將集合中的內容使用增強for進行輸出顯示。
//BufferedReader 字符緩衝流讀操做 public class Meth09 { public static void main(String[] args)throws IOException{ List<String>list=new ArrayList<>(); BufferedReader fr=new BufferedReader(new FileReader("F:\\aaa\\a.txt")); String s=null; //每次讀取一行的內容 while ((s=fr.readLine())!=null){ list.add(s); } for(String ff:list){ System.out.println(ff); } fr.close(); } } |