CS401 - Solutions for Exam 1

Make sure you have your exam handy when you work through these solutions.

My general reaction is that people did quite well on the exam, so that was nice to see.  Many people lost points, nonetheless, because they did not read the directions properly.  In the future, before you write an answer to any question, double-check that what you are writing does answer the question accurately (to the best of your knowledge) and completely.  Of course, brevity is good because everything you write down must be graded (unless you X it out) and if you say something that is incorrect, then it has to be marked wrong.
 

  1. True/False
  2. (a)  TRUE
    (b)  FALSE, = is the assignment operator.  == is the equality operator
    (c)  TRUE, 18 in binary is 00010010, flip the bits to get 11101101, then add 1 to get 11101110.
    (d)  TRUE, the input operator (>>) will skip all consumed spaces (and tabs, returns, etc.).  This problem was omitted from the exam.
    (e)  TRUE, ++ applies to any integer type, which char is considered a part of.
  3. Describe in a few lines - Essentially, there were some key ideas I was looking for you to mention. 
  4. (a)  fetch-execute cycle:  the CPU runs this loop, it has three stages:  (1) get the next instruction at the address of the program counter (PC), (2) increment the PC, (3) execute the instruction.  Then go back to (1).

    (b)  short-circuit evaluation:  this is an optimization used by the C++ compiler when evaluating logical expressions.  It says to evaluate only as much as you need to get the final answer.  For example, the following expression will short circuit (to false) if x holds 5 (or any positive number, it does not matter what y holds):

    ((x < 0) && (y > 10))
    (c)  operator precedence:  this refers to the order that operations are applied when expressions are evaluated.  It only comes into play when parantheses are not used to dictate that order.
    (d)  breakpoint:  when you use a debugger you set these at particular lines in your program, then when you run that program, execution will hold up and wait for you to tell it how to proceed. 
    (e)  overflow:  this occurs when the value of an expression exceeds the capacity of the destination memory location.  For example, if x holds 20000 (and we are using 16-bit integers), the following line will cause overflow (the upper limit is 32,768):
    x = x*2;
  5. declarations - a declaration is when you declare a variable.  All of these could be done in one line, but two is permissible.
  6. (a)  int par=72, score=0;
    (b)  bool likesColdDrinks;
    (c)  const double avogad = 6.02e23;
    (d)  string favorite_team = "Pittsburgh Panthers";  // or any team
    (e)  char ch1='t', ch2;

    You should never do more than what is asked for, especially on exams.  Just stick to the specifications.  It was ok to use pow() on (c), but the listed way is superior. 

  7. code segments - the directions stated to give the values for x, y, and z after each segment.  Each segment was independent (you should have known a segment is a group of statements).
  8. (a) 
    int x = 7, y = 22, z = 43;
    x = y - x;    // x gets 15
    y += z / 2;   // y grows by 21 to get 43
    z = z % 10;   // z gets 3 (remainder of 43 divided by 10)

    So, x ends with 15, y with 43,  and z 3.

    (b)
    int y, x = 10;
    bool z;
    y = x++;     // y gets 10, then x goes up to 11
    z = (x + y) < 15;   // 10 + 11 is less than 15, so z gets false (or 0)

    So, x ends with 11, y with 10, and z with false (or 0).  MANY people did not get x and y correct here.  Make sure you understand why.

    (c)
    string x = "On Wisconsin", y;
    int z;
    z = x.size();      // z gets 12 (just count the letters and space)
    y = x.substr(0,6); // y gets "On Wis", start at loc 0, go for 6 spaces
    x = x + ' ' + x;   // just concatenate x with itself, 1 space in between

    So, x ends with "On Wisconsin On Wisconsin", y with "On Wis", and z with 12.

  9. formulas - this was pretty straight forward.
  10. 1 + 8 * sqrt(2*x + 1)

    (1.0 - b) / (pow(a+4.17, c) - a * (b + 2.0))

  11. output - for this one, make two columns:  one for count and one for amount, update the values as you work through the code.  The loop ends because count reaches 5, which is not less than the amount (3 at that point).
  12. How Many? 11
    1 would cost 11
    2 would cost 18
    3 would cost 21
    4 would cost 20
  13. expressions - some of you did not read the directions... you were supposed to give me a value and a type for each.
  14. Answer format:  <value, type>

    (a)  <'n', char>     (you can increment chars, the increment will occur AFTER the expression value is determined)
    (b)  <11.55, float>
    (c)  <5.0, double>
    (d)  <500, long> 
    (e)  (100500 - 500) + 8.0 = 100000 + 8.0... <100000.8, double>

    A note about (d)... taking the mod by a power of ten returns than many digits, so anything mod 10^3 will return the right-most 3 digits.

  15. draw a picture

  16. source code -->  COMPILER --> object file --> LINKER --> executable --> LOADER --> EXECUTION
                                                           libraries --^
  17. write a program - here is one possible solution.  Many people made it harder than it was.  For absolute value, you needed to check to see if it was a negative number first.  Also, many people paid no attention to formatting or comments.  I was not too worried about it, but please do give it an effort, even though it is hard on unlined paper.
  18. //directives here

    int main() {
       int x;
       cout << "Enter a value: ";
       cin >> x;

       // eveness and oddness
       if (x%2)
          cout << x << " is odd\n";
       else
          cout << x << " is even.\n";

       // half
       cout << "half of x is " << x/2 << endl;

       // absolute value
       cout << "abs value of " << x << " is ";
       if (x<0)
          cout << -x << endl;
       else
          cout << x << endl;

       return 0;
    }


 

Last Updated: 6/18/01 by H. Chad Lane, hcl@cs.pitt.edu
© 2000-2001 Jim Skrentny, University of Wisconsin