[an error occurred while processing this directive]

Sample Questions for quiz 1

True or False Questions

  1. It's best never to use expressions with side-effects.

  2. If p is a variable of type BOOL which has a value then x == !!x is always true.

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

  4. x=x+2 and x=1 + x++ are equivalent expressions.

  5. x=x+2 and x=1 + ++x are equivalent expressions.

  6. a=b=1; and a=1;b=1 are equivalent.

  7. a==b; and a=b; are equivalent.

  8. An integer variable that has never been assigned any value must have the value 0.

  9. The main reason to use good indentation in a C program is to make it readable.

  10. The purpose of a compiler is to turn source code into machine language.

  11. The fraction 1/5 can be stored exactly (without round-off error) in a double variable.

  12. The value of the expression 23%3 is 3.

  13. All variables must be declared before they are used.

  14. The expression ++x has a side effect.

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

  16. The expressions a - b - c and a - (b - c) are equivalent.

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

  18. The declaration bool flag; is used to declare a variable with Boolean type.

  19. When a C program is compiled, the source code written in C is transformed into object code that is written in machine language.

  20. A variable may begin with a letter, a digit, or an underscore '_'.

  21. An expression has a side effect if it changes the value stored in a variable.

  22. The expressions ++x and x++ have the same value but have different side effects.

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

  24. The comments in a C program begin with /* and end with */ and are ignored by the compiler.

  25. (17%2 == 17%4)

  26. All variables in a C program must be explicitly declared.

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

  28. The expressions 5.0 / 2 and 5 / 2.0 are equivalent.

  29. "Cascading if statements" should not be used when writing C programs.

  30. The printf function is one of several functions defined in the C standard I/O library commonly known as stdio.h.

  31. a flag is another word for an index variable in a loop.

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

  33. According to DeMorgan's laws, ((!(A && B)) == ((!A) && (!B))) for all boolean variables A and B.

  34. Every for loop can be converted into an equivalent while loop.

  35. Every while loop can be converted into an equivalent for loop.

  36. The expression x = ++x has a side effect.

  37. Every C program must have a main procedure.

  38. x+=2 and x=x+2 are equivalent expressions.

  39. 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.

  40. #define PI = 3.14 This statement give a compilation error.

  41. A C program can run without a main() in it.

  42. The following variable declaration statement is correct

    int myVariable-1;

  43. The following variable declaration statement is correct

    int my Variable;

  44. The following variable declaration statement is correct

    int my_variable_1;

  45. The following variable declaration statement is correct

    int (myVariable);

  46. x+5 = 5; is a valid C statement

  47. a-b/c and (a-b)/c are equivalent

  48. The following statements are equivalent
    int i = 0;
    if (i == 0)
    {
            i = i+1;
    }
    
    and
    
    int i = 0;
    if (i == 0)
            i = i+1;
    

  49. The following statements are equivalent
    int i = 0;
    int j = 0;
    if (i == 0)
    {
            i = i + 1;
            j = j + 1;
    }
    
    and
    
    int i = 0, j = 0;
    if (i == 0)
            i = i+1;
            j = j+1;
    
    

  50. The C compiler ignores 'white / blank' spaces during compilation.

  51. The following statement is a valid C statement

    #define BAND Jon Bon Jovi

  52. Variable names in C can include special characters like &, %, $ etc.



      Multiple Choice

      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.

      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))

      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

      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

      5. Which of the following statements correctly describes the expression
        (n = 3) * 2 + (n = n + 10) * 4?
        a. The expression has a syntax error.
        b. The = symbols should be replaced by ==.
        c. A program containing this expression will crash if the expression is executed.
        d. The value of the expression depends on which compiler is used.

      6. Which of the following is a legal variable name in C:
        a. FiftyFifty
        b. 50_50
        c. Fifty-Fifty
        d. Fifty/fifty

      7. 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.

      8. What is the value of the integer expression: 1 - 3 * 5 + 6 / 4 % 2
        a. 0
        b. -6
        c. -8
        d. -13

      9. 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.

      10. If p and q are Boolean variables, which expression is equivalent to !( p || q)?
        a. !p || !q
        b. p || q
        c. !p && !q
        d. p && q

      11. Which of the following is an integer expression.
        a. the value given by a call to GetInteger().
        b. the value stored in an integer variable.
        c. the result of applying + to two integer expressions.
        d. All of the above.

      12. We say that the minus operator, -, associates left to right meaning:
        a. In the expression (3 * 2) - (4 * 5), the expression on the left (3 * 2) is evaluated first.
        b. In the expression 5 - 3 - 2, the - on the left is evaluated first.
        c. In the expression 5 - 3 * 2, the - is evaluated first because it is on the left.
        d. All of the above.

      13. The expression 19/8.0
        a. has type double and value 2.375.
        b. has type double and value 2.
        c. has type int and value 2.
        d. has type int and value 2.375.

      14. 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).

      15. 2. 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.

      16. 3. Which of the following is a C language keyword?
        • (a.) main
        • (b.) int
        • (c.) while
        • (d.) Only b and c.
        • (e.) None of the above, since C doesn't have keywords.

      17. 4. Printf format codes. Which of the following format codes could be used to print a floating point value in a printf statement? (Five points)
        • (a.) %d
        • (b.) %f
        • (c.) %g
        • (d.) Only b and c.
        • (e.) All of the above.

      18. 5. What is the value of 7 + 2 / 3 - 4 + 4 / 3?
        • (a.) 4
        • (b.) 5
        • (c.) 0
        • (d.) None of the above.

      19. The operators + - * / and % are
        	a. left associative
                b. right associative
        

      20. 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
        


      Short Answers, 4 points each

      This section cannot be graded on-line. To practice taking the exam, jot down your answer and follow the link after each question to see a sample solution.

      1. Write down the output of the following program fragment.
        	int n ;
        
        	n = 3 ;
        	if (n != 3) {
        	   printf("Captain Fantastic\n") ;
        	} else if (n * 2 <= 5 + n) {
        	   printf("Brown Dirt Cowboy\n") ;
        	} else {
        	   printf("Rocket Man\n") ;
        	}
        

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

      3. What is the output of the following code fragment?
        	int n, m ;
        
        	n = 1 ;
        	m = 1 ;
        	while (n < 30) {
        	   printf ("n = %d, m = %d\n", n, m) ;
        	   m = m + 2 ;
        	   n = n + m ;
        	}
        
      4. Write down (line by line) the output of the following program fragment.
              int a = 1, b = 2 ;
              
              printf("a = %d, b = %d\n", a, b) ;
              a += b ;
              printf("a = %d, b = %d\n", a, b) ;
              a = b++ ;
              printf("a = %d, b = %d\n", a, b) ;
              

      5. What is the output of the following code?
              int a, b ;
        
              a =  6 ;
              b =  9 ;
              if ( a * b == 42 ) {
                 printf("Hitchhiker's Guide\n") ;  
              } else if ( a - b < -3  ) {
                 printf("Milliway's\n") ;
              } else {
                 printf("Life, the Universe and Everything\n") ;
              }
              

      6. What is the output of the following code?
              int n, i ;
             
              i = 0 ;
              n = 2048 ;
              while (n > 0) {
                 n = n / 2 ;
                 printf("n = %d\n", n) ;
                 i++ ;
              }
              printf ("i = %d,  n = %d\n", i, n) ;
              
      7. 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  
        

      8. Write down (line by line) the output of the following program fragment.
              int a, b ;
              
              a = 6 ; 
              b = a + 7 ;
              printf("a = %d, b = %d\n", a, b) ;
              a += b / 3  ;
              printf("a = %d, b = %d\n", a, b) ;
              a = b = 2 * a ;
              printf("a = %d, b = %d\n", a, b) ;
              

      9. What is the output of the following code?
              int a ;
        
              a = 27 ;
              if ( a > 27 ) {
                 printf("Georges Seurat\n") ;
              } else if ( a % 3 == 0) {
                 printf("Edvard Munch\n") ;
              } else {
                 printf("Joan Miro\n") ;
              }
              

      10. 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);
        

      11. What does this print?
          int i, j, k;
          i = j = 0;
          k = 3;
          while ( (i+j) < k) {
            i++;  j++;  k++;
            printf("End of pass %d: j=%d and k=%d\n",i,j,k);
          }
        

      12. What does this print?
          #define pi 3.1416
          int a;
          double b;
          float c;
          b = a = pi*2.0;
          c = (int) pi + a
          printf("a is %d b is %f c is %f\n,a,b,c);
        

      13. What does this print?
          int a, b; c;
          a = b = c = 2;
          a *= (b *= (c *= 2));
          printf("a is %d, b is %d, and c is %d\n", a, b, c);
        

      14. What does this print?
          BOOL p, q, r, s;
          p = TRUE;
          q = FALSE;
          p_and_q = (p && q);
          p_or_q = (p || q);
          if (p_and_q) printf("Great!\n");
          if (p_or_q) printf("Good!\n");
        

      15. What is the default executable file that is generated when a C program is compiled successfully ?

      16. How many times is "Hello World" is printed if the following program is run ? while(1) { printf("Hello World\n"); }

      17. Predict the output of the following:
        int i;
        while(i<5) {
        
                if(i=2) {
                       printf("Hello \n");
                }
        
                else {
                        printf("Hello World \n");
                }
        	i++;
        }
        

      18. Here's a fragment of C code
        int num, i= 1, count =0;
        num = GetInteger();
        
        while(num/i != 0) {
         i = i*10;
         count ++;
        }
        
        printf("count = %d \n", count);
        

        Assume you are the user entering the value for num. what does count end up calculating?

      19. 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");
        }
        

      20. What is the output of the following program segment :
                int p = 0;
        
                if (p)
                        { printf("ADIOS \n"); }
                else
                        { printf("ALOHA \n"); }
        

      21. What is the output of the following :
                int p = 4; 
        	float q = 10.0;
                int result;
        
                while (p > 0)
                
                {
                        result = q / p;
                        printf("%d \n", result);
                        p -- ;
                }
        

      22. What does this print? (experts only!) int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\ o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
      [an error occurred while processing this directive]
      last modified on Friday, 20-Sep-1996 19:12:16 EDT