作者dibery (Bor)
看板NCCU_Exam
標題[試題] 1001 蔡銘峰 計算機程式設計 期中考
時間Tue Jul 10 11:15:40 2012
課程名稱:計算機程式設計(一)
課程性質:必修
課程範圍:1~7章 (introduction ~ pointer)
開課教師:蔡銘峰
開課學院:理學院
開課系級:資科一
考試日期(年月日):2011/11/16
考試時限(Mins):180分
附註:
試題本文:
1. Basic Unix Commands [5%] ( 1 point for each )
Write Unix command for the following situations:
a) Create a directory named nccucs
b) Rename a file from a.txt to b.txt
c) Display the line-by-line difference between MRU.txt and GTR.txt
d) Display the on-line manual pages about the cp command
e) List all the files whose filename starts with assign, and store the
result into a file named result.txt
2. Basic C statements [12%] ( 2 points for each )
Write a "single" C statement to accomplish each of the following: (Note that
in C each statement should end with a semicolon.)
a) Define the variables
c, thisVariable, q76354 and
number to be of
type int.
b) Print "The product is" followed by the value of the integer
variable
result.
c) Define a variable
x and initialize the variable to 1.
d) Add variable
x to variable
sum and assign the result to variable
sum
e) Cauculate the reminder after
q is divided by
divisor and assign the
result to
q.
f) Print the value 123.4567 with 2 digits of precision.
3. Write down the output of the following programs.
a)
#include <stdio.h>
int main(){
int i;
for( i = 1; i <= 100; i++ ){
if( i % 9 != 0 ) continue;
if( i >= 80 ) break;
printf("%d", i);
}
printf("\n");
return 0;
}
b)
#include <stdio.h>
int main() {
int i = 20;
while (1) {
switch(i % 2){
case 0:
printf("even");
break;
case 1:
printf("odd");
break;
default:
printf("Not a number!");
}
i -= 2;
if (i < 10) break;
}
printf("\n");
return 0;
}
4. Operator Priority and Logical Operations [10%] (5 points for each)
Please write down the values of each variable in the following programs:
a) What are the values of i, j, k, x, y, and z? (1 point for each answer)
#include <stdio.h>
int main(){
int i = 2;
int j = 3;
int k = 4;
int x = i+++j;
int y = 0;
if(j=k<<1)
y = j + k;
else
y = k - k;
int z = k + (k+=1);
printf("i=%d,j=%d,k=%d,x=%d,y=%d,z=%d\n",i,j,k,x,y,z);
return 0;
}
b) What are the values of x and y? (2.5 points for each answer)
#include <stdio.h>
int main(){
int a = 4, b = 7, x = 0, y = 0;
x = a & b;
if(a && b)
y = (!a||!b)+(a&&!b);
else
y = (!a||b)+(!a&&b);
return 0;
}
5. Function Prototype [8%] (2points for each)
Write down the function prototype for each of the following: (Note that a
function prototype should end with a semicolon.)
a) Fuction
hypotenuse that takes two double arguments:
side1 and
side2, and
returns a double result
b) Fuction
smallest that taskes three integers:
x, y, z, and returns an
integer
c) Function
instructions that does not receive any arguments and does
not return a value.
d) Function
intToFloat that takes an integer argument:
number, and returns
a floating-point result
6. Call-by-Value and Call-by-Reference [12%]
What is the result of the following program? (3 points for each answer)
#include <stdio.h>
void callBy Value( int number ){
number = number * 10;
}
void callByReference( int *nPtr ) {
*nPtr = *nPtr * *nPtr * *nPtr;
}
int main( void ) {
int number = 2;
callByValue( number );
printf( "%d ", number );
callByReference( &number );
printf( "%d ", number );
callByValue( number );
printf( "%d ", number );
callByReference( &number );
printf( "%d\n", number );
return 0;
}
7. Write down the output of the program:
#include<stdio.h>
int love, hate;
int fp(int x){
int life=1;
love++; hate=x+1;
life+=love+hate; x++;
return life;
}
int f2(int x){
static int life=1;
love++; hate=x+1;
life+=love+hate; x++;
return life;
}
int main(){
int life;
love=hate=1;
life=fp(love); printf("%3d %3d %3d\n", life, love, hate);
life=fp(love); printf("%3d %3d %3d\n", life, love, hate);
love=hate=1;
life=fp(love); printf("%3d %3d %3d\n", life, love, hate);
life=fp(love); printf("%3d %3d %3d\n", life, love, hate);
life=fp(love+1); printf("%3d %3d %3d\n", life, love, hate);
return 0;
}
8. Pointer [8%] (2 points for each)
#include<stdio.h>
int main(){
int *a;
int b = 2, c = 5;
a = &c;
b = *a;
int *d = &b;
return 0;
}
Suppose that the address of
b is at 3000 and that of
e at 4000. What is the
result of following questions? (Note that the space of an integer is 4 byte)
a) ++*a = ?
b) (++b)*2 = ?
c) (c+2) = ?
d) (d+1) = ?
9. Pointer and Array [12%] (2 pts for each)
An array of integers is stored starting at address 1000.
int arr[] = {12, 15, 32, 14, 66};
/* the address of the first in in the array is 1000*/
/* arr:[1000] ---> [12][15][32][14][66] */
What is the value pruduced by each of the following expressions. The value
will either be an address like 1004 or an integer value inside the array,
like 15 or 66 etc. (Note that the space of an integer is 4 byte.)
a) arr + 1 = ?
b) arr + 3 = ?
c) arr[2] = ?
d) &arr[2] = ?
e) *arr = ?
f) *(arr + 3) = ?
10. Pointer and Swap [10%] (1 point for each blank)
Add * and & to the following code so that it implements a swap function. At
the end of the function, x should contain y, and y should contain x, and
those changes should remain when the function returns to main. Then show a
function call to swap in the main function provided.
#include <stdio.h>
void swap (int ___x, int ___y)
{
int ___temp=___x;
___x=___y;
___y=___temp;
}
int main()
{
int x=3, y=4;
swap(___x, ___y);
return 0;
}
11. const Qualifer [5%] Bonus
Identify the errors in the following program, and explain the reason of
causing the error in details.
1. #include <stdio.h>
2. int main( void ) {
3. int x = 5;
4. int y;
5. const int *const ptr = &x;
6. printf( "%d\n", *ptr );
7. *ptr = 7;
8. ptr = &y;
9. return 0;
10. }
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.119.27.46
※ 編輯: dibery 來自: 140.119.27.46 (07/10 11:16)
※ 編輯: dibery 來自: 140.119.27.46 (07/10 11:28)
※ 編輯: dibery 來自: 140.119.27.46 (07/10 11:29)