Académique Documents
Professionnel Documents
Culture Documents
5. If you have to compute with integers that will go up to about a hundred million, what data type should you use? Answer: (a) int 6. If you have to compute with integers that will go up to about a hundred billion, what data type should you use? Answer: (c) long long. On most machines int and long go up to only about 2 billion. 7. Exhibit the pattern of 32 bits that is used to store the integer -1. Answer: 11111111111111111111111111111111 8. Give a single printf command that will cause the following to be printed on TWO lines, with each line left-justied on the screen (without making any assumptions about what other printing commands might have just been executed). Number of giraffes: Number of elephants: Answer: printf("\n%s\n%s","Number of giraffes:","Number of elephants:"); 9. Suppose that price is an array of n doubles giving the price of silver for the years 1980-1991. Exhibit two lines of code that will print a nice two-column table of these prices, with the years in the left-hand column and the prices in the right-hand column. (Do not worry about making a title, just print the data.) The rst line should start with for and the second line with printf. Answer: for(i=0;i<n;i++) { printf("\n%d %10.2lf", 1980 + i, prices[i]); } 10. What will be printed by the following program? void f(int x) { x = x+1; } void main(void) { int x = 5; f(x); printf("\%d",x); } Answer: 5 will be printed. One doesnt even have to read to denition of f to answer this question, one only has to read main to see that f cannot aect the value of x.
11. What will be printed by the following program? void f(int *x) { *x = *x+1; } void main(void) { int x = 5; f(&x); printf("\%d",x); } Answer: 6 will be printed. Since the address of x is passed, the execution of f(&x) does aect the value of x. 12. Declare x to be an array of 1000 integers. int x[1000]; 13. Which of the following declarations would you use if you planned to read a string from the keyboard and put it in x? (a) char *x; (b) char x[128]; (c) char *x[80]; Answer: (b) 14. How many bytes will be allocated for an array of 100 longs? Answer: 400, assuming longs are 4 bytes each 15. What will be printed by the following code? void main(void) { char x[80] = "You know the answer?" *(x+1) = a + 5; *(x+2) = x[15]; x[4] = 0; printf("\%s",x); } Answer: Yfs. I had intended it to be Yes, but apparently, I cant count properly. 16. Suppose x is an array of integers, and we have just executed this code: for(i=0;i<10;i++) x[i] = i+1; Suppose that x[0] is stored at address 4530. What is the value of each of the following expressions? (a) x 4530 (b) &x[0] 4530 (c) *x 1 (d) x[1] 2 (e) &x[1] 4534; four bytes beyond x 3
(f) x+2 4538; this is the same as &x[2] (g) *(x+2) 3; the given expression simplies to x[2]. (h) *(&x[2] +1) 4; the given expression simplies to x[3]. 17. In the following code, identify the (a) local variables: sum, i,z,i (b) global variables: username (c) formal parameters: x, n (d) actual parameters: z, 10 Please write your answers above, not on the code below. { char *username = "Jack the Ripper"; double average( double *x, int n) { double sum=0.0; int i; for(i=0;i<n;i++) sum = sum + x[i]; return sum/n; } } void main(void) { double z[10]; int i; for(i=0;i<10;i++) z[i] = i; printf("\%d",average(z,10)); } 18. What function from the standard C library is this? int mysteryFunction(char *x) { char *marker; int count = 0; for(marker = x; *marker; ++marker) ++ count; return count; } Answer: this is strlen. 19. Is the following code for strcpy correct? If not, correct it. void strcpy( char *destination, char *source) { char *marker, *t; for(marker = source, t = destination; *marker; marker++, t++) *t = *marker; } Answer: This code fails to put a null terminator at the end of the copied string. To x that problem add one more line of code at the end: *t = 0;, or equivalently, *t = \0;
20. If you use a computer program to solve the equation x = cos x, representing numbers as doubles, which of the following would be the answer you could expect to get? (a) 0.73908513321516064165531208767387340401341175890076 (b) 0.73908513321516064165531208767387 (c) 0.739085133215161 (d) 0.739085 Answer: (c), since doubles have about 15 decimal digits of precision.