Docsity

Midterm Exam with Solution - Computer and Software Tools | CSC 230, Exams of Computer Science

Toggle sidebar
bg1
CSC 230 Section 001
Midterm test
February 14, 2007
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 wher e you
are specifically ask ed to. Otherwis e, do not use the back of the paper or attach extra sheets unless you
absolutely have to. You may consult any books, no tes, etc., but no other person. You must not u se a
cellphone, comput er, PDA, or any other information processing device.
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: __________________________________________
Answers
Grading Remarks
pf3
pf4
pf5

Partial preview of the text

Download Midterm Exam with Solution - Computer and Software Tools | CSC 230 and more Exams Computer Science in PDF only on Docsity!

CSC 230 Section 001

Midterm test

February 14, 2007

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.

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: __________________________________________

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 ____________.

  1. “function definitions” 2. “function declarations” (b) Three distinct stages of building an executable program are ____________, _____________, and _____________. (c) If the return value of a function is not specified by the source code defining or declaring that function, the compiler assumes that the function returns a _______.
  2. “int” 2. “float” 3. “char” 4. “string” 5. “nothing; this is rejected by compiler” (d) The preprocessor directive ______________ causes the header file named after the directive to be bodily included in the source code before the result is passed to the compiler. (e) The process by which the compiler groups successive keywords, literals, identifers, punctuators in preparation to parsing the source file is called ___________________. Ans: Ans: It is the variable called i, defined at the very beginning of the main() function, and has scope local to the entire function main() Total 9. If name or scope is missing, deduct 3 in each case. If scope is incomplete, e.g. simply “local” without specifying block, deduct 1 or 2 2 Total 6 marks. 6 if correct, 0 if incorrect. 2 1 preprocessing compiling linking 1 #include include (without #) is also okay tokenization

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();

  • does not work. Because long may be no larger than int. This solution should earn part, but not all, marks, say 7. If assumption that LONG_MAX is greater than INT_MAX is explicitly stated, then full marks. Failing to cast as double and just doing if (INT_MAX/j < i) abort();
  • does not work, because this becomes integer division with the fractional part truncated, so fails to check the original condition. This solution should earn part marks, say 4. Actually, this works just fine; it was a mistake on my part to say that this was wrong! Because INT_MAX is itself an int, and integer division always truncates, it will be an equivalent condition.

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.