



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 and Q4 carry 15 marks each, all other questions carry 10 marks each. This adds up to 80, but 75 is a perfect score. If you obtain more than 75, your marks will be added up to 75 and will be considered 100%. It should be possible to answer the questions in the space provided. Use the back of the paper where you are specifically asked to. Otherwise, do not use the back of the paper or attach extra sheets unless 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. This is a one hour test.
Answers Grading Remarks
Q1. Consider the following complete program. #include <stdio.h> #include <stdlib.h> int i = 42; int main(void) { int i = 1; int j = 10; do { /* some code here, not changing i or j / } while (--j > i); i++; { i += j; } i = i + 2 * j – i * j; if (ij > j) { /* let’s call this Block A */ int i; i = 9; i = i + 2 * j – i * j; } printf("%d\n\n", i); return 0; } (a) Which variable’s value is being printed out by the final printf() statement? Answer by identifying the scope of that variable, and the name of the variable. (b) What value will be printed out by the program when it is run? Q2. Fill in the blanks in the following statements, using the numbers corresponding to the options in brackets at the end, and writing in your own text if there are fewer options than blanks. (a) Typically header (.h) files contain __________, and source (.c) files contain ____________.
Q4. Given the declarations int i = 4; int j = 6; float f1 = 3.14159; find the values of each of the following expressions if it is evaluated immediately after the declarations. You must also indicate the type of each evaluated value (int, float, etc.). Expression Value Type j + f i + (j + f1) (i + (int)(j + f1)) (i + (int) j + f1) (int) (i + j + f1) Q5. A source file called new_test.c uses functions whose definitions are available in another source file called log_functions.c, and also uses functions that are available in the math library. It is desired to compile this file to an executable called new_test. Write a single command line function, using the compiler gcc, to perform this action. Q6. Write a statement that will cause the program to call a function abort() if the current value of two int variables i and j is such that multiplying these values together will result in a number greater than INT_MAX, the macro which indicates the maximum value an int variable can hold. Ans: Ans:
float float int float int gcc – lm new_test.c log_functions.c – o new_test If any of the components of the command are left out, deduct points. If other options such as – pedantic are also used, this is fine. If – c is used, this is not okay – deduct points. if ( (double) INT_MAX / (double) i < j) abort(); Other equivalent statements will do: however, note that if ( (long)i * (long) j > INT_MAX) abort();
Q7. Re-write the following segment of code using a do-while loop instead of a for loop, and without using any side-effects. You are not allowed to define any new variables. i and j are integer variables, and f is a float variable. All of these variables should have the same values when your loop terminates as at the termination of the original one. You are allowed to write at most one statement outside your loop body, either before the beginning or after the end. for (j=i; (f=j/(float)i) > 0.5;) { printf (“Now trying %d\n”, --j); } Write your answer to Q7 on the reverse side of the preceding page (i.e., the side facing this page). Use the bottom half of the page for this answer. j = i; do { f = j / (float)i; /* / if (f > 0.5) / Strictly not needed, since / break; / j equals i at beginning */ --j; printf (“Now trying %d\n”, j); f = j/(float)i; } while (f > 0.5); If new variables are used, deduct marks. There are several ways to go wrong in this one. Correct variations are okay, but there are not many simple yet correct variations. If the answer differs from this one, make sure it is correct (by using a small initial value for i, say 5) that it in fact does the right thing. The three lines marked by comments are logically required, but in this particular case, since i and j start out being the same, the loop will at least run once, so it is okay to eliminate those lines; no penalty.