Review Questions

I l @ ve RuBoard

Review Questions

  1. Assume all variables are of type int . Find the value of each of the following variables:

    1. x = (2 + 3) * 6;

    2. x = (12 + 6)/2*3;

    3. y = x = (2 + 3)/4;

    4. y = 3 + 2*(x = 7/2);

  2. Assume all variables are of type int . Find the value of each of the following variables:

    1. x = (int) 3.8 + 3.3;

    2. x = (2 + 3) * 10.5;

    3. x = 3 / 5 * 22.0;

    4. x = 22.0 * 3 / 5;

  3. You suspect that there are some errors in the next program. Can you find them?

     int main(void) {   int i = 1,   float n;   printf("Watch out! Here come a bunch of fractions!\n");   while (i < 30)     n = 1/i;     printf(" %f", n);   printf("That's all, folks!\n");   return; } 
  4. Here's a first attempt at making min_sec interactive, but the program is not satisfactory. Why not? How can it be improved?

     #include <stdio.h> #define S_TO_M 60 int main(void) {   int sec, min, left;   printf("This program converts seconds to minutes and ");   printf("seconds.\n");   printf("Just enter the number of seconds.\n");   printf("Enter 0 to end the program.\n");   while (sec > 0) {     scanf("%d", &sec);     min = sec/S_TO_M;     left = sec % S_TO_M;     printf("%d sec is %d min, %d sec. \n", sec, min, left);     printf("Next input?\n");     }   printf("Bye!\n");   return 0; } 
  5. What will this program print?

     #include <stdio.h> #define FORMAT "%s is a string\n" int main(void) {      int num = 0;      printf(FORMAT,FORMAT);      printf("%d\n", ++num);      printf("%d\n", num++);      printf("%d\n", num--);      printf("%d\n", num);      return 0; 
  6. What will this program print?

     #include <stdio.h> int main(void) {      char c1, c2;      int diff;      float num;      c1 = `D';      c2 = `A';      diff = c1 - c2;      num = diff;      printf("%c%c%c:%d %3.2f\n", c1, c2, c1, diff, num);      return 0; 
  7. What will this program print?

     #include <stdio.h> #define TEN 10 int main(void) {      int n = 0;      while (n++ < TEN)           printf("%5d", n);      printf("\n");      return 0; 
  8. Modify the last program so that it prints the letters a through g instead.

  9. If the following fragments were part of a complete program, what would they print?

    1.  int x = 0; while (++x < 3)     printf("%4d", x); 
    2.  int x = 100; while (x++ < 103)     printf("%4d\n",x);     printf("%4d\n",x); 
    3.  char ch = `s'; while (ch < `w') {     printf("%c", ch);     ch++; } printf("%c\n",ch); 
  10. What will the following program print?

     #define MESG "COMPUTER BYTES DOG" #include <stdio.h> int main(void) {    int n = 0;    while ( n < 5 )       printf("%s\n", MESG);       n++;    printf("That's all.\n");    return 0; 
  11. Construct statements that do the following (or, in other terms, have the following side effects):

    1. Increase the variable x by 10 .

    2. Increase the variable x by 1 .

    3. Assign twice the sum of a and b to c .

    4. Assign a plus twice b to c .

  12. Construct statements that do the following:

    1. Decrease the variable x by 1 .

    2. Assigns to m the remainder of n divided by k .

    3. Divide q by b minus a and assign the result to p .

    4. Assign to x the result of dividing the sum of a and b by the product of c and d .

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