Review Questions

I l @ ve RuBoard

Review Questions

  1. Determine which expressions are true and which are false.

    1. 100 > 3 && `a'>`c'

    2. 100 > 3 `a'>'c'

    3. !(100>3)

  2. Construct an expression to express the following conditions:

    1. number is equal to or greater than 1 but smaller than 9.

    2. ch is not a q or a k character.

    3. number is between 1 and 9 but is not a 5.

    4. number is not between 1 and 9.

  3. The following program has unnecessarily complex relational expressions as well as some outright errors. Simplify and correct it.

     #include <stdio.h> int main(void)                                      /* 1  */ {                                                   /* 2  */   int weight, height;  /* weight in lbs, height in inches */                                                     /* 4  */   scanf("%d, weight, height);                       /* 5  */   if (weight < 100)                                 /* 6  */      if (height >= 72)                              /* 7  */         printf("You are very tall for your weight.\n");      else if (height < 72 && > 64)                  /* 9  */         printf("You are tall for your weight.\n");   else if (weight > 300 && ! (weight <= 300))       /* 11 */      if (!(height >= 48)                            /* 12 */          printf(" You are quite short for your weight.\n");   else                                              /* 14 */      printf("Your weight is ideaL.\n");             /* 15 */                                                     /* 16 */   return 0; } 
  4. What is the numerical value of each of the following expressions?

    1. 5 > 2

    2. 3 + 4 > 2 && 3 < 2

    3. x >= y y > x

    4. d = 5 + ( 6 > 2 )

    5. `X' > `T' ? 10 : 5

    6. x > y ? y > x : x > y

  5. What will the following program print?

     #include <stdio.h> int main(void) {   int num;   for (num = 1; num <= 11; num++)   {        if (num % 3 == 0)             putchar(`$');        else             putchar(`*');             putchar(`#');        putchar(`%');   }   putchar(`\n');   return 0; } 
  6. What would the following program print?

     #include <stdio.h> int main(void) {     int i = 0;     while ( i < 3) {        switch(i++) {            case 0 : printf("Merry");            case 1 : printf("Merr");            case 2 : printf("Mer");            default: printf("Oh no!");        }        putchar(`\n');     }     return 0; } 
  7. What's wrong with this program?

     #include <stdio.h> int main(void) {   char ch;   int lc = 0;    /* lowercase char count   int uc = 0;    /* uppercase char count   int oc = 0;    /* other char count   while ((ch = getchar()) != `#')   {        if (`a' <= ch >= `z')             lc++;        else if (!(ch < `A')  !(ch > `Z')             uc++;        oc++;   }   printf(%d lowercase, %d uppercase, %d other, lc, uc, oc);   return 0; } 
  8. What will the following program print?

     /* retire.c   */ #include <stdio.h> int main(void) {   int age = 20;   while (age++ <= 65)   {      if (( age % 20) == 0) /* is age divisible by 20? */          printf("You are %d. Here is a raise.\n", age);      if (age = 65)          printf("You are %d. Here is your gold watch.\n", age);   }   return 0; } 
  9. What will the following program print when given this input?

     q c g b #include <stdio.h> int main(void) {   char ch;   while ((ch = getchar()) != `#')   {        if (ch == `\n')             continue;        printf("Step 1\n");        if (ch == `c')             continue;        else if (ch == `b')             break;        else if (ch == `g')             goto laststep;        printf("Step 2\n");   laststep:  printf("Step 3\n");   }   printf("Done\n");   return 0; } 
  10. Rewrite the program in Question 9 so that it exhibits the same behavior but does not use a continue or a goto .

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net