QUIZ Nov. 24, 2004 :       Check all correct answers.

(The correct answers are marked by green circles (). The answers which might be correct in some sircumstances are marked by white circles ().

  1. How does the computer know whether the program is in C or C++ (in the UNIX environment we work in)?
    1. By the filename extension: .c or .cxx etc.
    2. By the command used to compile the program: gcc or g++
    3. The programmer must provide special options in the command line
    4. The computer is able to tell a C program from a C++ program automatically.

    Comment: In some systems, answers A or C would be correct. In fact, the system administrator or an advanced user can write a short program in a special language (shell programming language, AWK, or Perl) that will automatically recognize file extensions and invoke an appropriate compiler. Command line options can also be used to control the language conventions assumed by the compiler.

  2. If the variable n is int and the variable x is float, then the assignment n=x; is
    1. illegal; it will cause compilation error
    2. safe and sound
    3. will pass compilation but may lead to a mathematical error due to roundoff effect
    4. will cause no problems for the program, but isn't best in terms of style; better use the explicit cast: n=(int)x;

    Comment: Consider this fragment of code (assume that R and Circumference are float):
       x=3.141592635; 
       n=x; 
       Circumference =2*n*R;  
    
    The code will make Circumference equal to 6R rather than 2*Pi*R.
    The answer D may be true in some situations. For example, numerical summation of an infinite series to a given accuracy epsilon may require the number of loop iterations approximately equal to 1/epsilon. The code below is a bit ugly but it does the job and causes no problems (assuming epsilon is float and i an integer and the loop is properly implemented):
       epsilon=1.0E-6; 
       x=1.0/epsilon; /* determine approximate number of iterations */
       n=x;  /* determine the number of iterations as an integer value */
       for (i=1; i<=n; i++) 
       {  .................
       }   
    
    In all cases, using the explicit cast ((int)), alone or in combination with roundoff functions double floor(double) and double ceil(double) of math.h is a more sound option.

  3. The number of bits in one byte is
    1. 2
    2. 8
    3. 8 times the number of different bit values
    4. 256

    Comment: 2 is the number of different bit values, while 256= 28 is the number of different byte values. This must be firmly understood, see Sect.2.8 (p.77) if in doubt.

  4. The value of    5 && 2   is
    1. 0, since in the binary form 5=101 and 2=10
    2. 7, since 5+2=7
    3. 1, since 5 and 7 are not equal to 0
    4. 2 or 5 depending on the exact form of the code

    Comment: Evaluation rules for logical operations && (AND), || (OR), ! (NOT) and the relational (comparison) operations >, <, ==, , != etc. must be firmly understood, see Sect.4.1 (p.123). The result of any of these, being a logical TRUE or FALSE by its nature, can assume only two values 0 or 1.

  5. The function declaration contains
    1. return type or the word void
    2. list of arguments of the function (including types of the arguments)
    3. function name
    4. function body
    5. return statement
    6. prefix (class name::) if the function is a member of a class

    Comment to F: Prefixing the function name in the declaration is admissible by language rules.
     
       class MyClass
       {
          void MyClass::myFunction(); /* OK with the compiler */ 
       };
    
    It is just unnecessary and I've never seen that anybody would prefer this "full form".