Spring 2005 CSMC 212 Final Exam: C Programming - Prof. Jeffrey K. Hollingsworth, Exams of Computer Science

The final exam for the spring 2005 csmc 212 course at an unspecified university. The exam covers various topics in c programming, including the c preprocessor, data types, arrays, pointers, recursion, and memory management. Students are required to answer multiple-choice and programming questions.

Typology: Exams

Pre 2010

Uploaded on 02/13/2009

koofers-user-nx5
koofers-user-nx5 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 212 - FINAL EXAM - Spring 2005 - Hollingsworth/Plan e - p. 1 of 10
CMSC 212 FINAL EXAM (Spring 2005)
Name _GRADING DIRECTIONS_
Signature ________________________
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Spring 2005 CSMC 212 Final Exam: C Programming - Prof. Jeffrey K. Hollingsworth and more Exams Computer Science in PDF only on Docsity!

CMSC 212 FINAL EXAM (Spring 2005)

Name GRADING DIRECTIONS

Signature ________________________

Discussion Section Time (circle one): 12:00 1:00 2:00 3:

Asad Konstantin

(1) This exam is closed book, closed notes, and closed neighbor. No calculators are permitted. Violation of any of these rules will be considered academic dishonesty.

(2) You have 120 minutes to complete this exam. If you finish early, you may turn in your exam at the front of the room and leave. However if you finish during the last ten minutes of the exam please remain seated until the end of the exam so you don't disturb others. Failure to follow this direction will result in points being deducted from your exam.

(3) Write all answers on the exam. If you need additional paper, we will provide it. Make sure your name is on any additional sheets.

(4) Partial credit will be given for most questions assuming we can figure out what you were doing.

(5) Please write neatly. Print your answers, if that will make your handwriting easier to read. If you write something, and wish to cross it out, simply put an X through it. Please clearly indicate if your answer continues onto another page.

1.) [20 points]

a) Explain the purpose of C's preprocessor and give two different preprocessor directives (with explanation of their purpose.

[3 pnts] Makes a pass over the program substituting pre-processor directives with values or filles from other locations.

Question Possible Score

Total 100

2.) [11 points] Give all of the output of the following code on the lines given (it does compile and

run). If you have a situation where you have a pointer into non-allocated space, use ??? to

indicate that value.

OUTPUT goes on these lines

1 point for each value

up to -2 for extra values

or incorrect end of lines etc.

#include <stdio.h>

#include <malloc.h>

int f1(int *x){

*x = 2;

x = (int*)calloc(sizeof(int),1);

*x = 3;

return *x;

int* f2( int x, int **y){

int m= 8;

int *t = &m;

x += m--;

y = &t;

printf("%d %d %d\n",x,t,*y);

return t;

int main(void){

int a,b,c;

int *p, *q, *r;

a = 5;

p = &a;

b = f1(p);

printf("%d %d %d\n",a,b,*p);

c = 5;

q = &c;

r = f2( c,&q);

printf("%d %d %d\n",c,q,r);

return 0;

3.) [15 points] Write the assembly code for the following function (following the stack conventions of the machine):

void foo(int x, y) { if (x > 0) { foo(x-1, y); else if (y > 0) { printf(“%d\n”, y); } }

foo: Store R14 R15 0 Load R10 R0 # Negate R Add R15 R10 R Move R11 R Add R2 R10 R Negate R Bnn R11 skip Branch R14 R0 foo Branch R0 R0 skip skip1: Move R12 R Negate R Bnn R12 skip Output R skip2: Load R10 R0 # Add R15 R10 R Load R14 R15 0 Branch R0 R14 0

Grading: 2 points for correctly testing and branching on x > 0 2 points for correctly storing the x and y values 2 points for storing stack pointer 3 points for initiating the recursive call 2 points for restoring stack pointer 2 points for coming out of the recursion 2 points for the output when y > 0

5.) [10 Points] Implement strstr without using any array notation or any string library functions. (searches for the second parameter within the first and returns a pointer to the beginning of that substring as the return value of the function).

char * strstr(const char * full, const char* sub);

{ char *curr1, curr2; if (!full || !sub) return NULL; for (;full; full++) { for (curr1=sub, curr2 = full; *curr1 && curr2; curr1++, curr2++) { if (curr1 != curr2) break; } if (!curr1) return full; } return NULL; }

GRADING:

2 points for the NULL or NULL test (what to do if either or both are NULL) 4 points for the outer loop that traverses starting the comparison at each character of the full string 4 points for the innerloop that compares each element of the sub string to the corresponding elements in the full string

-4 if used array notation but otherwise it is completely working

6.) [12 points] Write a paragraph for each sub-question

a) Even though lcov reports 100% coverage of your program being tested, how could the program being tested still have bugs.

[3 points] Lcov reports statement level coverage information, but it is possible that different paths of execution have not been exercised even though all statements have run.

b) Give one benefit of each in C and Fortran90’s choices in representing and storing arrays.

[3 points] C’s representation is smaller and has less overhead. Fortran90’s allows for multi-dimensional arrays and allows for runtime detection of array bounds errors.

c) Give two examples of situations where you might want a Java program to call one or more functions written in C.

[3 points] You have a highly tuned library (like an mpeg encoder) written in C and you want to re-use this code as part of a new Java program. You need to call some machine specific code (i.e. to a low level graphics processor) which can’t be written in Java.

d) If you were to write code that was a Java function that was calling a C function, you would need to consider the possibility of them sharing data. When converting between the scalar types of Java (int for example) to the corresponding type in C - what is the primary difficulty.

[3 points] In Java, the int types have a well defined size (16, 32, or 64 bits), but in C the size of the int type is machine specific, so there would be concerns about the what could or could not be represented depending on the relative size of the ints.

void *myCalloc(int num, int size) { int i; char *ptr; ptr = myMalloc(num * size); for (i=0; i < num * size; i++) ptr[i] = ‘\0’; return ptr; }

GRADING [5 points]: 1 point for returning pointer 2 points for calling myMalloc on correct size 2 points for setting space to 0

void myFree(void *ptr);

{ boundry *head; head = ptr; head--; head->free = 1; }

GRADING [5 points]: 3 points for finding boundary tag 2 points for setting it free up to 2 points for errors if they tried to coalesce blocks, but had errors.