搜档网
当前位置:搜档网 › C语言考试题及答案

C语言考试题及答案

C语言考试题及答案
C语言考试题及答案

一、单项选择题:(10分,每题2分)

1.char *p[10];该语句声明了一个: c 。

A)指向含有10个元素的一维字符型数组的指针变量p

B)指向长度不超过10的字符串的指针变量p

C)有10个元素的指针数组p,每个元素可以指向一个字符串

D) 有10个元素的指针数组p,每个元素存放一个字符串

2.若int x;且有下面的程序片断,则输出结果为: D 。

for (x=3; x<6; x++)

{

printf((x%2) ? "##%d" : "**%d\n", x);

}

A) ##3B) **3C) **3D)##3**4

**4 ##4 ##4**5 ##5

##5 **5

3.在while(!x)语句中的!x与下面条件表达式等价的是:D 。

A) x!=0 B) x==1 C) x!=1 D) x==0 4.已知

struct point

{

int x;

int y;

};

struct rect

{

struct point pt1;

struct point pt2;

};

struct rect rt;

struct rect *rp = &rt;

则下面哪一种引用是不正确的__D______。

A) r t.pt1.x B) (*rp).pt1.x

C) r p->pt1.x D) rt->pt1.x

5.若二维数组a有m行n列,则下面能够正确引用元素a[i][j]的为: C 。

A) *(a+j*n+i) B) *(a+i*n+j)

C) *(*(a+i)+j) D) *(*a+i)+j

CDDDC

二、分析程序并写出运行结果。(25分,每题5分)

1.

#include

main()

int n;

static char *monthName[]=

{"Illegal month", "January", "February", "March", "April", "May", "June", "July", "August",

"September", "October", "November", "December"};

for (n=1; n<=12; n++)

{

printf("%s\n", monthName[n]);

}

}

运行结果是:

January

February

March

April

May

June

July

August

September

October

November

December

2.

#include

#define ARR_SIZE 7

void YH(int a[][ARR_SIZE], int n);

void PrintYH(int a[][ARR_SIZE], int n);

main()

{

int a[ARR_SIZE][ARR_SIZE];

YH(a, ARR_SIZE-1);

PrintYH(a, ARR_SIZE-1);

}

void YH(int a[][ARR_SIZE], int n)

{

int i, j;

for (i=1; i<=n; i++)

{

for (j=1; j<=i; j++)

{

if (j==1 || i==j)

{

a[i][j] = 1;

}

else

{

a[i][j] = a[i-1][j-1] + a[i-1][j]; }

}

}

}

void PrintYH(int a[][ARR_SIZE], int n)

{

int i , j ;

for (i=1; i<=n; i++)

{

for (j=1; j<=i; j++)

{

printf("%4d", a[i][j]);

}

printf("\n");

}

运行结果是:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

3.

#include

main()

{

int i, n;

for (i=1; i<=5; i++)

{

printf("Please enter n:");

scanf("%d", &n);

if (n <= 0) continue;

printf("n = %d\n", n);

}

printf("Program is over!\n");

}

程序运行时输入:1 -2 3 -4 5↙

运行结果是:

n = 1

Please enter n: Please enter n:n = 3 Please enter n: Please enter n:n = 5 Program is over!

4.

#include

void Func(int n);

main()

{

int i;

for (i = 0; i < 2; i++)

{

Func(i);

}

}

void Func(int n)

{

static int t = 1;

printf("t=%d\n", t++);

}

运行结果是:

t=1

t=2

5.

#include

int Func(int i);

main()

{

int i;

for (i=3; i<5; i++)

{

printf(" %d", Func(i));

}

printf("\n");

}

int Func(int i)

{

static int k = 10;

for (; i>0; i--)

{

k++;

}

return (k);

}

运行结果是:

13 17

三、阅读并完成程序,在标有下划线的空白处填入适当的表达式或语句,使程序完整并符合题目要求。(20分,每空2分)

1. 下面程序用于读入10个字符串,然后找出最大的字符串并打印。

#include

#include

main()

{

char str[10][80],maxstring[80];

int i;

printf("Enter ten strings:\n");

for (i=0; i<10; i++)

{

scanf("%s", str[i] );

}

strcpy(maxstring, str[0]);

for (i=1; i<10; i++)

{

if (strcmp(maxstring, str[i])<0)或strcmp(str[i], maxstring)>0

{

strcpy(maxstring, str[i]);

}

}

printf("The max string is:%s\n",maxstring);

}

2. 下面这个程序用于交换两个数组的对应元素的值。

#include

#define ARRAY_SIZE 10

void Swap(int *x, int *y);

void Exchange(int a[], int b[], int n);

void InputArray(int a[],int n);

void PrintArray(int a[],int n);

main()

{

int a[ARRAY_SIZE], b[ARRAY_SIZE], n;

printf("Input array lenth n<=10: ");

scanf("%d", &n);

printf("Input array a:\n");

InputArray(a, n);

printf("Input array b:\n");

InputArray(b, n);

Exchange(a,b,n);

printf("After swap:\n");

printf("Array a:\n");

PrintArray(a, n);

printf("Array b:\n");

相关主题