You are on page 1of 2

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

學號 : 姓名 : 分數 :

1 變數宣告
(a). 請選出可以做為變數名稱的項目並打勾. (5%) (b). 請宣告下列變數 (10%)
(1) (X) 3a5c (1) 請宣告一個單精度浮點數 f
float f;
(2) () a321 (2) 請宣告一個字元 c,並將其初始值設定為 ‘c’.
char c= 'c';
(3) (X) 34(21) (3) 請宣告一個布林值 x,並將其初始值設為 false.
boolean x=false;
(4) () XYZ (4) 請宣告一個位元組 b ,並將其初始值設為 23.
byte b=23;
(5) () a_c (5) 請宣告一個字串 s, 並將其初始值設定為 "hello!".
String s= "hello";

2 基本運算的結果 (請填寫空格處的內容)
(a). 加減乘除 (10%) (b). 邏輯判斷 (10%) (c). 運算 (10%)
class TestOp1 { class TestOp2 { class TestOp3 {
public static void main(String args[]) { public static void main(String args[]) { public static void main(String args[]) {
int a = 5, b = 3; int a=5, b=3; int a = 5, b = 3;
a = a+b; if (a < b) a = b+1; a++;
System.out.println("a= "+a); System.out.println("a= "+a); System.out.println("a= "+a);
b = a-b; if (a > b) b = a+1; b--;
System.out.println("b= "+b); System.out.println("b= "+b); System.out.println("b= "+b);
a = a*b; if (a < b) a = b+1; boolean x = (a > b);
System.out.println("a= "+a); System.out.println("a= "+a); System.out.println("x= "+x);
b = a%b; if (a > b) b = a+1; x = !x;
System.out.println("b= "+b); System.out.println("b= "+b); System.out.println("x= "+x);
a = a*a; if (a < b) a = b+1; x = (!x && a>b);
System.out.println("a= "+a); System.out.println("a= "+a); System.out.println("x= "+x);
} } }
} } }
輸出結果 輸出結果 輸出結果
a= 8 a= 5 a= 6
b= 5 b= 6 b= 2
a= 40 a= 7 x = true
b= 0 b= 8 x= false
a= 1600 a= 9 x = true

3 程式架構 (10%)
(a). 請於右格中寫出一個完整的 Java 程式, class k100 {
可以印出 1, 2, 3, … 100 等 100 個數字,並將 public static void main(String[] args)
檔案存為 k100.java (4%) {
for (int i=1; i<=100; i++)
(b). 請寫出你用來編譯該程式的指令 (3%) System.out.println(i);
javac k100.java }
(c). 請寫出你用來執行該程式的指令 (3%) }
java k100
94 學年 上學期 金門技術學院 資管系 日四技 一年級 程式設計 期中考 出題者 : 陳鍾誠
學號 : 姓名 :
4 基本控制邏輯 (20%)
(a) 請利用邏輯判斷 if ,將下列程式中的數字 x (b). 請寫出一個可以從 100 印到 1 的迴圈.(10%)
轉換成其絕對值,然後將絕對值印出來。(你的程式 (注意、順序是由大到小)
必需在 x 為任意整數時都是對的)(10%)
class ABS { class Count {
public static void main(String[] args) { public static void main(String[] args) {
int x = 2; for (int i=100; i>=1; i--)
if (x < 0) System.out.println(i);
System.out.println("|x|="+(-1*x)); }
else }
System.out.println("|x|="+x);
}
}

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

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

You might also like