Final Exam, C programming

May 24, 2010

Rules: Open book, open notes, open any printed or handwritten material. No electronic devices (except a music player). If you use a music player nobody else must be able to hear it and you must not use its controls during the exams, just put it on shuffle. For the exam assume that the basic data types of C have the sizes they actually have on all common compilers today, even though the ANSI standard does not specify those sizes. Five points per problem, 21 problems, total 105 points. Solutions will be posted to the website Tuesday, and grades on Wednesday or Thursday. 1. The largest double is about how big? (c) About 10308 2. Suppose that we want to print a table of items and prices. The items are given in an array items of strings and the prices are in an array prices of doubles. Write code that will print the table in two columns, with the items left-justified in their column, and the decimal points of the prices vertically lined up, with exactly two digits to the right of the decimal point. Leading zeroes are printed only for prices less than one dollar. You do not need to know how many prices and items there are to write this code, so do not introduce a number or variable for that. Three of the five points are for correctly handling this issue. for(i=0;inext->next->data

9

(c) x ->next ->next->next

NULL

19. What is the value of the following expressions? (a) 0xfff2 ^ 0xfff3

1

(b) 0xfff2 | 0xfff3

0xfff3

(c) 0xfff2 & 0xfff3

0xfff2

(d) 0xfff2 && 0xfff3

1

(e) 0xfff2 || 0xfff3

1

20. Fill in the blanks in the following code int main(void) { FILE *fp; // declare a variable to use fp = fopen("Grades.txt","r+"); // open fseek(fp,0,SEEK_END ); // move to the fputs("George was here", fp); // write fclose(fp); // close the file }

for a file pointer the file "Grades.txt" for reading and modif end of the file "George was here!" at the end

21. Write a rule to use in a makefile that says, *.class files depend on *.java files, and whenever the .java file changes, you should run javac on that file to rebuild the class file. Indicate in a comment (that would be legal as a comment in a makefile) what character the second line begins with. *.class : *.java javac $*.java # second line begins with a tab character

5