TOC PREV NEXT

Quiz — Fundamentals


This quiz focuses on the aspects of the C language that are conceptually similar to those in other procedural languages: variable declaration, expression evaluation, flow-of-control constructs, and input/output. Three especially tricky aspects of C covered on this quiz are the difference between = and ==, the difference between char and int values, and the representation of "true" and "false".

Readings

House, chapters 1 through 6 except material in chapters 3, 5, and 7 on arrays. House omits the type of the main function throughout; in contrast, we will always specify it as type int in accordance with the C standard. (By convention, a return value of 0 from the main function means successful completion; nonzero return values indicate some sort of error.)

Kernighan and Ritchie, sections 1.1 through 1.6; chapters 2 and 3 except material on arrays and functions.

Suggested exercises

House, "short answer" self-test exercises and test questions in chapters 2 through 6 except section 5.7 #2 and #7. You may also be asked to write small program segments.

Sample quiz

  1. Suppose that an integer variable n contains the value 80. Fill in the blanks in the printf statement below so that it produces the output
		80% usually
		means "excellent"
printf statement:
		printf ("________", n);
  1. Consider the program segment
		int k;
		k = getchar ( );
		printf ("%d", k);
What gets printed if the user types a 2, then hits return?
  1. The following program is intended to compute 5! = 120, but it prints 24 instead. What's wrong with it?
	int main ( ) {
		int theNum, total;
		total = 1;
		theNum = 5;
		while (theNum > 1) {
			total *= --theNum;
		}
		printf ("%d", total);
		return 0;
	}
 
  1. What would be the effect of substituting !(theNum = 1) for the while condition theNum > 1 in the main program of exercise 3?
  2. Fill in the blanks in the code below to convert the while loop in exercise 3 to an equivalent for loop.
	int main ( ) {
		int theNum, total;
		total = 1;
		for ( _____ ; _____ ; _____ ) {
			total *= theNum;
		}
		printf ("%d", total);
		return 0;
	}
  1. Write down the values for all variables appearing in each statement.
	int main ( ) {
		int x, y, z;
		float a, b;
		x = 3;  y = 5.3;  z = 2;	x ____	y ____	z ____
		a = 2.5; b = 3.14;		a ____	b ____
	/*1*/	x += y + a * z-b;		x ____
	/*2*/ 	a = b / (x%y + z)		a ____
		z += (x == y);			z ____
	/*3*/ 	x == (a = b);			x ____
		return 0;
	}
Where do you need parentheses to have x assigned a value of 14 in the statement labeled /*1*/? What then is the new value of a in the statement labeled /*2*/? What is the new value of x in the statement labeled /*3*/?
  1. Predict the output of the following program. Hint: only three lines get printed. (This is somewhat more complicated than any of the actual quiz questions.)
	int main ( ) {
		char c;
		for (c='a'; c<'g'; ++c) {
			switch (c) {
				case 'a':	c += 2;
				case 'c':	c += 1;
				case 'g':	++c;
						printf ("%c\n" , c-- );
				default:	++c;
			}
			printf ("*** %c\n" , c);
		}
		return 0;
	}

TOC PREV NEXT