CS 2213/1, Spring 2005      
Quiz 1 Questions
Last Name:                            First Name:                  

Don't spend too much time on any one question.

  1. C basics (from Weiss book):

    1. Can comments be nested in C, that is, is the following a legal comment in C? (Will the following compile without error in C?)

        
        /* start of a comment,
          but here is /* a comment */ nested 
          within the outer comment
         */
        

       

       

       

    2. Consider the following two C programs:

      #include <stdio.h>
      int main() {
         int a[5] = {0, 0, 0, 0, 0};
         int i;
         for (i = -10000; i < 10; i++)
            if (a[i] == 0 && i >= 0)
               a[i] = i + 10;
      }
      
               
      #include <stdio.h>
      int main() {
         int a[5] = {0, 0, 0, 0, 0};
         int i;
         for (i = -10000; i < 10; i++)
            if (i >= 0 && a[i] == 0)
               a[i] = i + 10;
      }
      

      One of these two has a very serious flaw (although both will compile, one may not run correctly).

      1. Which one has the flaw, and what is it?

         

         

         

      2. Explain briefly how the correct program avoids the problem.

       

       

       

    3. Say what will be printed by the following program (it compiles and runs without error), and say why this value will be printed.

      #include <stdio.h>
      int main() {
         int a = -1;
         int b = ( a > 0 ? 47 : 53 );
         printf("b: %i\n", b);
      }
      

     

     

     

  2. C I/O:

    1. Say exactly what will be printed by the following program (give every character printed):

      #include <stdio.h>
      #define PI 3.14159265358979
      int main() {
         printf("The value of \"PI\" is: %7.4f\n", PI);
      }
      

     

     

     

  3. Random Numbers:

    1. Consider my own function rand() that is defined in the first section of the web page on random numbers. (This is not the same as the C library function rand().) What specifically can you say about the numbers returned by my function (besides saying that they are pseudo-random, that is, that they appear to be random)?

     

     


Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.