Docsity

Midterm Makeup Test | C and Software Tools | CSC 230, Exams of Computer Science

Toggle sidebar
bg1
CSC 230 Section 001
Midterm Makeup test
April 4, 2006
Questions Q1 โ€“ Q3 each carries 15 marks. The maximum possible score is 45, considered 100%.
It should be possible to answer the questions in the space provided. Us e the back of the paper or attach
extra sheets only if you absolutely have to. You may consult any books, notes, etc., but no other person.
You must not use a cellphone, computer, PDA, or any other information processing device.
All effort has been made to make th e test completely unambiguous. However, should you still face a
dilemma, you should simply make the assumption that seems most reasonable to you. In your answer,
write down briefly and clearly the ambiguity in the question that requires you to make an assumption, what
exactly is the assump tion you made, and then answer the question correctly based on that assumption. You
will be graded on the basis of that assumption.
Caution: you are only privileged to make an assumption when the question leaves som e pertinent point
unstated. You cannot make an assump tion that simply goes against a stated condition of the question; that
will earn you no score.
This is a one hour test.
First Name: _____________________________
Last Name: _____________________________
Student ID: _____________________________
Pledge of Honor: I pledge on my honor that I have answered the questions
in this test purely on my own individual ability, and have neither given help
to nor received help from another person.
Signature: __________________________________________
pf3
pf4

Partial preview of the text

Download Midterm Makeup Test | C and Software Tools | CSC 230 and more Exams Computer Science in PDF only on Docsity!

CSC 230 Section 001

Midterm Makeup test

April 4, 2006

Questions Q1 โ€“ Q3 each carries 15 marks. The maximum possible score is 45, considered 100%. It should be possible to answer the questions in the space provided. Use the back of the paper or attach extra sheets only if you absolutely have to. You may consult any books, notes, etc., but no other person. You must not use a cellphone, computer, PDA, or any other information processing device. All effort has been made to make the test completely unambiguous. However, should you still face a dilemma, you should simply make the assumption that seems most reasonable to you. In your answer, write down briefly and clearly the ambiguity in the question that requires you to make an assumption, what exactly is the assumption you made, and then answer the question correctly based on that assumption. You will be graded on the basis of that assumption. Caution: you are only privileged to make an assumption when the question leaves some pertinent point unstated. You cannot make an assumption that simply goes against a stated condition of the question; that will earn you no score. This is a one hour test.

First Name: _____________________________

Last Name: _____________________________

Student ID: _____________________________

Pledge of Honor: I pledge on my honor that I have answered the questions

in this test purely on my own individual ability, and have neither given help

to nor received help from another person.

Signature: __________________________________________

Q1. Consider the following complete program (intended to be treated as a single file and compiled into a binary executable). In the table below, write down the output lines that the program would produce, in the order in which they would be produced. For each line of output, identify in the second column the line of code that produces that output. Remember that it may be impossible to predict the value printed out by the program in some cases, because the value is indeterminate and/or platform specific. In that case, state this in the third column. (The first two have been done for you, as examples.) #include <stdio.h> #include <stdlib.h> int iG; float func1 (short a, short b) { int i; float result = 1; a: printf ("received: %d, %d\n", a, b); for (i=0; i<b; i++) { result = result * a; } return (result); } int main (void) { int i; short base, power; double d_base, d_power, d_result; base = 200; power = 3; b: printf ("i = %d\n", i); c: printf ("iG = %d\n", iG); d_base = func1 (base, power); d_power = 2; d: printf ("d_base = %f\n", d_base); d_result = func1 (d_base, d_power); e: printf ("d_result = %f\n", d_result); }

Output Line Comment

i = 1234 b Value is indeterminate (local variable, uninitialized) iG = 0 c received: 200, 3 a Assuming short is at least 2 bytes d_base = 8000000.00 d received: 4, 2 a First argument is indeterminate, assuming short cannot hold 8000000 d_result = 16 e Value is indeterminate, because of above

Q3. Consider the following complete program, and answer the following questions. (a) In the box below, write down the output if this source code is passed through the preprocessing stage only (gcc โ€“ E or equivalent). You need only write down output corresponding to the line of code in bold. (b) The program appears to compute the total cost of a given number of toys specified by the user together with 5 existing toys. However, due to an error in the macro, the program will compute incorrect values. State what the error is and how the macro should be re-defined to remove this error. #include <stdio.h> #include <stdlib.h> /* Cost of toy only is 29.99, cost of battery is 19.99 extra */ #define TOTAL_COST(x) x * (29.99 + 19.99) int main (void) { int old_pieces = 5; int new_pieces; scanf ("%d", &new_pieces); printf ("Total cost of %d pieces is %f\n", (old_pieces + new_pieces), TOTAL_COST (old_pieces + new_pieces)); } printf ("Total cost of %d pieces is %f\n", (old_pieces + new_pieces), old_pieces + new_pieces * (29.99 + 19.99));

(b) The macro replacement causes the operator binding to change from the intended use.

Instead of the old and new number of pieces being added first , then multiplied by the sum

of the two cost components, only the new pieces is multiplied by the cost and the old

number of pieces simply added to it. This is because the multiplication in the body of the

macro takes precedence over the addition in the expression passed as the argument of the

macro.

This can be fixed by using a parenthesis:

#define TOTAL_COST(x) ((x) * (29.99 + 19.99))