


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



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.
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); }
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));
#define TOTAL_COST(x) ((x) * (29.99 + 19.99))