Bristol Institute of Technology Academic Year: 09/10 Module Leader: Module Code: Title of Module: Ian Johnson UFCETS-20-1 Programming in C Examination Date: Monday 12 th January 2009 Examination Start time: 09:00 or 13:30 Duration of Examination:3Hours Instructions to Candidates: Candidates must answer ALL questions. Materials supplied to the candidate will be: Number of Examination Booklets per Examination 1 Number of sheets of Graph Paper size G3 (Normal) 0 Number of Pre-printed OMR (Multiple Choice Answer Sheet) 0 Additional Instructions to Invigilators: University approved calculators may be used Candidates permitted to keep Examination Question Paper Material supplied by student allowed (must be collected with answer booklet) please specify: Yes Yes No Additional Specialised Material : NONE Treasury tags & adhesive triangles will be supplied as standard
Question 1. Question 2. Question 3. Pick the odd one out and explain why: double bool int What value wouldisxdigit('f') return and why? Pick the odd one out and explain why? long short double Question 4. Explain the difference between the following literals in C. How much memory would each require? 'A' "A" (4 marks) Question 5. On a Pentium architecture computer running linux, how large in bits are the following variable types: a) unsigned char b) char * c) int d) int * e) long (5 marks) Question 6. In the laboratory you have used GVD the Gnu Visual Debugger. Why is such a tool required? (4 marks) Page 1 of 7
Question 7. Consider the following poorly written C program: 1. #include <stdio.h> 2. 3. main(int argc, char **argv) 4. { 5. int y; 6. 7. y = 4; 8. printf("%4.2f\n", root2(y)); 9. 10. } 11. 12. double root2(int *x) 13. { 14. double z; 15. 16. z = sqrt((double) *x); 17. return z; 18. } When compiled with gcc Wall exam06.c the following output is produced: exam06.c:5: warning: return type defaults to `int' exam06.c: In function `main': exam06.c:9: warning: implicit declaration of function `root2' exam06.c:9: warning: double format, different type arg (arg 2) exam06.c:11: warning: control reaches end of non-void function exam06.c: At top level: exam06.c:14: warning: type mismatch with previous implicit declaration exam06.c:9: warning: previous implicit declaration of `root2' exam06.c:14: warning: `root2' was previously implicitly declared to return `int' exam06.c: In function `root2': exam06.c:17: warning: implicit declaration of function `sqrt' /tmp/ccjtkwnt.o(.text+0x4e): In function `root2': : undefined reference to `sqrt' collect2: ld returned 1 exit status a) If the compilation was successful, what would the name of the generated executable be? (1 mark) b) Explain the errors and warning in the above gcc output, suggesting improvements to the program to fix them. (12 marks) c) Having fixed the program as discussed in (b), recompilation generates the following error: exam06.c: In function `main': exam06.c:8: warning: passing arg 1 of `root2' makes from integer without a cast Explain what has caused this error and how you would fix it. Page 2 of 7
d) If the program was fixed, what would be output on line 8? (1 mark) Question 8. Explain the meaning of the following operators in C and give an example of where each could be used. a)! b) ~ c) d) (8 marks) Question 9. What is the most common cause of a segmentation fault? Question 10. Consider the following C program: 1. 2. 3. #include <stdlib.h> 4. 5. void main() 6. { 7. char x; 8. 9. x = calloc(6,sizeof(char)); 10. 11. x[0] = 'H'; //fill buffer with string 12. x[1] = 'E'; 13. x[2] = 'L'; 14. x[3] = 'L'; 15. x[4] = 'O'; 16. x[5] = '\0'; 17. 18. printf("%s\n",x); 19. 20. return 1; 21. } Page 3 of 7
When compiled withgcc Wall ansi pedantic the following output is generated: foo.c:5: warning: return type of `main' is not `int' foo.c: In function `main': foo.c:8: warning: assignment makes integer from without a cast foo.c:10: subscripted value is neither array nor foo.c:10: parse error before '/' token foo.c:12: subscripted value is neither array nor foo.c:13: subscripted value is neither array nor foo.c:14: subscripted value is neither array nor foo.c:15: subscripted value is neither array nor foo.c:17: warning: implicit declaration of function `printf' foo.c:17: warning: format argument is not a (arg 2) foo.c:19: warning: `return' with a value, in function returning void Explain what the above warnings & errors mean, and how you would modify this program to prevent them. (12 marks) Page 4 of 7
Question 11. Consider the following C program: 01: 02: main() 03: { 04: int x = 3; // my variable 05: 06: if (x=1) 07: printf("hello\n"); 08: else 09: printf("world\n"); 10: } 11: When compiled with gcc Wall the following output is generated: foo.c:3: warning: return type defaults to `int' foo.c: In function `main': foo.c:6: warning: suggest parentheses around assignment used as truth value foo.c:7: warning: implicit declaration of function `printf' foo.c:10: warning: control reaches end of non-void function Explain what each of the four warnings mean, and how you would modify this program to prevent these warnings. (10 marks) Question 12. You wish to compute the cosine of 90 degrees a) Which header file would you need to include? b) Write a C statement to assign the result to a variable X. Question 13. What would you expect to find in a C header file (*.h)? (3 marks) Question 14. The C operator -> is a shorthand for a more complex notation. a) Explain the circumstances in which it would be used b) Write an example of the more complex notation Page 5 of 7
Question 15. Consider the following C program: #include <stdio.h> main() { int x; x = 0xffffffff; printf("%d\n",x); } What value will be printed? (1 mark) Question 16. Consider the following C fragment unsigned char x; x = 0xaa; What is the value of x in binary? (1 mark) Question 17. Consider the following declaration: int MY_VAR, i, total%, UFS001, long, &, total-val, z.height, *q, 1st, count; Which of the above variables would not be legally named in C? Explain why. (12 marks) Question 18. Consider the following C fragment: double x; int y=9; int z=4; x = y/z; /* statement (1) */ a) What value does x contain and why? b) Rewrite statement (1) to ensure the answer is 2.25 Page 6 of 7
Question 19. An unsigned char variable A has the value 187 decimal. a) What is the value in binary? b) What is the value in hexadecimal? c) Write C statements using only bitwise operators to: i. Divide the value by 16 ii. Set the most significant bit without altering any other bits iii. Clear the least significant bit without altering any other bits (5 marks) Question 20. In C, the function main traditionally has a standard declaration. a) Write a standard declaration for main. b) Explain what the arguments to main are, and how you would access its return value (5 marks) END OF QUESTION PAPER Page 7 of 7