---
# System prepended metadata

title: APCS 2021/01/09 - B卷
tags: [筆試題目, 2021 觀念題]

---

APCS 2021/01/09 - B卷
===
###### tags: `2021 觀念題` `筆試題目`

```c=
void swap( int a, int b ) {
    printf("%d, %d", a, b) ;
    __________
    b = a - b ;
    a = a - b ;
    printf("%d, %d", a, b) ;
}
```
01. 以上程式碼如何達成交換2個變數的功能？
	+ [A] a = a - b
	+ [B] a = a + b
	+ [C] b = a - b
	+ [D] b = a + b

---
```c=
int f(int n){
    if (n > 17 ) 
        n += 5 ;
    while ( n >= 23 ) 
        n -= 6 ;
    if (n > 17 ) 
        n += 2 ;
    return n;
}
```
02. 執行以上程式碼，不可能回傳哪個值？
	+ [A] 23
	+ [B] 21
	+ [C] 19
	+ [D] 17

---
```c=
int a = 0 ;
for ( int i = 1 ; i <= n ; i = i + 1 )
    for ( int j = 1 ; j <= n ; j = 2 * j )
        for ( int k = i ; k <= n ; k = k + 1 )
            a += 1 ;
```
03. 執行以上程式碼後的 a 的值是多少？
	+ [A] $\frac{n(n+1)(n+2)}{3}$
	+ [B] $\frac{n(n+1)(2n+1)}{6}$
	+ [C] $\frac{n\lfloor \log_2{n} \rfloor(n+1)}{2}$
	+ [D] $\frac{n(\lfloor \log_2{n} \rfloor+1 )(n+1)}{2}$

---
```c=
int num[ 10 ] ;
for( int i = 0 ; i < 10 ; i = i + 1 ) 
    scanf( “%d”, &num[ ( i + 6 ) % 10 ] ) ;
```
04. 執行上述程式碼時依序輸入 10, 9, 8, ..., 1 ，請問陣列內的數字為何？
    + [A] { 6, 5, 4, 3, 2, 1, 10, 9, 8, 7 }
    + [B] { 5, 6, 7, 8, 9, 10, 1, 2, 3, 4 }
    + [C] { 4, 5, 6, 7, 8, 9, 10, 1, 2, 3 }
    + [D] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 10 }

---
```c=
int a = 7, b = 3 ;
float w = a / 2 / b * 1.0 ;
float x = a / b / 2.0 ;
float y = a / 2 / b ;
float z = 1.0 * a / 2 / b ;
```
05. 以下選項敘述何者正確？
    + [A] $\lvert w-x  \rvert > 0.5$ 
    + [B] $\lvert x-z  \rvert \leq 0.0$
    + [C] $\lvert y-z  \rvert \leq 0.5$
    + [D] $\lvert x+y  \rvert \leq 0.5$

---
```c=
int Q[30] ;
int count = 0 ;
int front = 0 ;
int back = 0 ;
for ( int i = 1; i <= 15; i = i + 1 ) 
{
    Q[ back ] = i ;
    back = back + 1 ;
}
while ( front < back-1 )
{
    int v = Q[ front ] ;
    front = front + 1 ;
    count = count + 1 ;
    if ( count == 3 ) 
    {
        count = 0 ;
        Q[ back ] = v ;
        back = back + 1 ;
    }
}
printf("%d", Q[front] ) ;
```
06. 以下選項敘述何者正確？
    + [A] 6
    + [B] 9
    + [C] 12
    + [D] 15
   
---
```c=
int a[10] = { 10, 9, 7, 2, 3, 4, 5, 6, 1, 8 } ;
int b[10] = { 2, 1, 3, 4, 5, 6, 7, 2, 4, 5 } ;
for (int i = 0 ; i < 10 ; i = i + 1 ) {
    a[ b[i] - 1 ] = b[ a[i] ] + 1 ;
}
printf("%d", a[7]);
```
07. 以下選項敘述何者正確？
    + [A] 輸出 2
    + [B] 輸出 6
    + [C] a[ ] 的索引值錯誤
    + [D] b[ ] 的索引值錯誤

---
```c=
int f(int n){
    int A[1000] = { } ;
    for ( int i = 1 ; i != n ; i = i + 2 ) {
        A[i] = 1;
    }
}
```
08. 當 n 設定為多少時迴圈才能停下來？
    + [A] 1000
    + [B] 999
    + [C] 0
    + [D] -1

---
```c=
int sum( int n ) {
    if (__________) {
        return __________ ;
    }
    return n + sum( n - 1 ) ;
}
```
09. 停止條件應該如何設定才能計算出 1 + 2 + 3 + ... + n ?
    + [A] $n\leq 1 , n$ 
    + [B] $n\leq 0 , n$ 
    + [C] $n\leq-1 , n+1$ 
    + [D] 以上條件都可以

---
```c=
int a[21] = { } ;
for ( int i = 3 ; i <= 20 ; i = i + 1 )
{
    for ( int j = i * i ; j <= 20 ; j = j + i )
    {
        a[ j ] = 1 ;
    }
}
int k = 0 ;
for ( int i = 0 ; i <= 20 ; i = i + 1 )
{
    k = k + a[ i ]  ;
}
```
10. 執行完上述程式碼後，k的值應該符合下列哪個選項
    + [A] $1 \leq k \leq 5$ 
    + [B] $6 \leq k \leq 10$ 
    + [C] $11 \leq k \leq 15$ 
    + [D] $16 \leq k \leq 20$

---
```c=
typedef struct student_info
{
    int math, chinese, english ;
} std[20] ;
int main () 
{
    int n = 20 ;
    int best_ID = 0 ;
    int best_sum = std[0].math + std[0].chinese + std[0].english ;
    for ( int i = 1 ; i < n ; i = i + 1 ) 
    {
        int sum = std[i].math + std[i].chinese + std[i].english ; 
        if ( sum > best_sum ) 
        {
            best_ID = i ;
            best_sum = sum ;
        }
        else if ( ___________ )
        {
            best_ID = i ;
        }
    }
}
```
11. 為了找到 n 位學生的成績當中成績最好的，先比總分高的，若總分相同時則比照數學成績，數學成績相同時則依照學號較小的為主，下列的判斷式應該如何選擇才能保證達成條件？
    + [A] sum >= best_sum && std[ i ].math >= std[ best_ID ].math
    + [B] sum >= best_sum && std[ i ].math >  std[ best_ID ].math
    + [C] sum > best_sum  && std[ i ].math >  std[ best_ID ].math
    + [D] sum > best_sum  && std[ i ].math >= std[ best_ID ].math


---
```c=
int f() 
{
    int a[8] = { 8, 7, 6, 5, 1, 2, 3, 4 } ;
    for (int i = 7 ; 0 < i ; i = i - 1 ) 
    {
        for (int j = 0 ; j < i ; j = j + 1) 
        {
            if ( a[j+1] >= a[j] ) 
            {
                int temp = a[j+1] ;
                a[j+1] = a[j] ;
                a[j] = temp ;
            }
        }
    }
}
```
12. 輸出 a[ ] 執行完後的數字？
    + [A] = { 8, 7, 6, 5, 1, 2, 3, 4 } 
    + [B] = { 8, 7, 6, 5, 4, 3, 2, 1 } 
    + [C] = { 1, 2, 3, 4, 5, 6, 7, 8 } 
    + [D] = { 5, 6, 7, 8, 1, 2, 3, 4 } 

---
```c=
int f ( int n ) 
{
    if ( n >= 101 ) 
    {
        return n - 10 ;
    }
    return f( f( n + 11 ) ) ;
}
```
13. 上述程式碼執行 f(5) 和 f(99) 之後的值各是多少？
    + [A] f(5) = 91,  f(99) = 91
    + [B] f(5) = 94,  f(99) = 91
    + [C] f(5) = 91,  f(99) = 99
    + [D] f(5) = 94,  f(99) = 99

---
```c=
int i = 5 ;
int main()
{
    while ( i )
    {
        i = i - 1 ;
        main() ;
        printf( "%d ", i ) ;
    }
    return 0 ;
}
```
14. 上述程式碼執行後會輸出的結果？
    + [A] 0 0 0 0 0
    + [B] 1 2 3 4 5
    + [C] 5 4 3 2 1
    + [D] 4 3 2 1 0