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.
Here's the text of the question:
Here's a complete answer (note that there are many possible answers).(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
drawLin 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
drawLomitted), 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.)
// 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:
Number conversions:
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.
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.
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;
}
|
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
a = mysteryFunc(b,c) * 2; (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).
// 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;
} |
E
n
t
e
r
x
:
5
*
*
*
Another correct answer:
| * | * | * | |||||||||||||||||
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.)
For answers such as "+=", "/=", etc., see the comments on part
c above.
(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.
(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.
(1 pts) In the expression 100101 x 210010, which part is the mantissa?
| (a) 100101 | (b) x | (c) 2 | (d) 10010 |
float and double in C and C++).
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.
cat lab03.cpp or more lab03.cpppwd is not a correct answer (-2).lab03b.cpp.