課程名稱︰計算機程式
課程性質︰系定必修
課程教師︰廖世偉
開課學院:工學院
開課系所︰化工系
考試日期(年月日)︰96.11.12
考試時限(分鐘):125分鐘
是否需發放獎勵金:是
(如未明確表示,則不予發放)
試題 :
Programming Language 2007 Fall
Midterm Exam
1.Multiple choice (30%)
(a)If j=5, k=2, what are the values of n after execution of this statement?
n=j-++k;
(1)1(2)2(3)3(4)4
(b)If x=3, y=5 and z=2. What are the values of x,y and z after execution of
this statement?
x*=y+z;
y/=2*z+1;
z+=x;
(1)x=20,y=1,z=22(2)x=20,y=0,z=23(3)x=21,y=0,z=23(4)x=21,y=1,z=23
(c)Switch statement may also be used in C to select one of several
alternatives. Which one could exit the switch case immediately?
(1)quit(2)break(3)leave(4)go home
(d)Evaluate each of the following expressions if a is 5, b is 10, c is 15 and
flag is 1. Which parts of these expressions are not evaluated due to
short-circuit evaluation?
(1)c= =a+b||!flag
(2)a!=7&&flag||c>=6
(3)!(b<=12)&&a%2= =0
(4)!(a>5||c<a+b)
(e)How many bytes does the array occupy?
char test[8]={'a','b','c','d','e','f','g','h'}
(1)4Bytes(2)8Bytes(3)16Bytes(4)32Bytes
(f)What is the range of int data type?
(1)0~32767(2)-32767~32767(3)-32768~32768(4)-32768~32767
(g)Num is a double data type. If Num=3.12159, after execute this statement
Num=(int)(Num+0.5)
(1)3.54159(2)3(3)4(4)3.5
(h)If you want use "system("pause")" statement, then which header file you
should use?
(1)stdlib.h(2)math.h(3)system.h(4)stdio.h
(i)If p is a pointer, it point to variable y, which statement is correct?
(1)*p=y(2)p=*y(3)p=&y(4)y=&y
(j)What is the output following the program?
#include<stdio.h>
void main(){
int i=1;
i++;
func();
printf("%d\n",i);
}
void func(){
int i=1;
i--;
printf("%d",i);
}
(1)0,0(2)0,1(3)1,0(4)1,1
2.(10%)Executable statements:
Input a is an integer data type and b is floating point data, the procedure
are as follow, please fill blank.
int a;
float b;
scanf("____,____",____,____);
printf("a=____,b=____",a,b);
3.(10%)Write down an infinite loop using while and for loop.
4.(15%)loop and nested-loop (evaluate n after loop)
(i)
int n=0;
for(int x=0;x<10;x++)
n++;
(ii)
int n=0;
for(int x=0;x<10;x++)
for(int y=0;y<10;y++)
n++;
(iii)
int n=0;
for(int x=0;x<10;x++)
for(int y=x;y<10;y++)
n++;
5.(10%)Condition
int key=1;
int flag[2]={1,0};
if(key && flag[1])
printf("A\n");
else if(key && flag[0])
printf("B\n");
else
printf("C");
What is the result on your screen?
6.(10%)Programming Knowledge
Please correct the program,(do not add more than 3 statement)
Rule:
(1)If you input a, then program output Function a.
(2)If you input b, then program output Function b.
(3)Other Plz key in a or b.
void main{
char option
scanf(%c,&option)
switch [option];
{
case 'a' : printf("Function a\n");
case 'b' : printf("Function b\n");
default : printf("Plz key in a or b\n");
return 0;
}
7.(15%)Function call
Write a fibonacci program using function.
[hint]f(n)=f(n-1)+f(n-2),n>=2,f(0)=0,f(1)=1.
int fib(int x);
void main(){
int result=fib(5); //call fibonacci function
}
8.(10%)pointer and array
Write a swap function. The detail are as follow.
int main()
{
int a=1,b=2;
swap(&a,&b);
printf("a=%d b=%d\n",a,b);
}
void swap(int *a,int *b)
{
????????????????????
}
9.(20%)pointer and array
Strings are important because many computer applications are concerned with
the manipulation of textual data rather than numerical data. In C programming
language, the string data structure is implemented using char arrays. The
programmer can process strings easily using C standard library string.h. For
example: strcpy(), strcmp(), etc. In this part, you have to implement them by
yourself.
Function 1:myStrcpy(char *dest,char *src)
The function copies the string pointed to be src(including the
terminating '\0' character) to the array pointed to by dest.
[Requirement]
˙The main function:
int main(){
char str1[80]="Hello!";
char str2[80]="My name is Joe";
char str3[80]=" ";
myStrcpy(str3,str1);
printf("%s \n",str3);
return 0;
}
10.(20%)Advanced Topic
In this part, you have to write a program to generate the calendar of the
month specified be the user after 1990. Given that January 1st of 1990 is
Monday, you need to print the calendar as follow. For example, if November
2006 is specified.
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
Note that the leap year should be taken into consideration. The leap years
are those divisible by 4 except those are divisible by 100 but not by 400.
For example, 2000 is a leap year but 1900 is not.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.169.49.37