Você está na página 1de 2

94 學年 上學期 金門技術學院 資管系 夜二技 一年級 程式設計 期中考 出題者 : 陳鍾誠

學號 : 姓名 : 分數 :

1 變數宣告
(a). 請選出可以做為變數名稱的項目並打勾. (5%) (b). 請宣告下列變數 (10%)
(1) (√ ) ggg (1) 請宣告一個整數 i
int i;
(2) (X) 123.45 (2) 請宣告一個字元 c,並將其初始值設定為 'c'.
char c='c';
(3) (X) x-5 (3) 請宣告一個布林值 x,並將其初始值設為 false.
boolean x = false;
(4) (√) a_f (4) 請宣告一個浮點數 f ,並將其初始值設為 100
float f=100.0f;
(5) (√) Hello (5) 請宣告一個字串 s, 並將其初始值設定為 "George".
String s="George";

2 基本運算的結果 (請填寫空格處的內容)
(a). 加減乘除 (10%) (c). 運算 (10%)
class TestOp1 { class TestOp3 {
public static void main(String args[]) { public static void main(String args[]) {
int a = 7, b = 4; int a = 7, b = 4;
b = a+b; a++;
System.out.println("a= "+a); System.out.println("a= "+a);
a = b-a; b--;
System.out.println("b= "+b); System.out.println("b= "+b);
a = a+3; boolean x = (a > b);
System.out.println("a= "+a); System.out.println("x= "+x);
b = a%b; x = !x;
System.out.println("b= "+b); System.out.println("x= "+x);
a = a*a; x = (!x && a>b);
System.out.println("a= "+a); System.out.println("x= "+x);
} }
} }
輸出結果 輸出結果
a= 7 a= 8
b = 11 b= 3
a= 7 x = true
b= 7 x= false
a = 49 x = true

3 程式架構 (10%)
(a). 請於右格中寫出一個完整的 Java 程式,可以 class HelloJava
印出 “你好,我是 Java !”,並將檔案存為 {
HelloJava.java (4%) public static void main(String[] args)
{
(b). 請寫出你用來編譯該程式的指令 (3%)
System.out.println("你好,我是 Java !");
javac HelloJava.java
}
(c). 請寫出你用來執行該程式的指令 (3%)
}
java HelloJava

1
94 學年 上學期 金門技術學院 資管系 夜二技 一年級 程式設計 期中考 出題者 : 陳鍾誠
學號 : 姓名 :

4 基本控制邏輯 (20%)
(a) 請利用邏輯判斷 if ,將 x, y 當中較大的 (b). 請寫出一個可以印出 1 到 100 之間的偶數.(10%)
數值指定給 max 變數(你的程式必需在 x,y (注意:包含 100)
為任意整數時都是對的)(10%)
class MAX { class EvenList {
public static void main(String[] args) public static void main(String[] args) {
{ for (int i=1; i<=100; i++)
int x = 2, y = 5, max; {
if (x<y) if (i%2 == 0)
max = y ; System.out.println(i);
else }
max = x ; }
} }
}

5 陣列 (20%)
(b). 請寫一個程式使 c = a+b. (10%)
(a). 請 寫 一 個 含 有 迴 圈 的 程 式 , 將 a 陣 列 的
總乘積放入 p 中. (10%)
class Array1 { class Array2 {
public static void main(String[] args) public static void main(String[] args) {
{ int a[][] = {{1,5},{2,4}};
int a[] = {6, 7, 1, 3, 2, 4}; int b[][] = {{3,6},{7,8}};
int p = 1; int c[][] = new int[2][2];
for (int i=0; i<a.length; i++) for (int i=0; i<a.length; i++)
p = p*a[i]; {
System.out.println("p="+p); for (int j=0; j<a[i].length; j++)
} {
} c[i][j]=a[i][j]+b[i][j];
}
}
}
}

6 除錯題 (5%)
請圈選出右列 Java 程式中錯誤的項目(共 public class Debug {
有五個),並註明正確的寫法 (本程式所 public static void main(string[] args) {
想要產生的輸出結果為 i=3 i=4 i=5) int i == 3
1. string  String
WHILE (i <= 5) {
2. int i = = 3  int i=3;
system.out.print("i="&i);
3. WHILE  while
i+;
4. system  System
}
5. i+  i++
}
}

Você também pode gostar