Unformatted text preview:

CMSC 212 - FINAL EXAM - Spring 2005 - Hollingsworth/Plane - p. 1 of 11 CMSC 212 FINAL EXAM (Spring 2005) Name _________________________ Signature ________________________ Discussion Section Time (circle one): 12:00 1:00 2:00 3:00 Asad Konstantin (1) This exam is closed book, closed notes, and closed neighbor. No calculators or other references 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. Question Possible Score 1 20 2 11 3 15 4 12 5 10 6 12 7 20 Total 100CMSC 212 - FINAL EXAM - Spring 2005 - Hollingsworth/Plane - p. 2 of 11 1.) [20 points] a) Explain the purpose of C's preprocessor and then give two different preprocessor directives (with explanation of their purpose. b) What is the purpose of each of these compiler switches? Your answer must be correct regardless of what other switches appear on the same compilation command line. 1. -o __________________________________________________________ 2. -O ____________________________________________________________ 3. -c ___________________________________________________________ 4. -g ____________________________________________________________ c) According to the code segment in the first column - write the type of each of the given expressions in the third column. (i.e. Integer, Integer pointer, address of an integer pointer, etc) If you can not tell from the code given, you must write the word "unknown". You do not need to worry about what value (or even if there is space allocated) - just what type it would be. T *(&a) &b q int a, b; int* t, q; *t d) Given one example of a situation where optimizing wall time (vs. process time) makes more sense and one where optimizing process time (vs. wall time) makes sense.CMSC 212 - FINAL EXAM - Spring 2005 - Hollingsworth/Plane - p. 3 of 11 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: #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; }CMSC 212 - FINAL EXAM - Spring 2005 - Hollingsworth/Plane - p. 4 of 11 3.) [15 points] Write the assembly code for the following function (following the stack conventions of the machine as discussed in class. You may pass parameters in the registers starting at register #2): void foo(int x, int y) { if (x > 0) { foo(x-1, y); } else if (y > 0) { printf("%d\n", y); } }CMSC 212 - FINAL EXAM - Spring 2005 - Hollingsworth/Plane - p. 5 of 11 4.) [12 points] Write a makefile that builds the programs with the following dependencies. A solid line indicates a file included by another file and a dash line indicates a file that should be built from one or more files. You must write the makefile in such a way that typing the command make at the command line will create both executable files based on the correct versions of the files in the current directory. private1.h common.h private2.h helpers.c myStuff.c yourStuff.c yourStuff.o myStuff.o helpers.o prog1 prog2CMSC 212 - FINAL EXAM - Spring 2005 - Hollingsworth/Plane - p. 6 of 11 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). If either of the pointers passed as the parameters have the value NULL, the function should return the value NULL. Other than the case where the pointer itself is NULL, you may assume that the strings passed are valid C strings with appropriate termination. If the sub-string is not present in the full-string, the function should also return the value NULL. char * strstr(const char * full, const char* sub);CMSC 212 - FINAL EXAM - Spring 2005 - Hollingsworth/Plane - p. 7 of 11 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. b) Give one benefit of each comparing C and Fortran90’s choices in representing and storing arrays. c) Give two examples of situations where you might want a Java program to call one or more functions written in C. 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.CMSC 212 - FINAL EXAM - Spring 2005 - Hollingsworth/Plane - p. 8 of 11 7.) [20 points] Write the following routines: void *myMalloc(int size); void *myCalloc(int num, int size); void myFree(void *ptr); These routines must mimic the C library routines malloc, calloc and free respectively. Your are given a function void *add10Meg() that grows the memory of your process by 10 megabytes at a time, and returns a pointer to the


View Full Document

UMD CMSC 212 - FINAL EXAM

Download FINAL EXAM
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view FINAL EXAM and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view FINAL EXAM 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?