您的位置: 网站首页 > 程序开发 > C语言程序设计案例教程 > 第四章 选择结构程序设计 > 【4.4 思考与练习】

4.4 思考与练习

 

思考与练习

1选择题

1)两次运行以下程序时,如果从键盘上分别输入64,则输出结果是     

#include <stdio.h>

main()

{

    int x;

    scanf("%d",&x);

    if(x++>5) printf("%d",x);

    else printf("%d\n",x--);

}

A75                                                        B63

C74                                                        D64

2)若kint型变量,则以下程序片段的输出结果是     

k=-3;

if(k<=0) printf("####");

else  printf("&&&&");

A####                                                         B&&&&

C####&&&&                                              D.有语法错误,无输出结果

3)若执行以下程序时,从键盘上输入2.0<CR>,则输出结果是      

#include <stdio.h>

main()

{

    float x,y;

 

    scanf("%f",&x);

    if(x<0.0) y=0.0;

    else if((x<5.0)&&(x!=2.0))

                y=1.0/(x+2.0);

            else if(x<10.0) y=1.0/x;

            else y=10.0;

    printf("%f\n",y);

}

A0.000000                                                   B0.250000

C0.500000                                                   D1.000000

4)以下程序的输出结果是     

#include <stdio.h>

main()

{

    int x=1,y=0,a=0,b=0;

    switch(x)

    {

        case 1:

            switch(y)

            {

                case 0: a++; break;

                case 1: b++; break;

            }

        case 2:

            a++;

            b++;

            break;

    }

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

}

Aa=2, b=1                                                    Ba=1, b=1

Ca=1, b=0                                                    Da=2, b=2

5)若执行下以下程序时,从键盘上输入34,则输出结果是     

#include <stdio.h>

main()

{

    int a,b,s;

    scanf("%d %d",&a,&b);

    s=a;

    if(a<b) s=b;

    s=s*s;

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

}

A14                          B16                          C18                   D20

6)关于以下程序,说法正确的是     

#include <stdio.h>

main()

{

    int x=3,y=0,z=0;

    if(x=y+z)  printf("* * * *");

    else  printf("# # # #");

}

A.有语法错误不能通过编译

B.输出* * * *

C.可以通过编译,但是不能通过连接,因而不能运行

D.输出# # # #

7)若执行以下程序时,从键盘上输入5,则输出结果是     

#include <stdio.h>

main()

{

    int x;

    scanf("%d",&x);

    if(x++>5) printf("%d\n",x);

    else printf("%d\n",x--);

}

A7                            B6                            C5                     D4

8)以下程序的输出结果是     

#include <stdio.h>

main()

{

    int x=100,a=10,b=20,ok1=5,ok2=0;

    if(a<b)

        if(b!=15)

            if(!ok1)

                x=1;

            else

            if(ok2)x=10;

    x=-1;

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

}

A-1                                                             B0

C1                                                               D.不确定的值

9)若要求在if后一对圆括号中表示a不等于0的关系,则能正确表示这一关系的表达式为     

Aa<>0                      B!a                           Ca=0                 Da

10)以下程序的输出结果是     

#include <stdio.h>

main()

{

    int a=-1,b=4,k;

    k=(++a<0) && !(b--<=0);

    printf("%d%d%d\n",k,a,b);    

}

A104                      B103                        C003                 D004

11)表达式10!=9的值是     

Atrue                      B.非0                   C0                     D1

12)能正确表示a10a0的关系表达式是     

Aa>=10 or a<=0                                         Ba>=10 | a<=0

Ca>=10 || a<=0                                           Da>=10 || a<=0

13)设a为整型变量,      不能正确表达数学关系10<a<15

A!(a<=10)||!(a>=15)                                          Ba>10&&a<15

C!(a<=10||a>=15)                                       D!(a<=10)&&!(a>=15)

14)若有以下定义:

float x; int a,b;

则正确的switch语句是     

Aswitch(x)                                               Bswitch(x)

          {                                      { 

          case1.0: printf("*\n");                case1,2: printf("*\n");

          case2.0: printf("**\n");               case3: printf("**\n");

          }                                      }

Cswitch (a+b)                                          Dswitch (a+b);

          {                                      {

          case 1: printf("\n");                  case 1:printf("*\n");

          case 1+2: printf("**\n");              case 2:printf("**\n");

          }                                      }

15)设所有变量均已正确说明,以下程序片段运行后,x的值是     

a=b=c=0;

x=35;

if(!a) x--;

else if(b);

if(c) x=3;

else x=4;

A34                        B4                            C35                   D3

16)能正确表示关系x<=y<=zC语言表达式是     

A(x<=y)&&(y<=z)                                     B(x<=y)and(y<=z)

C(x<=y<=z)                                               D(x<=y)&(y<=z)

17)以下程序的输出结果是     

#include <stdio.h>

main()

{

    int m=5;

    if(m++>5)  printf("%d\n",m);

    esle  printf("%d\n",m--);

}

A7                          B6                            C5                     D4

18)当a=1b=3c=5d=5时,执行以下程序片段后,x的值为     

if(a<b)

    if(c<d)  x=1;

    else

        if(a<c)

            if(b<d) x=2;

            else   x=3;

        else   x=6;

else   x=7;

A1                          B2                            C3                     D6

19)设有如下定义:int x=l,y=-1;,则语句printf("%d\n",(x--&++y));的输出结果是

      

A1                          B0                            C-1                   D2

20)当c的值不为0时,以下选项中,能正确将c的值赋给变量ab的是     

Ac=b=a;                                                     B(a=c) || (b=c) ;

C(a=c) &&(b=c);                                        Da=c=b;

21)语句printf("%d \n"12 &012);的输出结果是     

A12                        B8                            C6                     D012

22)能正确表示ab同时为正或同时为负的逻辑表达式是     

A(a>=0b>=0)&&(a<0b<0)                   B(a>=0&&b>=0)&&(a<0&&b<0)

C(a+b>0)&&(a+b<=0)                                Da*b>0

23)以下程序的输出结果是     

#include <stdio.h>

main()

{  

    int a=-1,b=1,k;

    if((++a<0) && !(b--<=0))

        printf("%d  %d\n",a,b);

    else

        printf("%d  %d\n",b,a);

}

A-1  1                  B0  1                      C1  0               D0  0

24)与语句y=(x>0?1:x<0?-1:0);的功能相同的if语句是     

Aif(x>0) y=1;                                         Bif(x)

         else if(x<0) y=-1;                      if(x>0) y=1;

         else y=0;                               else if(x<0) y=-1;

                                                else y=0;

Cy=-1                                                        Dy=0;

        if(x)                                   if(x>=0)

        if(x>0) y=1;                                if(x>0) y=1;

            else if(x==0) y=0;                      else y=-1;

            else y=-1;

25)若有定义语句float w;int a, b;,则正确的switch语句是     

Aswitch(w)                                               Bswitch(a);

        {   case 1.0: printf("*\n");        {   case 1 printf("*\n");

            case 2.0: printf("**\n");            case 2 printf("**\n");

        }                                       }

Cswitch(b)                              Dswitch(a+b);

        {   case 1:   printf("*\n");             {   case 1: printf("*\n");

            default:  printf("\n");                  case 2: printf("**\n");

            case 1+2: printf("**\n");                default: printf("\n");

        }                                       }

26)能正确表示逻辑关系a=10a0C语言表达式是     

Aa>=10 or a<=0                                         Ba>=0|a<=10   

Ca>=10 &&a<=0                                        Da>=10a<=0

27)以下程序的输出结果     

#include <stdio.h>

main()

{   

    int x=1,a=0,b=0;

    switch(x)

    {

        case 0:  b++;

        case 1:  a++;

        case 2:  a++; b++;

    }

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

}

Aa=2,b=1                                                   Ba=1,b=1

Ca=1,b=0                                                   Da=2,b=2

28)以下程序的输出结果是     

#include <stdio.h>

main()

{    

    float x=2.0,y;

    if(x<0.0) y=0.0;

    else if(x<10.0) y=1.0/x;

          else y=1.0;

    printf("%f\n",y);

}

A0.000000                     B0.250000                C0.500000         D1.000000

29)以下程序的输出结果是     

#include <stdio.h>

main()

{   

    int a=2,b=-1,c=2;

    if(a<b)

        if(b<0) c=0;

    else c++;

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

}

A0                          B1                            C2                     D3

30)执行以下程序片段后,x的值为     

int a=14,b=15,x;

char c='A';

x=(a&&b)&&(c<'B');

Ature                      Bfalse                       C0                     D1

31xyt均为int型变量,则执行语句x=y=3; t=++x||++y;后,y的值为     

A.不定值                 B4                            C3                     D1

32)若执行以下程序时,从键盘上输入9,则输出结果是     

#include <stdio.h>

main()

{ 

    int  n;

    scanf("%d",&n);

    if(n++<10) printf("%d\n",n);

    else printf("%d\n",n--);

}

A11                        B10                          C9                     D8

33)若abc1c2xy均为整型变量,则正确的switch语句是     

Aswich(a+b);                                           Bswitch(a*a+b*b)   

        {                                       {

            case 1: y=a+b; break;                    case 3:   

            case 0: y=a-b; break;                    case 1: y=a+b; break;

        }                                           case 3: y=b-a; break;

                                                }

Cswitch(a-b)                                           Dswitch a

        {                                       {

            default: y=a*b; break;                   case c1: y=a-b; break;

            case 3: case 4: x=a+b; break;            case c2: x=a*d; break;

            case 7: case 9: y=a-b; break;            default:x=a+b;

        }                                       }

34)若变量cchar型,能正确判断出c为小写字母的表达式是      

A'a'<=c<= 'z'                                               B(c>= 'a')||(c<= 'z')

C('a'<=c)and ('z'>=c)                                    D(c>= 'a')&&(c<= 'z')

35)设abcdmn均为int型变量,且a=5b=6c=7d=8m=2n=2,则运算逻辑表达式(m=a>b)&&(n=c>d)后,n的值为     

A0                          B1                            C2                     D3

36)若执行以下程序时,从键盘上输人5,则输出结果是     

#include <stdio.h>

main()

{  

    int x;

    scanf("%d",&x);

    if(x--<5) printf("%d",x);

    else printf("%d",x++);

}

A3                          B4                            C5                     D6

37)设wxyzm均为int型变量,以下程序片段运行后,m的值是     

w=1;  x=2;  y=3; z=4;

m=(w<x)?w:x;    m=(m<y)?m:y;    m=(m<z)?m:z;

A4                          B3                            C2                     D2

38)整型变量xy的值相等、且为非0值,则以下选项中,结果为零的表达式是

     

Ax || y                    Bx | y                       Cx & y                     Dx ^ y

39)以下程序的输出结果是     

#include <stdio.h>

main()

{ 

    int  a=5,b=4,c=6,d;

    printf("%d\n",d=a>b?(a>c?a:c):b);

}

A5                          B4                            C6                     D.不确定

40)以下程序的输出结果是     

#include <stdio.h>

main()

{

    int a=4,b=5,c=0,d;

    d=!a&&!b||!c;

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

}

A1                          B0                            C.非0            D-1

41)以下程序的输出结果是     

#include <stdio.h>

main()

{  

    int a=5,b=4,c=3,d;

    d=(a>b>c);

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

}

A0                          B1                            C2                     D3

42)设已有定义语句int x=3,y=4,z=5;,则表达式!(x+y)+z-1 && y+z/2的值是     

A6                          B0                            C2                     D1

43)以下程序的输出结果是     

#include <stdio.h>

main()

{  

    int a=15,b=21,m=0;

    switch(a%3)

    {

        case 0: m++; break;

        case 1: m++;

                switch(b%2)

                {

                    default: m++;

                    case 0: m++; break;

                }

    }

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

}

A1                          B2                            C3                     D4

2上机操作题

1)编写一个程序,输入三个整数,按由大到小的顺序输出这三个数。

2)编写一个程序,要求用户输入运算数和四则运算符,输出计算结果。

3)编写一个程序,求三个整数中的最大值。

4)利用如下函数编写一个程序,要求输入一个x值,输出y值。