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)  FALSE, you know this is false immediately because the lead bit is a 1, only negative numbers start with 1 in 2's complement.
    (b)  TRUE
    (c)  TRUE, the input operator (>>) will skip all consumed spaces (and tabs, returns, etc.).  This problem was omitted from the exam.
    (d)  FALSE, = is the operator for assignment (== is equality)
    (e)  FALSE, ++ applies to any ordinal type (i.e., discrete), float is not an ordinal type.
  3. Describe in a few lines - Essentially, there were some key ideas I was looking for you to mention. 
  4. (a)  breakpoint:  while using a debugger you set these by telling the debugger which variables you want to track the value of in a small "watch" window.  The value will change as you step through the program (assuming your code changes it!)

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

    (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 = 5, y = 12, z = 56;
    x = y - x;    // x gets 7
    y += z / 2;   // y grows by 28 to get 40
    z = z % 10;   // z gets 6 (remainder of 56 divided by 10)

    So, x ends with 7, y with 40,  and z 6.

    (b)
    int y, x = 8;
    bool z;
    y = x++;     // y gets 8, then x goes up to 9
    z = (x + y) > 14;   // 8 + 9 is greater than 14, so z gets true (any nonzero value, probably 1)

    So, x ends with 9, y with 8, and z with true.  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 - y * sqrt(2*x + 9)

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

  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 7, which is not less than the amount (6 at that point).
  12. How Many? 18
    1 would cost 18
    2 would cost 32
    3 would cost 42
    4 would cost 48
    5 would cost 50
    6 would cost 48
  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;

       // times 100
       cout << "one hundred times x is " << x*100 << endl;

       // divisible by 3  (if x%3 returns a 1 or 2, it is not)
       if (x%3)  
          cout << x << " is not divisible by 3\n";
       else
          cout << x << " is divisible by 3\n";

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