Final Exam Solutions C Programming Dr. Beeson, Spring 2009

advertisement
Final Exam Solutions
C Programming
Dr. Beeson, Spring 2009
May 20, 2009
Instructions: Please write your answers on the printed exam. Do not turn in any extra pages. No
interactive electronic devices of any kind are allowed during the exam (no computer, no calculator).
You can use your music player (if nobody else can hear it) if you put it on shuffle and don’t punch
any of its buttons during the exam. The exam is open notes, open book–you can use any printed
materials that you brought with you. Scoring: Four points per problem, total 100.
1. What will be printed by the following code?
void main(void)
{ printf("%d",’a’);
}
Answer : 97 will be printed, the ascii code of character ’a’.
2. Correct the following code, if necessary, to remove run-time or compile-time errors. Change the
code as little as possible. If no change is needed, just say so.
void main(void)
{ char x[3];
//
strcpy(x,"cat");
}
should be x[4] to leave room for the null terminator.
3. Accounting software has to deal with money in amounts exceeding a trillion dollars and yet keep
the arithmetic accurate to the penny. What kind of number that C supports would be adequate?
(Make your choice bearing in mind that the code must run on all the different computers common
in business today, e.g., you can’t assume that it only has to run on your own state-of-the-art 64-bit
dual quad-core machine.)
(a) int No, because the largest int is usually 4 billion.
(b) long No, for the same reason
(c) long long Yes, it’s big enough. This is the correct answer.
(d) double No, because we can’t guarantee the accuracy.
4. What will be printed by the following code?
void main(void)
{ printf("%d", 0x0f);
}
// 15 will be printed
1
5. What will be printed by the following code?
void main(void)
{ printf("%d", 1 << 3);
}
// 8 will be printed
6. What will be printed by the following code?
int f(int x)
{ ++x;
return x;
}
void main(void)
{ printf("%d",f(3));
}
// 4 will be printed.
7. Correct the following code so it fulfills its specification. Change the code as little as possible. If
it’s already correct, just say so.
int count(char c, char *x)
// return the number of characters in string x that are equal to c
{ char *t;
int ans = 0;
for(t=x; t; t++)
// Need to change ; t ; to
; *t ;
{ if(*t = c)
// Need to change = to ==
++ans;
}
return ans;
}
8. Fill in the missing two lines in the following code to match the comments given.
void main(int argc, char *argv[])
// takes two integers on the command line and prints their sum.
// Example: Assuming the executable file is ’foo’, foo 34 40 prints 74
{ int n,m;
sscanf(argv[1], "%d", &n);
// get n from argv[1]
sscanf(argv[1],"%d",&m);
// get m from argv[2]
printf("%d",n+m);
}
9. 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; // No, you can’t write to an uninitialized pointer
(b) char x[128]; // Yes, this will do
(c) char *x[80]; // No, this declares an array of 80 pointers that don’t point to space we own
10. Suppose x is an array of integers, and we have just executed this code:
2
for(i=0;i<5;i++)
x[i] = i*i;
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, same as x
(c) *x
0, same as x[0]
(d) x[1]
1
(e) &x[1]
4534 (four bytes from &x[0] )
(f) x+2
4538 (four more bytes)
(g) *(x+2)
4, same as x[2]
(h) *(&x[2] +1)
9, same as x[3], which is *(x+2+1).
11. What function from the standard C library is this? strcpy.
void mysteryFunction(char *x, char *y)
{ char *marker, *marker2;
for(marker = x, marker2=y; *marker2; ++marker2, ++marker)
*marker = *marker2;
*marker = *marker2;
}
12. By what means is an “object file” produced?
(a) By the compiler
13. If you get an error message ”unresolved external f” while trying to build your C program, what
software produced that message?
(b) the linker
14. Consider this code fragment:
int x[5];
x[6] = 5;
Assuming that this fragment exists in otherwise correct code, will the error in the second line cause
(c) a runtime error, because you don’t own the space at x[6], or even at x[5] for that matter.
15. Declare an array of three strings and initialize it to contain the three strings cat, dog, giraffe.
char *animals[3] = {"cat", "dog", "giraffe"};
16. What will be printed by the following code?
#define CUBE(x) x*x*x
void main(void)
{ printf("%d", CUBE(2+2));
}
3
Answer : 12 will be printed. The macro expands to 2+2*2+2*2+2 since there are no parentheses
around the x in the macro definition. That expression evaluates to 12, not the cube of 2 + 2.
17. What will be printed by the following code? Answer : 5, the final value of i.
void main(void)
{ int i;
for(i=0;i<5;i++)
;
printf("%d", i);
}
18. What will be printed by the following code? Answer : 3, the value of i when execution hits the
break.
void main(void)
{ int i;
for(i=0;i<5;i++)
{ if(i==3)
break;
}
printf("%d", i);
}
19. Recall the definition of a simply linked list of integers that we studied:
typedef struct L { int data;
struct L *next;
} intlist;
If x points to a list of three nodes containing 32, 54, and 25, respectively, then what will be the
value of x->next->data?
(b) 54
20. Write a function to negate the data in each node of a linked list. For example, if x points to
(32, 54, 25), then after running ListNegate(x) the list would be changed to (-32, -54, -25).
void ListNegate(intlist *x)
{ intlist *marker;
for(marker = x; marker; marker = marker->next)
marker->data *= -1;
}
21. Recall the sample Makefile that we studied (it is printed below). Show the changes in this
makefile that would be required when we add one more source file kepler.c, and that file calls the
mathematical functions sin and cos, so the math library is needed. (So gcc needs to get the option
-lm, for “library, math”.) Indicate your changes in this copy of the Makefile:
4
#sample Makefile for cs49
BASE =
/home/beeson/cs49
CC
=
gcc
CFLAGS = -O -Wall
// add -lm
to this line
EFILE = $(BASE)/bin/test_add
INCLS = -I$(LOC)/include
LOC
= /usr/local
OBJS = main.o add.o
// add kepler.o to this line
$(EFILE): $(OBJS)
@echo "linking ..."
@$(CC) $(CFLAGS) -o $(EFILE) $(OBJS)
$(OBJS): add.h
#@echo "compiling ..."
#$(CC) $(CFLAGS) $(INCLS) -c $*.c
22. Which of these lines of code opens a text file for reading and writing?
(b) FILE *fp = fopen("myfile","r+");
23. When fgets is called to read a line from a file into a character buffer, will the newline at the
end of the line be put into the buffer?
(c) Well, usually, or at least often, but not always. It depends. If the line is longer than the
number of bytes you told fgets to read, then the newline won’t be read, so it won’t be in the buffer.
But if the line was short, it will be read, and put into the buffer. This was the most-missed question
on the exam. I guess I should have put some test cases into the ChangeGrade assignment with the
maximum permitted number of characters, so that you would have encountered this behavior of
fgets.
24. If we are reading a file line by line with fgets, how do we know when we are finished with the
whole file?
(a) fgets returns EOF
Can’t be right, because EOF is an integer, and fgets returns a
pointer to char. It is fgetc that can return EOF.
(b) fgets returns NULL
This is the answer.
25. The function void qr(int n, int m, int *q, int *r) is designed to compute the quotient
and remainder (q and r) of n on division by m. For example, the quotient and remainder of 25 on
division by 7 are 3 and 4, since 3 times 7 is 21, which is 4 less than 25. How would you use qr? Fill
in the missing lines of code:
void main(void)
{ int n = 25;
int m = 7;
int q, r;
qr(n,m,&q,&r);
// This is the answer
printf(q = %d and r = %d", q, r); // prints "q = 3 and r = 4",
}
5
the values from the example.
Download