[an error occurred while processing this directive]

CMSC201 Evening Lecture
Quiz 1

September 24, 1996

Thought for the day: "Nothing defines humans better than their willingness to do irrational things in the pursuit of phenomenally unlikely payoffs. This is the principle behind lotteries, dating, and religion. -- cartoonist Scott Adams, The Dilbert Principl (1996), p. 76.

True or False Questions (3 points each)

  1. If x is an integer variable which has a value, then (x%2==0)||(x%2==1) is always true. TRUE

  2. The expressions a + b % c and (a + b) % c are equivalent. FALSE

  3. The expressions 15 / 3 and 15 / 3.0 are equivalent. FALSE

  4. The difference between variables of type double and variables of type int is that double variables can store negative numbers but int variables cannot. FALSE

  5. A variable declaration sets the value of double variables to 0.0. FALSE

  6. In C, semicolons should only be placed between pairs of adjacent statements. FALSE

  7. Every while loop can be converted into an equivalent for loop. TRUE

  8. If age is an integer variable with a value then 17<age<22 is a statement in C which is true if the value of x is either 18, 19, 20 or 21. FALSE

  9. x+5 = 5; is a valid C statement FALSE

  10. a-b/c and (a-b)/c are equivalent FALSE

Multiple Choice (3 points each)

  1. Consider the integer expression (17+3)*(14+9)+3. The fact that multiplication has higher precedence than addition tells you that:

    a. The expression (17+3) is evaluated before (14+9)+3.
    b. The expression (17+3) is evaluated after (14+9)+3.
    c. The expression above is equivalent to (17+3)*((14+9)+3).
    d. The expression above is equivalent to ((17+3)*(14+9))+3.
    D

  2. Consider the integer expression (4+5)-(7+9)-(2+4). The fact that subtraction associates left to right tells you that

    a. The expression (4+5) must be evaluated before the expression (7+9).
    b. The expression (7+9) must be evaluated before the expression (2+4).
    c. The expression above is equivalent to ((4+5)-(7+9))-(2+4).
    d. The expression above is equivalent to (4+5)-((7+9)-(2+4))
    C

  3. Which of the following is not an integer expression?

    a. 14%3
    b. 14/3.0
    c. (int)(14.0/3.0)
    d. 14/3
    B

  4. Which of the following expressions does not have a side effect?

    a. i == 17 - i
    b. n = GetInteger()
    c. n = m = 0
    d. i++= 17 - i
    A

  5. In C, the term ``white space'' means:

    a. A single space, tab or new-line character.
    b. Several spaces, tabs or new-line characters.
    c. Any combination of spaces, tabs and new-line characters.
    d. All of the above.
    D

  6. By the rules of short-circuit evaluation in p || ( n < 3 )

    a. if p is true, then n < 3 will not be evaluated.
    b. if p is false, then n < 3 will not be evaluated.
    c. if p is true, then n < 3 will be evaluated.
    d. None of the above.
    A

  7. What numbers are printed out when the following for loop is executed? (Note that the printf statement prints out the expression i+1 and not i.)
       for (i = 0 ; i < 12 ; i++ ) {printf("%d\n", i + 1) ;}
    

    a. The numbers 0 through 12 (including 0 and 12).
    b. The numbers 1 through 12 (including 1 and 12).
    c. The numbers 0 through 11 (including 0 and 11).
    d. The numbers 1 through 11 (including 1 and 11).
    B

  8. Which of the following is a legal identifier in C?

    (a.) NINE_TO_FIVE
    (b.) 9_to_5
    (c.) This_identifier_may_be_too_long
    (d.) All of the above.
    (e.) Only a and c.
    E

  9. What is the value of 7 + 2 / 3 - 4 + 4 / 3?

    (a.) 4
    (b.) 5
    (c.) 0
    (d.) None of the above.
    A

  10. Parentheses in an expression such as a * (b + c) are used for

    (a.) Good programming style
    (b.) To take care of operator precedence
    (c.) Readability of code
    (d.) None of the above
    B

Short Answers (10 points each)

Assume everything compiles correctly and don't worry about the exact spacing.

  1. What is the output of the following code fragment?
    	int i;
    
    	for (i = 0 ; i < 9 ; i = i + 2) {
    	   printf("%d\n", 2 * i) ;
    	}
    
    0 4 8 12 16

  2. Evaluate the following expression. Recall that *, / and % have higher precedence than + and - and all five operators associate left to right.
       2 + 7 + 6 * 4 - 5 / 2  + 8 % 3  
    
    33

  3. Predict the output of the following code fragment
    int i = 0;
    
    if(i = 1) {
            printf ("Pink Floyd");
    }
    else if (i == 2) {
            printf ("Led Zeppelin");
    }
    else {
            printf ("Grateful Dead");
    }
    
    Pink Floyd

  4. What does this print?
      int a, b;
      a = 11;
      b = 27;
      a++;
      a += b;
      b = a - b;
      a -= b;
      printf("a is %d and b is %d\n", a, b);
    
    a is 27 and b is 12
[an error occurred while processing this directive]
last modified on Thursday, 03-Oct-1996 21:10:18 EDT