Final Test with Resolution - C Based Programming and Software Tools | CSC 230, Exams of Computer Science

Material Type: Exam; Class: C and Software Tools; Subject: Computer Science; University: North Carolina State University; Term: Spring 2006;

Typology: Exams

Pre 2010

Uploaded on 03/18/2009

koofers-user-hny
koofers-user-hny 🇺🇸

5

(1)

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSC 230 Section 001
Final test
May 2, 2006
Questions Q1-Q6 carry 3 marks each. Indicate your answer by circling “a”, “b”, etc. Circle all the correct
answers, for each qu estion. Q7 carr ies 14 marks. Q8 carries 8 marks. Q9-Q10 each carry 20 marks. Q11
carries 25 marks. Although this adds up to 105, a perfe ct score is 100. If you obtain more than 100, your
marks will be added up to 100 and will be considered 100%.
It should be possible to answer the questions in the space provided. Us e the back of the paper if you
absolutely have to. You may not attach extra sheets. 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, wha t
exactly is the assump tion you made, and then answer the question correc tly 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 assump tion that simply goes against a stated condition of the question; that
will earn you no score.
This is a three 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
pf5

Partial preview of the text

Download Final Test with Resolution - C Based Programming and Software Tools | CSC 230 and more Exams Computer Science in PDF only on Docsity!

CSC 230 Section 001

Final test

May 2, 2006

Questions Q1-Q6 carry 3 marks each. Indicate your answer by circling “a”, “b”, etc. Circle all the correct answers, for each question. Q7 carries 14 marks. Q8 carries 8 marks. Q9-Q10 each carry 20 marks. Q carries 25 marks. Although this adds up to 105, a perfect score is 100. If you obtain more than 100, your marks will be added up to 100 and will be considered 100%. It should be possible to answer the questions in the space provided. Use the back of the paper if you absolutely have to. You may not attach extra sheets. 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 three 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. When the statement i += 2; is executed, it results in the value of i :

a. Being incremented by 1

b. Being decremented by 2

c. Being doubled

d. Becoming zero

e. None of the above

Q2. The statement extern int i; has the effect of:

a. Always defining a new variable i

b. Finding a global variable i in the same or some other file and using it, and

defining a new variable if there is no such global variable

c. Finding a global variable i in some other file and using it, otherwise causing a

linker error

d. Always causing a linker error

e. Possibly causing a compiler error, but never a linker error

Q3. When the statement i = (double) j + 2; is executed, it results in:

a. The value of i being modified

b. The value of j being modified

c. The value of 2 being modified

d. A program crash, always

e. Decreasing the value of i, possibly

Q4. The statement p = q++; :

a. Always makes sense if p and q are of type short

b. May make sense if p and q are pointer variables

c. Never makes sense if p and q are of type long

d. Never makes sense if p and q are pointer variables

e. Changes the value of both p and q , when executed

Q5. The pair of statements p = malloc (5000); free (p); occur as shown,

without any intervening code, in a program. When this code is executed, the free()

a. May cause a program crash

b. Cannot possibly cause a program crash

c. May result in the memory NOT being freed

d. Will always result in a program crash

e. None of the above

Q6. A while{} block in a program

a. May not be possible to replace by a for(){}block that is precisely equivalent

b. Can always be replaced by a for(){}block that is precisely equivalent

c. Can never be replaced by a for(){}block that is precisely equivalent

d. Can be replaced by a for(){} block but not a do{}while() block

e. All of the above

Q8. The following code is obviously syntactically not correct. It is intended that the

behavior of the loop will be controlled with preprocessor directives, using conditional

compilation. The intended behavior is: the loop should be traversed with values of i

going from 0 to max- 1 if the macro REVERSE is not defined, but should be traversed

in reverse if the macro is defined. Insert the appropriate pre-processor directives at the

appropriate places required to achieve this functionality.

void func1 (double *p, int max) { int i; double q; #ifndef REVERSE for (i=0; i<max; i++) #else for (i=max-1; i>=0; i--) #endif { q = p + i; / some code utilizing q, etc.... */ } return; }

Q9. Consider the following function, which is intended to strip trailing blanks from a

given string. The code is incomplete, and would not operate correctly. Insert code

ONLY inside the body of the while loop so that the function will perform the desired

task.

char *stripTrailingBlanks (char s) { / Given a pointer to a string, modify the original string so that if there were any trailing blanks, they are removed. Return pointer to original string. / char p; p = s + (strlen (s) - 1); / Point to last character / while (p == ' ') { / Scan backward as long as we find trailing blanks / / Insert code here ONLY */

*p = '\0';

p--;

if (p < s) {break;}

return (s); }

Q10. Consider the following function, which is meant to locate if the value x occurs in

the first n values in the block of doubles pointed to by p. Suppose the line of code in

bold were removed. Briefly comment on how the behavior of the function would change,

and whether the function would still return correct values in all cases or not.

int doesBlockContain (const double p, double x, unsigned int n) { / Given a pointer p to a block of doubles and an integer number n, return 0 if any of the first n doubles pointed to by p are equal to x, 1 if not. / int i; int return_value = 1; for (i=0; i<n; i++,p++) { if (p == x) { return_value = 0; break; } } return (return_value); }

Q11. The code below shows three arrays being utilized for keeping the population, area,

and inter-city distances for n citites. In the place indicated, insert the code to allocate

memory dynamically for these arrays, using the pointers already defined, such that the

array accesses later in the code will work. Also, initialize all array entries to zero.

void allocate_and_init_cities (int n) { long *population, area, distance; / Insert code to allocate memory and initialize HERE / if ((population = calloc (n, sizeof (long))) == NULL) { exit (EXIT_FAILURE); } if ((area = calloc (n, sizeof (long))) == NULL) { exit (EXIT_FAILURE); } if ((distance = calloc (nn, sizeof (long))) == NULL) { exit (EXIT_FAILURE); } / Other code, possibly to read in values / / Using values below */ for (i=0; i<n; i++) { printf (“Population of City %d is %d, area is %d\n”, i, population[i], area[i]); for (j=0; j<n; j++) { printf (“Distance from City %d to City %d is %d\n”, i, j, distance[n * i + j] } }

Currently it scans the block of memory pointed to by p

upto the first element which is equal to x, or upto the

n-th element, whichever is less. With the change, it

would scan exactly upto n elements, each time.

It would return the correct value in all cases.

Naturally, it might crash if the scan carries it into

unallocated memory; this is also true without the

change. It remains the responsibility of the calling

function to supply appropriate value for n.