Late submissions will receive 0 points.
Collaboration with others is strictly prohibited.
Assignment: #midterm Due: 11/11/2017 Points: 10
  1. C was created by ______________ circa _____ while he was a Computer Scientist at _______________.


  2. The next two questions use the following code snippet 0.

       // code snippet 0...
       int x[] =  { 110, 205, 120, 240, };   // statement A
       int current_class = x[3];             // statement B
    

  3. Rewrite statement B in code snippet 0 using pointer notation instead of [] notation.

  4. Rewrite statement A (specifically the initialization list) in code snippet 0 so that the following for() loop would work.

       for (int i = 0; i < x[i]; i++)
    


  5. Given  int i; ... The expression  cin >> i  evaluates to what?


  6. Record what the expression  2^4  evaluates to and briefly explain why.


  7. The next two questions use the following code snippet 1.

    // code snippet 1...
    int sum(int, bool = false);  // function prototype
    
    (void)sum(3);
    (void)sum(7);
    (void)sum(-8);
    cout << sum(99, true);
    (void)sum(2);
    cout << sum(-99, true);
    cout << sum(0);
    
    int sum(int n, bool done) {
       static int x = 0;
       if (!done) { x += n; return n; }
       int y = x;
       if (-99 == n) x = 0;
       return y;
    }
    
  8. Record the output for code snippet 1.

  9. Write a short function comment block for the sum() function found in code snippet 1. [Note: A function comment block describes what a function does. It also documents the parameters (if any) that the function receives and the function's return value (if not void).]


  10. Write a function named isnude() that satisfies the following function comment block. You are allowed to use a computer to test your function.

    /*
     * bool isnude(int n) 
     * returns true if n is a nude number; otherwise returns false
     *
     * A number n is a "nude" number if it's evenly divisible by each of 
     * its digits. Examples: 315 is a nude number because it's evenly 
     * divisible by 3, 1, and 5.  125 is not a nude number because it's 
     * not evenly divisible by 2.
     */
    bool isnude(int n) {
       // insert your code here...
    }
    

  11. Rewrite  !(expr1 || expr2)  without ()s.


  12. Rewrite the following do while() loop as a for() loop.

       char c = 'a';
       do {
          cout << c;
          c++;
       } while (c ≤ 'z');
    

  13. The next two questions uses code snippet 2.

       // code snippet 2...
       struct Pyramid {
          int l;
          int w;
          int h;
          double v;
       } p = { 2, 3, 4, };
       Pyramid* x = &p;  // statement F
       x->v = x->l * x->w * x->h / 3.0;
       cout << p.v;
    
  14. Record the output for code snippet 2.

  15. Briefly explain what happens when statement F in code snippet 2 is executed.


  16. Record the primitive data type for the constant 3.0.


  17. Briefly explain why the expression  5 / 2  evaluates to two.


  18. Given  int c = 240, record what the expression  c == c++  evaluates to and briefly explain why.


  19. << is the left-shift bit operator. The expression  240 << 5  evaluates to 240 shifted left five bits. Briefly explain why  <<  is the "insertion" operator when used in the expression cout << 5.


  20. Briefly explain the problem that is caused by code snippet 3.

       // code snippet 3...
       for (int i = 0; i != 4096; i++)
          (void) foo(1024);
       ...
       void foo(int nbytes) {
          (void) malloc(nbytes);
       }
    

  21. Briefly explain the problem that is caused by code snippet 4.

       // code snippet 4...
       char x[10];
       char c;
       int i = 0;
       for (c = '0'; c ≤ '9'; c++, i++)
          x[i] = c;
       for (c = 'a'; c ≤ 'f'; c++, i++)
          x[i] = c;
    

  22. Briefly explain why the statement labeled foo in code snippet 5 does not compile.
       // code snippet 5...
       class X { int x; };
       struct Y { int y; };
    
       X x;
       Y y;
       foo: x = 42;
       goo: y = 42;
    

  23. Briefly explain why code snippet 6 prints 240.
       // code snippet 6...
       int main(int argc, char* argv[]) {
          void f(int&, int = 240);
          int x = 205;
          f(x);
          cout << x << endl;
       }
    
       void f(int& y, int w) { y = w; }
    

  24. Lisp was created by ______________ circa _____ while he was a Computer Scientist at _______________.


  25. List the five languages mentioned by Dr. Stroustrup in the following video.