【java】pta18周練習題

判斷題

1、java語言中不用區分字母的大寫小寫。 F

2、如果線程死亡,它便不能運行。 T

3、靜態變量是被同一個類的所有實例所共享的。 T

4、在Java中,高優先級的可運行線程會搶佔低優先級線程。 F

5、ArrayList類是線程安全的。F

6、Java中所有的I/O都是通過流來實現的。 T

7、Java中數組的元素只能是簡單數據類型。 F

8、當GUI應用程序使用邊界佈局管理器BorderLayout時,GUI組件可以按任何順序添加到面板上。( ) T

9、類也是一種數據類型(type)。 T

10、匿名類的類體中不可以聲明static成員變量和static方法。 T

11、Almost anything can be considered an object. 分值爲3分。T

12、對象是類的實例(instance)。 T

13、容器是用來組織其他GUI組件的單元,它不能嵌套其他容器。( ) F

14、StringBuilder類是線程安全的,StringBuffer類是線程不安全的。 F

15、如果一個類的聲明中沒有使用extends關鍵字,這個類被系統默認爲是繼承Object類。T

16、在AWT的事件處理機制中,每個事件類對應一個事件監聽器接口,每一個監聽器接口都有相對應的適配器。( ) F


選擇題

1、在複選框中移動鼠標,然後單擊一選項,要捕獲所選項必需實現哪個接口?() (2分)

MouseListener
MouseMotionListern
ItemListener
ActionListener

2、下列哪一項不屬於佈局管理器? ( ) (2分)

GridLayout
CardLayout
BorderLayout
BagLayout

3、list是一個ArrayList的對象,哪個選項的代碼填寫到//todo delete處,可以在Iterator遍歷的過程中正確並安全的刪除一個list中保存的對象?( ) (2分)

    Iterator it = list.iterator();
    int index = 0;
    while (it.hasNext()){ 
          Object obj = it.next(); 
          if (needDelete(obj)) { //needDelete返回boolean,決定是否要刪除
               //todo delete
           } 
          index ++;
    }

list.remove(obj);
list.remove(it.next());
list.remove(index);
it.remove();

4、下面說法不正確的是( ) (2分)

當子類對象和父類對象能接收同樣的消息時,它們針對消息產生的行爲可能不同;
子類在構造函數中可以使用super( )來調用父類的構造函數;
一個子類的對象可以接收父類對象能接收的消息;
父類比它的子類的方法更多;

5、JFrame的缺省佈局管理器是( )。 (2分)

GridLayout
FlowLayout
CardLayout
BorderLayout

6、編譯Java源程序文件將產生相應的字節碼文件,這些字節碼文件的擴展名爲( )。(2分)

.exe
.byte
.class
.html

7、下列方法頭中哪一個不與其他方法形成重載(overload)關係?( ) (2分)

void mmm()
void mmm(String s)
int mm()
void mmm(int i)

8、下列哪些語句關於Java內存回收的說明是正確的? ( ) (2分)

內存回收程序負責釋放無用內存
內存回收程序允許程序員直接釋放內存
內存回收程序可以在指定的時間釋放內存對象
程序員必須創建一個線程來釋放內存

9、以下關於Java的局部內部類的說法錯誤的是( ) (2分)

局部內部類不能包含靜態成員
局部內部類只能在當前類中使用
在局部內部類中定義的內部類不能被private修飾符修飾
局部內部類可以訪問外部類的所有成員

10、Swing組件必須添加到Swing頂層容器相關的( )。(2分)

選項卡上
複選框內
內容面板上
分隔板上

11、以下關於構造函數的描述錯誤的是( )。 (2分)

構造函數的返回類型只能是void型。
構造函數是類的一種特殊函數,它的方法名必須與類名相同。
構造函數的主要作用是完成對類的對象的初始化工作。
一般在創建新對象時,系統會自動調用構造函數。

12、如果需要從文件中讀取數據,則可以在程序中創建哪一個類的對象()。 (2分)

DataOutputStream
FileWriter
FileInputStream
FileOutputStream

13、以下關於繼承的敘述正確的是( )。 (2分)

在Java中類只允許單一繼承
在Java中接口只允許單一繼承
在Java中一個類不能同時繼承一個類和實現一個接口
在Java中一個類只能實現一個接口

14、聲明並創建一個按鈕對象b,應該使用的語句是( ) (2分)

button b=new button( );
Button b=new Button( );
b.setLabel(“確定”);
Button b=new b( );

15、paint( )方法使用哪種類型的參數? ( ) (2分)

Graphics
String
Color
Graphics2D

16、要產生[20,999]之間的隨機整數使用哪個表達式? ( ) (2分)

(int)Math.random()*999
20+(int)(Math.random()*980)
(int)(20+Math.random()*97)
20+(int)Math.random()*980

17、JPanel組件的默認佈局管理器是( )。 (2分)

BorderLayout
GridLayout
FlowLayout
CardLayout


函數題

從抽象類shape類擴展出一個正n邊形
在一個正n邊形(Regular Polygon)中,所有邊的邊長都相等,且所有角的度數相同(即這個多邊形是等邊、等角的)。請從下列的抽象類shape類擴展出一個正n邊形類RegularPolygon,這個類將正n邊形的邊數n和邊長a作爲私有成員,類中包含初始化邊數n和邊長a的構造方法。

計算正n邊形的面積公式爲: Area=n×a×a/(tan((180度/n))×4);

類名:RegularPolygon
裁判測試程序樣例:

abstract class shape {// 抽象類

    /* 抽象方法 求面積 */
    public abstract double getArea();

    /* 抽象方法 求周長 */
    public abstract double getPerimeter();
}
/* 你提交的代碼將嵌入到這裏 */ 

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        DecimalFormat d = new DecimalFormat("#.####");// 保留4位小數
        int n=input.nextInt();
        double side = input.nextDouble();

        shape rp = new  RegularPolygon(n,side);

        System.out.println(d.format(rp.getArea()));
        System.out.println(d.format(rp.getPerimeter()));
        input.close();
    }
}

輸入樣例:
5
7
輸出樣例:
84.3034
35
提交代碼:

class RegularPolygon extends shape {

    private int n;
    private double side;
    public RegularPolygon(int n, double side) {
        this.n = n;
        this.side = side;
    }

    public double getArea() {
        return n*side*side/(Math.tan(Math.toRadians(180/n))*4);
    }
    public double getPerimeter() {
        return n*side;
    }

}

編程題

1、字符串替換
將文本文件中指定的字符串替換成新字符串。 由於目前的OJ系統暫時不能支持用戶讀入文件,我們編寫程序從鍵盤輸入文件中的內容,當輸入的一行爲end時,表示結束。end後面有兩個字符串,要求用第二個字符串替換文本中所有的第一個字符串。

輸入樣例:

Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end
Institute
University
輸出樣例:

Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

提交代碼:

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String strings=new String();
        String s=in.nextLine();
        while(!s.equals("end")){
            strings=strings.concat(s);
            s=in.nextLine();
        }
        String re1=in.next();
        String re2=in.next();
        String string2=strings.replaceAll(re1,re2);
        System.out.print(string2);
        in.close();
    }
}

2、找素數
請編寫程序,從鍵盤輸入兩個整數m,n,找出等於或大於m的前n個素數。

輸入樣例:
9223372036854775839 2

輸出樣例:
9223372036854775907
9223372036854775931

提交代碼:

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        BigInteger x =in.nextBigInteger();
        int count=in.nextInt();
        while(count>0){
            if (x.isProbablePrime(100)) {
                System.out.println(x);
                x=x.nextProbablePrime();
                count--;
            }else
                x=x.nextProbablePrime();

        }
        in.close();

    }

}

3、查找電話號碼
文件phonebook1.txt中有若干聯繫人的姓名和電話號碼。

高富帥 13312342222

白富美 13412343333

孫悟空 13512345555

唐三藏 13612346666

豬悟能 13712347777

沙悟淨 13812348888

請你編寫一個簡單的通信錄程序,當從鍵盤輸入一個姓名時查找到對應的電話號碼並輸出。如果沒找到則顯示Not found. 由於目前的自動裁判系統暫時不能支持用戶讀入文件,我們編寫程序從鍵盤輸入文件中的姓名和電話號碼,當輸入的名字爲noname時,表示結束。noname後面有一個名字,需要查找其對應的電話號碼。

輸入樣例:

白富美 13412343333
孫悟空 13512345555
唐三藏 13612346666
豬悟能 13712347777
沙悟淨 13812348888
noname
白骨精
輸出樣例:
Not found.

提交代碼:

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        HashMap<String, String> map = new HashMap<String, String>();
        String str = null;
        while (in.hasNext()){
            if (!(str = in.next()).equals("noname")) {
                map.put(str, in.next());
            } else {
                break;
            }
        }
        String s = in.next();
        if (map.containsKey(s)) {
            System.out.println((map.get(s)));
        } else
            System.out.println("Not found.");

        in.close();

    }

}

4、日期加減
請編程從鍵盤輸入一個長整型的值,該值表示從1970年1月1日算起的一個特定時間(毫秒數),以此時間構造一個日期對象。再輸入一個普通整型值,該值表示天數,加上該天數後,然後輸出對應的年、月、日。

輸入樣例:

1234567898765
158
輸出樣例:

2009-02-14
2009-07-22

提交代碼:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Calendar date = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        long time = in.nextLong();
        date.setTimeInMillis(time);
        System.out.println(format.format(date.getTime()));
        int day = in.nextInt();
        date.add(Calendar.DAY_OF_MONTH, day);
        System.out.println(format.format(date.getTime()));
        in.close();

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章