C Primer Plus (5th Edition)
I l @ ve RuBoard |
Review Questions
-
Find the value of quack after each line.
int quack = 2; quack += 5; quack *= 10; quack -= 6; quack /= 8; quack %;= 3;
-
What output would the following loop produce?
for ( value = 36; value > 0; value /=2) printf("%3d", value);
-
Represent each of the following test conditions:
-
x is greater than 5 .
-
scanf() attempts to read a single double (called x) and fails.
-
x has the value 5 .
-
-
Represent each of the following test conditions:
-
scanf() succeeds in reading a single integer __________________________
-
x is not 5 ____________________
-
x is 20 or greater ___________________________
-
-
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 */
-
Use nested loops to write a program that produces this pattern:
$$$$$$$$ $$$$$$$$ $$$$$$$$ $$$$$$$$
-
What will each of the following programs print?
-
#include <stdio.h> int main(void) { int i = 0; while (++i < 4) printf("Hi! "); do printf("Bye! "); while (i++ ;lt 8); return 0; }
-
#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; }
-
-
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.)
-
#include <stdio.h> int main(void) { char ch; scanf("%c", &ch); while ( ch != 'g' ) { printf("%c", ch); scanf("%c", &ch); } return 0;}
-
#include <stdio.h> int main(void) { char ch; scanf("%c", &ch); while ( ch != 'g' ) { printf("%c", ++ch); scanf("%c", &ch); } return 0; }
-
#include <stdio.h> int main(void) { char ch; do { scanf("%c", &ch); printf("%c", ch); } while ( ch != 'g' ); return 0; }
-
#include <stdio.h> int main(void) { char ch; scanf("%c", &ch); for ( ch = '$'; ch != 'g'; scanf("%c", &ch) ) putchar(ch); return 0; }
-
-
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; }
-
Consider the following declaration:
double mint[10];
-
What is the array name ? ______________
-
How many elements does the array have? _________________
-
What kind of value can be stored in each element? ____________
-
Which of the following is a correct usage of scanf() with this array?
-
scanf("%lf", mint)
-
scanf("%lf", mint[2])
-
scanf("%lf", &mint[2])
-
scanf("%lf", &mint)
-
-
-
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; }
-
You want to write a function that returns a long value. What should your definition of the function include?
-
Define a function that takes an int argument and that returns, as a long , the square of that value.
-
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 |