CISC181, Fall 2004
Exam1, Post Exam Notes

These are some notes I came up with while grading the exams.

Even if you were satisfied with your performance on the exam, it may still be helpful to look over these comments, since in some cases I hint at how questions on future exams might be phrased. If you were disappointed in your performance, that's even more reason to look over these notes carefully.

This is not a complete answer key or rubric. For the questions that nearly every student got correct, I have not supplied answers here. Rather, I have supplied correct answers and comments on some incorrect answers only in cases where I think some additional comment might be helpful to the class' learning, or to CISC181 instructors in making up future exams on this material.

Question 2

Here's the text of the question:

(10 pts) One of your lab exercises involved writing functions to draw pictures using for loops. This questions tests your knowledge of that concept.

Complete the function drawL in the program listed below.

The function should draw a picture on standard output in the shape of the letter L of the given height and width, followed by a blank line.

If either height or width is less than 2, the function simply draws nothing (no error message is produced, and no blank line is printed.

The complete program appears below (with the body of function drawL omitted), with sample output on the following page. You may fill in the function in the space provided, or rewrite the complete function in the blank space on the next page (below the sample output.)

Here's a complete answer (note that there are many possible answers).
// e02.cc  Exam question for CISC181
// P. Conrad, 10/04/04

#include <iostream>
using namespace std;

void drawL(int height, int width, char c)
{

 if (height < 2 || width < 3 || width % 2 != 1 )
return;
for (int i=0; i<width; i++)
cout << c ; cout << endl; for (int i=0; i<height-1; i++) { for (int j=0; j<width/2; j++) cout << " "; cout << c << endl; } cout << endl; return;

} int main(void) { drawL(2,2,'s'); drawL(3,2,'y'); drawL(4,3,'x'); drawL(2,1,'a'); drawL(3,5,'z'); return 0; }
Output:
> g++ e02.cc
> ./a.out
s
ss

y
y
yy

x
x
x
xxx

z
z
zzzzz

>

Common difficulties that students ran into here include:

Question 3

Number conversions:

  1. (3 pts) Convert 73 from decimal to binary

    Few students had any trouble coming up with the correct answer: 1001001.
    A challenge for the next exam: what would this number be in Octal? In Hexadecimal? See Appendix C in Deitel and Deitel for more help on this.

  2. (3 pts) Convert 2A from hexadecimal to decimal

    The correct answer is 42. "A" is in the one's place, and represents 10. "2" is in the 16's place, which means that it contributes "2 x 16 = 32" to the total. So the total is 32 + 10 = 42.

    I note that some folks took "2" + "10" and came up with "12". If you got that answer, please try to understand why it is incorrect before the next exam. Appendix C in Deitel and Deitel may help. You can also see me during office hours for additional help.

  3. (3 pts) Convert the following from binary to hexadecimal:
    1100 0001 1000 0001 0010 1111 1010 1011

    Some of you made long computations here, and invariably, came up with an incorrect answer. This is actually a very simple question if you know the technique: take four bits at a time, and convert each to a digit (0-9, or A-F). This technique was covered in lecture. If you are still unsure of this, come by during office hours for a refresher course, or ask one of your classmates to help you; it only takes 5-10 minutes to learn it. Appendix C in Deitel and Deitel may help as well.

    The correct answer is: C1812FAB (which is a license-plate style commentary on how wonderful it is to be enrolled in—or teaching—this fabulous course we call CISC181.)

    The CISC105 class this semester had the following question, which might refer to a mode of transportation for a father that had consumed a bit too much of the flagship products of the establishment on Main Street, Newark Delaware, known as the "Iron Hill Brewery". For practice, can you translate this one?

    1010 1100 1010 1011 0100 1101 1010 1101

    Maybe we should have a competition to come up with the most clever use of the digits 0-9 and the letters A-F to spell out an message in exactly eight characters. If you come up with one, post it on your CISC181 web site (only in binary of course) along with a clue as to the interpretation. (We can relax the rules I suppose and allow message of more or less than 8 bytes, but it's nicer if they are 8 bytes since that is exactly the length of an "int" variable on strauss, i.e. 32 bits.)

    Here's one more, though this one is a 12 byte message: "If a Swedish 70s pop band all became vegetarians, Texas cattle ranchers might issue the following plea."

    1111 1110 1101 1101 1010 1011 1011 1010 1011 1110 1110 1111

    Finally a warning: occaisionally, I might try to be clever with my binary to hex conversions, but make a typo. If the binary seems to come "close" to spelling a message, but is one bit off, don't be deceived or doubt yourself; just translate the bits as they appear.

Question 4

    Consider the C++ program on the following page.

    Here's the program you were given:
    // e01.cc  Exam question for CISC181
    // P. Conrad, 10/04/04
    
    #include <iostream>
    using namespace std;
    
    
    int mysteryFunc(int x, int y)
    {
      x =  -y + x * 5;
      y--;
      cout << "x= " << x << endl;
      return y;
    }
    
    int main(void)
    {
      int a, b, c;
      a = 3; 
      b = 5;
      c = 7;
      
      cout << "a= " << a << endl;
       a = mysteryFunc(b,c);
      cout << "a= " << a ;
      cout << " b= " << b << endl;
      cout << " c= " << c << endl;
    
    }
    

    1. (8 pts) Give the output

      Note that you were given a "grid" in which to put your output. Note that you should use the grid to give your answer to such questions. (I'll try to be more specific about the instructions on future exams.) The reason for the grid (one character per space) is so that you must be very specific about where spaces appear and do not appear. In particular, in this program, there are "endl" directives in some of the "cout <<" statements, and not in others, and there are spaces in some of the string literals, and not in others. All of this affects the output.

      Here is the correct answer:
      a =   3                      
      x =   1 8                    
      a =   6   b =   5            
        c =   7                    
                                   
                                   
                                   
                                   
                                   
                                   

       

    2. (4 pts) What are the names of the parameters in the call to the user-defined function in this program (somes these are called ``actual parameters''?)

      The answer is was looking for here is "b,c". These are the names of the actual parameters in the function call.

      Some folks offered the answer "arguments". It is correct that the word arguments is a technical term that is sometimes by some authors interchangably with the term parameters. Other authors restrict the word arguments to mean only the actual parameters in a function call. In any case, if you gave the answer "arguments": I could argue that the question was worded specifically enough that you could have known I was looking for the names of the arguments (b,c), not a term used to describe them, but I gave you the benefit of the doubt.

      A similar answer that is a "true statement" about (b,c) but doesn't really answer the question I posed, is that answer "call-by-value parameters". It is true that in the function mysteryFunc the parameters are both passed "by value", which makes them "call-by-value parameters". Howver, that isn't the question I asked. Again, for this exam, I gave the benefit of the doubt to this answer, but I might not do so on future exams this semester.

      Note that "a" cannot be considered a correct answer here. The variable "a" is the target of an assignment statement in which mysteryFunc appears on the right hand side. In that sense, "a" is one step removed from the actual function call to mysteryFunc; we call mysteryFunc as part of evaluating the right hand side of the assignment statement, and then store the result of that evaluation into a. Note that I could have written the line of code:

      a = mysteryFunc(b,c) * 2;

      This would have further illustrated the "arms-length relationship" between the call to mysteryFunc, and the assignment to the variable a.

      In future, look for a question that more specifically gets to the issue I wanted to test here, which is whether you can distinguish a function definition from a function call, and actual parameters from formal parameters. Here are some sample questions in that form:
      • Do the variables (b,c) appear in this program as "actual parameters" or "formal parameters? (answer: actual parameters)
      • Do the variables (x,y) appear in this program in the function "definition" or in a function "call"? (answer: definition)
      • What is the "type" of the variable x in the function definition of mysteryFunc (the only correct answer is "int".)
      • How are the parameters passed to the variable mysteryFunc, by reference or by value? (answer: value).
    3. (3 pts) Circle the unary operators that appear inside the body of the user-defined function ``mysteryFunc''. Circle only the unary operators their operands.

      (If you circle the wrong thing accidentally and want to change your answer, just find some other way to indicate clearly what the unary operators are.)

      The correct answer is to indicate the -y and the y--. The -y is an example of the unary "negation" operator (top of p. 93 in Deitel/Deitel), while the y-- is an example of the unary "post-decrement" operator (Section 2.12 in Deitel/Deitel, p. 99).

      Note that the "<<" and ">>" operators are binary operators; they have a left and a right hand side. Also note that nothing appearing inside a string literal is every considered an operator: is it just text that gets printed out. Even if I wrote:

      cout << "-y = " << -y << endl;

      there is only one occurence of a unary operator on that line of code! The minus sign in front of the y inside the quotes is not a unary minus.

      Consider this: if you have a glass of milk in front of you, and a picture of a glass of milk next to it, how many beverages do you have? You have only one, because you can drink the glass of milk, but you cannot drink the picture of the glass of milk. In the same way, the minus sign inside the quotes is not a unary operator; it is only a kind of "picture" of a unary operator that can be printed on the standard output with the "cout <<" statement.

      I mention this because some students circled "x=" as an example of a unary operator. Of course, even if "x=" weren't in the quotation marks, the = sign, which represents the assignment operator, is never a unary operator anyway: it is always a binary operator since it must have a right hand side (the expression to be evaluated) and a right hand side (the target of the assignment).

       

Question 5

  1. Consider the C++ program on the following page.

    Here's the program you were given:
    // e03.cc  Exam question for CISC181
    // P. Conrad, 10/04/04
    
    #include <iostream>
    using namespace std;
    
    
    int main(void)
    {
      int x;
      cout << "Enter x: ";
      cin >> x;
    
      int i=1;
      while (i < x)
        {
          cout << "*";
          i *=2;
        }
      cout << endl;
    }
    1. (4 pts) Give the output when the input is 5

      Two possible correct answers are shown below. In the first answer, the prompt and user input are shown. In the second answer, only the output following that exchange is shown. (Both answers are acceptable since I didn't clarify; I'll try to be more specific next time.)

      Note that you should use the grid to give your answer to such questions; again, I'll try to be more specific about the instructions on future exams. The reason for the grid (one character per space) is so that you must be very specific about where spaces appear and do not appear.

      Correct Answer (with input)
      E n t e r   x :   5                    
      * * *                                  
                                             
                                             

      Another correct answer:
      * * *                                  
                                             
                                             
                                             


    2. (4 pts) Give the output when the input is 67

      The answer here is the same, except there are 7 stars instead of 3.

    3. (1 pts) What relational operator appears in this program?

      The relational operator that appears in this program is the less-than operator, "<".

      Strictly speaking, if you wrote "i<x" or "while (i<x)", those answers are incorrect, since only the "<" is the relational operator. I was generous this time, and did not deduct points for those answers, but next time I would require more precision. (Even this time, I probably would have deducted a partial penalty if the question had been worth more than 1 point.)

      The term "relational operator" is introduced on p. 34 in Chapter 1 of Deitel/Deitel. Note that while Deitel/Deitel use the term "relational operator" only for the operators <, <=, >, >=, and use the term "equality operators" to describe "=" and "!=", other textbooks use "relational operators" to describe all of these, since they test the relationship between two quantities, the expression on the left hand side of the operator, and that on the right hand side of the operator. In every case, a true/false value (0 or 1) is returned.

      The term "relational operator" only appears in the index on p. 105 and p. 124, however the term is used on many other pages. Surprisingly, the index doesn't even point to p. 34 where the entire section is named "relational operator", or to p. 35 which contains a complete list of the relational operators; you'll find many other references to this term if you look for it.

      Note that *= is not a relational operator. It is an "assignment operator" (see Section 2.11 of Deitel/Deitel, p. 98.)

    4. (2 pts) List two relational operators that are in C++ but do NOT appear in this program (give the C++ symbols.)

      A strict reading of Deitel and Deitel would indicate that the only acceptable answers here are <=, >, and >=. I would also accept != and == for reasons stated above, and I think most computer science professors would also accept those answers.

      The symbol = is not an acceptable answer, since the single equals sign is an assignment operator, not a test of the relationship between the left and right hand sides. Also unacceptable are the symbol , the symbol , and the symbol , since none of those symbols exists in the C++ language. These symbols are acceptable in pseudocode, but when writing C++, or answering a question about C++, you need to specify those three operators using two characters each, as >=, <=, and !=, respectively.

      For answers such as "+=", "/=", etc., see the comments on part c above.

 

Question 9

(1 pts) What symbol is used for the stream insertion operator in C++?

The correct answer is "<<". The symbol ">>" is used for the stream "extraction" opearator.

Question 11


(2 pts) In the C++ statement a = b + 7 * 4;
what is the right operand of the addition operator?

Correct answers are either "7 * 4" or "28". Note that the question asks for the right operand of the "addition operator". That means the plus sign. The answer "7" is not correct because the multiplication operator (*) is applied first.

Question 14

(1 pts) In the expression 100101 x 210010, which part is the mantissa?

(a) 100101 (b) x (c) 2 (d) 10010

It was clear from the responses to this question that I did not adequately cover this material in lecture, so
I threw this question out. The correct answer is (a). We'll discuss this concept in
lecture (it has to do with the internal representation of float and double in C and C++).

Question 16

 

Suppose you have a C++ program in a file named lab03.cpp
What Unix command do you enter to perform each of the following operations?

The exam may not have been clear on this point, but I'll try to be more clear next time: when I ask for a Unix command, I want the full Unix command. For example, the correct answer to (b) is either "cat lab03.cpp" or "more lab03.cpp". Just typing "cat" or "more" is not sufficient. I accepted the shorter answers this time, but next time I won't.

  1. (2 pts) Enter a text editor to make changes to the program
  2. (2 pts) Display the contents of the source code on your screen.

    Correct Answers: cat lab03.cpp or more lab03.cpp

    pwd is not a correct answer (-2).
  3. (2 pts) Compile the program
  4. (2 pts) Copy the program to a new file called lab03b.cpp.

    A correct answer is "cp lab03.cpp lab03b.cpp".

    Another correct answer is "cp lab03.cpp ./lab03b.cpp". The ./ means "the file is located in the current directory". Since that's the default anyway, the ./ is not needed here, but it doesn't hurt either.

    However, "cp lab03.cpp .lab03.cpp" will copy the file to a "hidden file" (one where the file name starts with a dot) called ".lab03.cpp". This is an incorrect answer receiving partial credit only (-1).

    The response "cp lab03.cpp > lab03b.cpp" may "seem" right, however it is incorrect. Try it out, and see what happens. (Partial credit, -1). On the other hand, "cat lab03.cpp > lab03b.cpp" would work just fine. For your reflection: why is that?

    The answer "cp ~/lab03.cpp ~/lab03b.cpp" received only partial credit (-1) because that command will only work if both files are in your home directory.

    The answer "cp lab03.cpp lab03b.cpp ." received only partial credit (-1). Try it on the system, and you'll see that it doesn't work. For your reflection: what does this command actually tell the cp utility to do?

    The answer "mv lab03.cpp lab03b.cpp" received only partial credit (-1) because this does a "rename", not a copy.

    The answer "cp lab03b.cpp lab03.cpp" received only partial credit (-1) because it is backwards.

    The answer "copy lab03.cpp lab03b.cpp" received no credit (-2) since "copy" is not a Unix command.

    The answer "cp -r (program address) ." received no credit (-2) since this doesn't address the specific question of how to copy lab03.cpp to lab03b.cpp.