MidTerm Examination 1. Explain briefly what is meant by saying that

COMP 170
1.
MidTerm Examination
Name:_____________________________
Explain briefly what is meant by saying that a function called search is
recursive.
(10 points)
A function is recursive if it invokes (calls) itself, either directly or indirectly, usually
for a smaller argument that it was originally invoked for.
Discussions of the appropriateness of the name “search” were not relevant.
2.
Recode the following program fragment without using a for statement:
double
total = 0;
for (int ctr=0; ctr < items.Length; ++ctr)
total += items[ctr];
Your version must produce exactly the same result.
(13 points)
The for statement controls iterative or repeated execution of a statement or a block
of statements. The only other way we know to do that is through the while
statement. We observed in class that anything you can do with for, you can also do
with while, although for is more compact and usually easier to read. Here’s an
obvious possibility:
double total = 0;
int
ctr
= 0;
while (ctr < items.Length)
{total += items[ctr];
++ctr;
}
3.
Is the following statement true in all cases?
“Any program that works is better than any program that doesn’t work”
Describe a situation in which it might not hold. In other words what program
quality criteria might be violated by a program that still produces the right
results?
(14 points)
A program might produce the correct output, but still have poor internal quality
(hard to understand, hard to modify, repetitive, poorly organized, etc.). On the other
hand, a program with one or two bugs might be well organized and easy to fix.
4.
Explain why console dialogs (“Please enter … ” etc) are usually not the best
way of obtaining test data. (Hint: There are at least two reasons.)
(15 points)
First, it requires a human operator, possibly the original programmer.
Second, there’s no reliable way to re-run a test session later (if the program is
changed or someone suspects a malfunction).
Third, it’s usually more work to set it up—(parsing input, etc.)
You got full credit for any answer that touched on two of those or any other plausible
reason.
MidTerm Examination
COMP 170
5.
Name:_____________________________
What’s wrong (either erroneous or poor practice) with these code fragments? If
nothing is wrong, just answer “nothing”.
(6 points each)
a.
if
{
(totalSale <= creditLimit)
return true;
}
else
{
return false;
}
I sometimes include a “gift” question that’s so obvious that everyone should
get it right, and it will bolster students’ confidence. This one was virtually a
duplicate of the simple example in the one-page October 14 reading
assignment:
http://www.idinews.com/peeves/boolset.html
In addition we discussed a similar example in class. Surprisingly, not
everyone got it right. I wonder whether those who missed this question either
(a) didn’t bother to read the assigned item or (b) didn‘t understand it or
disagreed with it but nevertheless failed to ask about it. I gave a generous 2
points to those who claimed there was nothing wrong, because the code does
produce the correct result. However, four or eight lines of code to produce the
effect of one line is considered very poor programming practice and
contributes to the high cost of program maintenance.
b.
while (tbl[k] = 0)
++k;
//
Find first non-zero element
We noted that a common source of error in the C-family is using the assignment
operator (=) when the relational equals (==) operator was intended. In C++ this code
will set the first (kth) element of tbl to zero and then quit. C# will probably give a
compiler error message, because it expects a condition (Boolean value).
6.
Describe the special role of the function Main() in a C# program. (10 points)
For a console application (a program invoked from the operating system’s command
line or an editor like Xamarin) execution always begins with Main(). Main() is the
entry point of the complete program. Main() then controls the top-level program
execution, invoking other functions as appropriate.
7.
Explain how hexadecimal representation of numeric data items can be useful
to a programmer
(6 points)
It’s very hard to read, remember, or transcribe the internal representation of a data
item, usually a binary integer in most modern computers. Hexadecimal (base 16)
provides a compact shorthand notation that (a) requires only ¼ the number of
“digits” as bits and (b) is easily converted at sight to and from binary.
COMP 170
8.
MidTerm Examination
Name:_____________________________
Design and code a static function to find the smallest element in a onedimensional array of long values.
(20 points)
(Note: If you’ve forgotten some essential detail (statement syntax, etc.) write
comments at the appropriate spots describing what you would like to do
or what specific result you’d produce if you remembered exactly how. )
The answer to this problem is part of assignment 6 to be discussed, if necessary,
November 11. But be sure your answer includes the specified function.