当前位置:嗨网首页>书籍在线阅读

34-复习题

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

6.15 复习题

复习题的参考答案在附录A中。

1.写出执行完下列各行后 quack 的值是多少。后5行中使用的是前一行生成的 quack 的值。

int quack = 2;
quack += 5;
quack *= 10;
quack -= 6;
quack /= 8;
quack %= 3;

2.假设 valueint 类型,下面循环的输出是什么?

for ( value = 36; value > 0; value /= 2)
       printf("%3d", value);

如果 valuedouble 类型,会出现什么问题?

3.用代码表示以下测试条件:

a. x 大于 5

b. scanf() 读取一个名为 xdouble 类型值且失败

c. x 的值等于 5

4.用代码表示以下测试条件:

a. scanf() 成功读入一个整数

b. x 不等于 5

c. x 大于或等于 20

5.下面的程序有点问题,请找出问题所在。

#include <stdio.h>
int main(void)
{                                       /* 第3行 */
     int i, j, list(10);                /* 第4行 */
     for (i = 1, i <= 10, i++)          /* 第6行 */
     {                                  /* 第7行 */
          list[i] = 2*i + 3;            /* 第8行 */
          for (j = 1, j > = i, j++)     /* 第9行 */
               printf(" %d", list[j]);  /* 第10行 */
          printf("\n");                 /* 第11行 */
}                                       /* 第12行 */

6.编写一个程序打印下面的图案,要求使用嵌套循环:

$$$$$$$$
$$$$$$$$
$$$$$$$$
$$$$$$$$

7.下面的程序各打印什么内容?

a.

#include <stdio.h>
int main(void)
{
     int i = 0;
     while (++i < 4)
          printf("Hi! ");
     do
          printf("Bye! ");
     while (i++ < 8);
     return 0;
}

b.

#include <stdio.h>
int main(void)
{
     int i;
     char ch;
     for (i = 0, ch = 'A'; i < 4; i++, ch += 2 * i)
             printf("%c", ch);
     return 0;
}

8.假设用户输入的是 Go west, young man! ,下面各程序的输出是什么?(在ASCII码中, ! 紧跟在空格字符后面)

a.

#include <stdio.h>
int main(void)
{
     char ch;
     scanf("%c", &ch);
     while (ch != 'g')
     {
          printf("%c", ch);
          scanf("%c", &ch);
     }
     return 0;
}

b.

#include <stdio.h>
int main(void)
{
     char ch;
     scanf("%c", &ch);
     while (ch != 'g')
     {
          printf("%c", ++ch);
          scanf("%c", &ch);
     }
     return 0;
}

c.

#include <stdio.h>
int main(void)
{
     char ch;
     do {
          scanf("%c", &ch);
          printf("%c", ch);
     } while (ch != 'g');
     return 0;
}

d.

#include <stdio.h>
int main(void)
{
     char ch;
     scanf("%c", &ch);
     for (ch = '$'; ch != 'g'; scanf("%c", &ch))
          printf("%c", ch);
     return 0;
}

9.下面的程序打印什么内容?

#include <stdio.h>
int main(void)
{
     int n, m;
     n = 30;
     while (++n <= 33)
          printf("%d|", n);
     n = 30;
     do
          printf("%d|", n);
     while (++n <= 33);
     printf("\n***\n");
     for (n = 1; n*n < 200; n += 4)
          printf("%d\n", n);
     printf("\n***\n");
     for (n = 2, m = 6; n < m; n *= 2, m += 2)
          printf("%d %d\n", n, m);
     printf("\n***\n");
     for (n = 5; n > 0; n--)
     {
          for (m = 0; m <= n; m++)
               printf("=");
          printf("\n");
     }
     return 0;
}

10.考虑下面的声明:

double mint[10];

a.数组名是什么?

b.该数组有多少个元素?

c.每个元素可以存储什么类型的值?

d.下面的哪一个 scanf() 的用法正确?

i. scanf("%lf", mint[2])

ii. scanf("%lf", &mint[2])

iii. scanf("%lf", &mint)

11.Noah先生喜欢以2计数,所以编写了下面的程序,创建了一个存储2、4、6、8等数字的数组。这个程序是否有错误之处?如果有,请指出。

#include <stdio.h>
#define SIZE 8
int main(void)
{
     int by_twos[SIZE];
     int index;
     for (index = 1; index <= SIZE; index++)
           by_twos[index] = 2 * index;
     for (index = 1; index <= SIZE; index++)
           printf("%d ", by_twos);
     printf("\n");
     return 0;
}

12.假设要编写一个返回 long 类型值的函数,函数定义中应包含什么?

13.定义一个函数,接受一个 int 类型的参数,并以 long 类型返回参数的平方值。

14.下面的程序打印什么内容?

#include <stdio.h>
int main(void)
{
     int k;
     for (k = 1, printf("%d: Hi!\n", k); printf("k = %d\n", k),
          k*k < 26; k += 2, printf("Now k is %d\n", k))
               printf("k is %d in the loop\n", k);
     return 0;
}