C Primer Plus (5th Edition)

I l @ ve RuBoard

Review Questions

  1. Find the value of quack after each line.

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

  2. What output would the following loop produce?

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

  3. Represent each of the following test conditions:

    1. x is greater than 5 .

    2. scanf() attempts to read a single double (called x) and fails.

    3. x has the value 5 .

  4. Represent each of the following test conditions:

    1. scanf() succeeds in reading a single integer __________________________

    2. x is not 5 ____________________

    3. x is 20 or greater ___________________________

  5. You suspect that the following program is not perfect. What errors can you find?

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

  6. Use nested loops to write a program that produces this pattern:

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

  7. What will each of the following programs print?

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

    2. #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. Given the input "Saddle up my charger!" , what would each of the following programs produce for output? (The ! follows the space character in the ASCII sequence.)

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

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

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

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

  9. What will the following program print?

    #include <stdio.h> int main(void) { int n, m; n = 10; while (++n <= 13) printf("%d",n); do printf("%d",n); while (++n <= 12); printf("\n***\n"); for (n = 1; n*n < 60; n +=3) printf("%d\n", n); printf("\n***\n"); for (n = 1, m = 5; n < m; n *= 2, m+= 2) printf("%d %d\n", n, m); printf("\n***\n"); for (n = 4; n < 0; n--) { for (m = 0; m <= n; m++) printf("+"); printf("\n"); } return 0; }

  10. Consider the following declaration:

    double mint[10];

    1. What is the array name ? ______________

    2. How many elements does the array have? _________________

    3. What kind of value can be stored in each element? ____________

    4. Which of the following is a correct usage of scanf() with this array?

      1. scanf("%lf", mint)

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

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

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

  11. Mr. Noah likes counting by twos , so he's written the following program to create an array and to fill it with the integers 2, 4, 6, 8, and so on. What, if anything, is wrong with this program?

    #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. You want to write a function that returns a long value. What should your definition of the function include?

  13. Define a function that takes an int argument and that returns, as a long , the square of that value.

  14. What will the following program print?

    #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; }

I l @ ve RuBoard

Категории