112 司法特考三等 檢察事務官電子資訊組 程式語言 考古題詳解
第 一 題30 分
請問下圖一 Java 程式 StarPattern 的執行結果為何?並請參考該程式碼,以 for 迴圈為主,撰寫 Java 程式 Diamond,以列印如下圖二的圖案。切勿直接列印,例如:
System.out.println(" ********"); System.out.println("**********"); System.out.println(" ********");
圖一:
public class StarPattern { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 1; j < i; j++) System.out.print(" ");
for (int j = 1; j <= 6 - i; j++)
System.out.print("**");
System.out.println();
}
}
}
圖二:
*
*
登入後即可作答並保存紀錄。
核心觀念
本題考查 Java 的巢狀 for 迴圈,以及:
System.out.print:列印後不換行。System.out.println:列印後換行。- 外層迴圈控制列數。
- 內層迴圈分別控制每列的空白數與星號數。
第一部分:圖一執行結果
外層迴圈變數 從 執行至 。
每列的空白數為:
每列的星號數為:
因此各列結果如下:
| 空白數 | 星號數 | |
|---|---|---|
| 1 | 0 | 10 |
| 2 | 1 | 8 |
| 3 | 2 | 6 |
| 4 | 3 | 4 |
| 5 | 4 | 2 |
其中每次列印 "**",因此星號總數為 。
執行結果為:
********************
****************
************
********
**
第二部分:Diamond 程式設計
圖二共有 列,分為上半部與下半部。
上半部
第 列,其中 至 :
- 前導空白數:
- 星號數:
例如:
- 第 1 列: 個空白、 顆星
- 第 3 列: 個空白、 顆星
- 第 5 列: 個空白、 顆星
下半部
第 列,其中 至 :
- 前導空白數:
- 星號數:
下半部從 顆星逐列遞減至 顆星。
第 二 題20 分
stringReverse() 能反向列印參數 array 的字元陣列,如底下 main() 會將字元陣列值 reverse 輸出為 esrever。請以遞迴(recursive)的方式撰寫 stringReverse(char[] array, int index),其中參數 array、index 的型態與命名必須維持不變,不需處理防呆。(Hint:使用 array.length 以取得 array 內字元的個數。)
public class Reverse { private static void stringReverse(char[] array, int index) { // 請以遞迴方式撰寫 }
public static void main(String args[])
{
char[] str = {'r', 'e', 'v', 'e', 'r', 's', 'e'};
stringReverse(str, 0);
}
}
輸出:
esrever
登入後即可作答並保存紀錄。
核心觀念
本題考遞迴的終止條件與遞迴步驟。array.length 代表字元陣列的長度;索引從 0 開始,因此陣列最後一個字元的索引是 array.length - 1。
解題方法
由 main 傳入 index = 0,每次呼叫先輸出目前索引位置的字元,再以 index + 1 呼叫自身。當索引到達陣列長度時,表示所有字元都已輸出,遞迴應停止。
private static void stringReverse(char[] array, int index)
{
if (index == array.length)
{
return;
}
System.out.print(array[array.length - 1 - index]);
stringReverse(array, index + 1);
}
輸出位置依序為:
第 三 題20 分
請寫出下列 Python 程式的執行結果為何?
def swap(x, y): temp = x x = y y = temp
x = 2 y = 3 swap(x, y) print(x, y) print()
l1 = [1, 2, 3, 4, 5] l2 = [6, 5, 4, 3, 2] l3 = [False, False, True, False, True] print([a if condition else b for a, b, condition in zip(l1, l2, l3)]) print()
import numpy as np
arr = np.array(l1) newarr = arr[arr % 2 == 0] print(newarr) print()
import pandas as pd
data = { 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }
df = pd.DataFrame(data) df['add'] = df.apply(np.sum, axis=1) print(df)
登入後即可作答並保存紀錄。
核心觀念
本題考查 Python 的變數指派與函式呼叫、串列生成式、NumPy 布林索引,以及 pandas DataFrame.apply 的列方向運算。
- Python 將整數等不可變物件傳入函式時,函式內重新指派參數,不會改變函式外的變數。
- 串列生成式可依條件選取兩個值之一;
zip會將多個序列中相同位置的元素配對。 - NumPy 布林索引會留下條件為
True的陣列元素。 DataFrame.apply(..., axis=1)會逐列套用函式;np.sum對每列加總。
解題方法
1. 函式內的變數指派
def swap(x, y):
temp = x
x = y
y = temp
x = 2
y = 3
swap(x, y)
print(x, y)
呼叫 swap(x, y) 時,函式參數 x、y 分別取得整數值 2、3。函式內交換的是參數名稱所指向的值;這些重新指派不會改變函式外的 x、y。因此輸出為:
2 3
接著的 print 沒有指定內容,會輸出一個空白行。
2. zip 與條件式串列生成式
[l1_item if condition else l2_item
for l1_item, l2_item, condition in zip(l1, l2, l3)]
zip(l1, l2, l3) 依位置配對三個串列:
l1 | l2 | l3(條件) | 選取結果 |
|---|---|---|---|
| 1 | 6 | False | 6 |
| 2 | 5 | False | 5 |
| 3 | 4 | True | 3 |
| 4 | 3 | False | 3 |
| 5 | 2 | True | 5 |
條件為 True 時取 a,否則取 b,所以輸出為:
第 四 題30 分
試就下列 Java 程式碼,回答三個問題:
(一)繪出這些程式間的類別繼承關係(class inheritance hierarchy)。
(二)於 StudentDemo 的 main() 中加入一小段具多型(polymorphism)特性的程式碼。
(三)修改程式,讓 Student 繼承 Person,其中 Person 擁有 firstName 與 lastName 兩個屬性(亦即 Student 只剩 id 與 tuition 兩個屬性),請完整撰寫 Person 與 Student 兩個類別,且需完成所有相關 constructors 與 methods 的實作。
原程式如下:
public abstract class Student { private final String id; private final String firstName; private final String lastName; protected double tuition;
public Student(String id, String firstName, String lastName)
{
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public String getId()
{
return id;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public double getTuition()
{
return tuition;
}
public abstract void setTuition();
}
public class UndergraduateStudent extends Student { public static final double UNDERGRAD_TUITION = 4000;
public UndergraduateStudent(String id, String firstName, String lastName)
{
super(id, firstName, lastName);
setTuition();
}
public void setTuition()
{
tuition = UNDERGRAD_TUITION;
}
}
public class GraduateStudent extends Student { public static final double GRAD_TUITION = 6000;
public GraduateStudent(String id, String firstName, String lastName)
{
super(id, firstName, lastName);
setTuition();
}
public void setTuition()
{
tuition = GRAD_TUITION;
}
}
public class StudentAtLarge extends Student { public static final double SAL_TUITION = 2000;
public StudentAtLarge(String id, String firstName, String lastName)
{
super(id, firstName, lastName);
setTuition();
}
public void setTuition()
{
tuition = SAL_TUITION;
}
}
public class StudentDemo { public static void main(String[] args) { ... } }
登入後即可作答並保存紀錄。
核心觀念
本題考查 Java 的類別繼承、抽象類別、建構子串接、多型,以及封裝與程式重構。
Student是抽象類別,不能直接建立物件;它宣告抽象方法setTuition,由子類別實作。UndergraduateStudent、GraduateStudent、StudentAtLarge都繼承Student,並分別設定自己的學費。- 多型是指以父類別型別的參考變數,指向子類別物件;呼叫被覆寫的方法時,執行的是物件實際類別所提供的版本。
- 將共同的姓名屬性移至
Person後,Student繼承Person,只保留學生專屬的id與tuition。
(一)類別繼承關係
Student 是抽象父類別,三種學生類別都是它的直接子類別:
Student(abstract)
/ | \
/ | \
UndergraduateStudent GraduateStudent StudentAtLarge
StudentDemo 負責建立或使用物件,並未出現在上述繼承關係中。
(二)在 main 加入多型程式碼
可建立一組 Student 型別的參考變數,分別指向不同子類別物件,再透過父類別型別呼叫共用方法:
public static void main(String[] args)
{
Student[] students = {
new UndergraduateStudent("U001", "怡君", "陳"),
new GraduateStudent("G001", "志明", "林"),
new StudentAtLarge("A001", "雅婷", "王")
};
for (Student student : students) {
System.out.println(
student.getFirstName + student.getLastName
+ ",學號:" + student.getId
+ ",學費:" + student.getTuition
);
}
}
陣列的元素型別是 Student,但實際物件分別是三種不同子類別。各子類別建構時都會呼叫自己的 setTuition,因此取得的學費依序為 $4000、$6000、`