當前位置:學問君>人在職場>IT認證>

JAVA語言程序設計練習題

學問君 人氣:2.16W

一、選擇題

1. 請說出下列代碼的執行結果 : B

String s = "abcd";

String s1 = new String(s);

if (s = = s1) System.out.println("the same");

if (s.equals(s1)) System.out.println("equals");

A. the same equals B. equals

C. the same D. 什麼結果都不輸出

2. 下列有關 Java 中接口的說法哪個是正確的? B

A. 接口中含有具體方法的實現代碼

B. 若一個類要實現一個接口,則用到 “implements” 關鍵字

C. 若一個類要實現一個接口,則用到“ extends ”關鍵字

D. 接口不允許繼承

3. 下列代碼的執行結果是什麼? B

String s1 = "aaa";

s1.concat("bbb");

System.out.println(s1);

A. The string "aaa".

B. The string "aaabbb".

C. The string "bbbaaa".

D. The string "bbb".

4. 如果有一個對象 myListener ( 其中 myListener 對象實現了 ActionListener 接口 ), 下列哪條語句使得 myListener 對象能夠接受處理來自於 smallButton 按鈕對象的動作事件 ? C

A. smallButton.add(myListener);

B. smallButton.addListener(myListener);

C. smallButton.addActionListener(myListener);

D. smallButton.addItem(myListener);

二.讀程序題

1. 讀下列代碼,說出這段程序的功能。

import java.io.*;

public class Test{

public static void main( String [] argv) {

try {

BufferedReader is =

new BufferedReader( new InputStreamReader(System.in));

String inputLine;

While ((inputLine = is.readLine ())!= null) {

System.out.println(inputLine);

}

is.close();

}catch (IOException e) {

System.out.println("IOException: " + e);

}

}

}

答案:讀取鍵盤輸入,顯示到屏幕上,直到鍵盤輸入爲空爲止。

2、 讀下列程序,寫出正確的執行結果。

class test {

public static void main (String [] args ){

int x=9, y;

if (x>=0)

if (x>0)

y=1;

else y=0;

else y=-1;

System.out.println(y);

}

}

答案:1

3、 讀程序,寫出正確的執行結果。

public class Father{

int a=100;

public void miner(){

a--;

}

public static void main(String[] args){

Father x = new Father();

Son y = new Son();

System.out.println(y.a);

System.out.println( y.getA());

y.miner();

System.out.println(y.a);

System.out.println(y.getA());

}

}

class Son extends Father{

int a = 0;

public void plus(){

a++;

}

public int getA() {

return super.a;

}

}

答案:

0

100

0

99

三 . 簡答題

1. Java語言的特點。

答:

簡單性:Java風格類似於C++,但它摒棄了C++中容易引起程序錯誤的`地方

面向對象:Java語言的設計是完全面向對象

分佈式:

解釋執行:

健壯性:Java提供自動垃圾回收機制,異常處理機制,進行嚴格的類型檢查

平臺無關性:

安全性

多線程

動態性

2. 請描述 AWT事件模型。

答:

結合AWT事件模型並舉例來說:

import java.awt.event.*;

1. 監聽對象必須實現對應事件監聽的機器的接口

class MyFirstFrame extends Frame implements ActionListener

{...}

2.明確事件監聽的機器的接口形式

public void actionPerformed ( ActionEvent event) {…}

3. MyFirstFrame 類必須實現接口ActionListener中的所有方法。

4. 註冊監聽對象.

爲了把MyFirstFrame對象註冊爲兩個按鈕的事件監聽對象,必須在MyFirstFrame

的構造函數中添加語句如下:

cancelButton.addActionListener(this);

okButton.addActionListener(this);

3. 在 Java中,怎樣創建一個線程?

答:

1、定義類來實現Runnable接口

class TestThread implements Runnable {

public void run() { …}

}

2、繼承Thread類來實現

class TestThread extends Thread {

TestThread(String name) {

super(name);

start();

}

public void run() {…}

}