2642024SpringExam1
2642024SpringExam1
9-10:15AM, February 8
Remove the top sheet. Return only the top sheet.
Name:
In signing this statement, I hereby certify that the work on this exam is my
own and that I have not copied the work of any other student while completing
it. I understand that, if I fail to honor this agreement, I will receive a score
of ZERO for this exam and will be subject to possible disciplinary action.
Signature:
You must sign here. Otherwise you will receive a 1-point penalty.
Read the questions carefully. Some questions have conditions and restrictions.
Please keep the answer sheet clean. Do not use the answer sheet as your scratch space. The
answer sheet should contain only the answers.
Please use DARK ink. If your pen is too light, your answer may not be graded.
This is an open-book, open-note exam. You may use any book, notes, or program printouts.
No electronic device is allowed. You may not borrow books from other students.
1, Version C
Name:
Please write ONLY ONE answer in each box. If you write multiple answers in one box,
you will receive no point.
Q1 Q2 Q3
A
Q4 Q5
A
2, Version C
This page is blank.
3, Version C
1 Stack Memory
Consider the following C program.
1 # include < stdio .h >
2 # include < stdlib .h >
3 // assume the size of the every data type is 1 byte
4 int f1 ( int a , int b )
5 {
6 int t = a + b ;
7 a = 5;
8 b = 10;
9 return t ;
10 }
11
12 int f2 ( int a , int b )
13 {
14 int t ;
15 a = 7;
16 b = 11
17 t = f1 (a , b );
18 return a ;
19 }
20
21 int main ( int argc , char ** argv )
22 {
23 int a = 2;
24 int b = 4;
25 int c = 9;
26 c = f2 (a , b );
27 printf ("% d \ n " , c );
28 return EXIT_SUCCESS ;
29 }
Please fill the stack as the program runs.
DNC means “do not care”. You do not need to answer.
Each address has A as the prefix.
Please fill the stack memory when the program has just completed line 7, before executing
line 8.
4, Version C
Frame Symbol Address Value
t A302 Question A
b A301 DNC
f1 a A300 DNC
value address: Question B
return location: Question C
t A202 DNC
b A201 DNC
f2 a A200 DNC
value address: Question D
return location: DNC
c A104 9
b A103 4
main a A102 2
argv A101 DNC
argc A100 DNC
Answer:
A: 18
B: A202 (202 acceptable)
C: line 18
D: A104 (104 acceptable)
5, Version C
2 File
Consider the following program.
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < string .h >
4 // different ways to read a file
5 int main ( int argc , char ** argv )
6 {
7 if ( argc < 2) { return EXIT_FAILURE ; }
8 FILE * fptr = fopen ( argv [1] , " r ");
9 if ( fptr == NULL ) { return EXIT_FAILURE ; }
10 int ch ;
11 while (( ch = fgetc ( fptr )) != EOF )
12 {
13 printf (" ch = %d , % c \ n " , ch , ch );
14 }
15 // return the the beginning of the file
16 fseek ( fptr , 0 , SEEK_SET );
17 int val ;
18 while ( fscanf ( fptr , "% d " , & val ) == 1)
19 {
20 printf (" val = % d \ n " , val );
21 }
22 // return the the beginning of the file
23 fseek ( fptr , 0 , SEEK_SET );
24 char buf [80];
25 while ( fgets ( buf , 80 , fptr ) != NULL )
26 {
27 printf (" buff = % s " , buf );
28 }
29 fclose ( fptr );
30 return EXIT_SUCCESS ;
31 }
This is the input file
1 -23 456 7890
365 49 716
3
-18
9 7 1
Please write the program’s output.
6, Version C
ch = 49, 1
ch = 32,
ch = 45, -
ch = 50, Answer A
ch = 51, 3
ch = 32,
ch = 52, 4
ch = 53, 5
ch = 54, 6
ch = 32,
ch = 55, 7
ch = 56, 8
val = 1
val = Answer B
val = 456
Answer:
A: 2
B: -23
C: 3
D: -18
7, Version C
3 GDB
Consider the following program:
1 # include < stdio .h >
2 # include < stdlib .h >
3 int f1 ( int a )
4 {
5 return 0;
6 }
7
8 int f2 ( int a )
9 {
10 int x = 3;
11 if ( a == 0)
12 {
13 x = f1 ( a );
14 }
15 else
16 {
17 x = f2 ( a - 1);
18 }
19 return x ;
20 }
21
22 int main ( int argc , char * * argv )
23 {
24 int s = f2 (4);
25 printf (" s = % d \ n " , s );
26 return EXIT_SUCCESS ;
27 }
Here are some frequently used commands:
Suppose you run the following GDB commands (“(gdb)” is the prompt).
(gdb) b f2
(gdb) r
(gdb) bt
8, Version C
#0 f2 (a=4) at q1.c:10
#1 0x00005555555551b8 in main (argc=1, argv=0x7fffffffddd8) at q1.c:answer A
(gdb) delete 1
(gdb) b f1
Breakpoint 2 at 0x555555555154: file q1.c, line 5.
(gdb) info b
Num Type Disp Enb Address What
2 breakpoint keep y 0x0000555555555154 in f1 at q1.c:5
(gdb) c
(gdb) bt
Answer:
A: 24
B: 0
C: 0
D: 13
9, Version C
4 Makefile
Consider the following Makefile.
1 WARNING = - Wall - Wshadow
2 ERROR = - Wvla - Werror
3 GCC = gcc - std = c99 -g $ ( WARNING ) $ ( ERROR )
4
5 SRCS = main . c eliminate . c
6 OBJS = $ ( SRCS :%. c =%. o ) # main . o eliminate . o
7 # equivalent to OBJS = main . o eliminate . o
8 main : $ ( OBJS )
9 $ ( GCC ) $ ( OBJS ) -o main
10
11 .c.o:
12 $ ( GCC ) -c $ *. c
13
14 testall : test1 test2 test3
15
16 test1 : main
17 ./ main 6 3 > output1
18 diff output1 expected / expected1
19
20 test2 : main
21 ./ main 6 4 > output2
22 diff output2 expected / expected2
23
24 test3 : main
25 ./ main 25 7 > output3
26 diff output3 expected / expected3
27
28 clean : # remove all machine generated files
29 rm -f main *. o output ? *~
4.1 Question A
Suppose these are the two commands. What is the second command’s output?
Here $ is the prompt from the terminal.
$ make clean
rm -f main *.o output? *~
$ make
10, Version C
2. gcc -std=c99 -g -Wall -Wshadow -Wvla -Werror -c main.c
gcc -std=c99 -g -Wall -Wshadow -Wvla -Werror -c eliminate.c
gcc -std=c99 -g -Wall -Wshadow -Wvla -Werror main.o eliminate.o -o main
4.2 Question B
After the two commands above, type make immediately (without doing anything else), what
is the output?
4.3 Question C
In test1, what does > do?
2. Compare the output of the program ./main with the expected output.
11, Version C
4.4 Question D
Which is correct of the diff command?
1
2
3
4
2
3
4
< 1
< 2
< 3
< 4
> 2
> 3
> 4
Answer:
2
3
1
4
12, Version C
5 Programming Assignment 2: Who Gets the Cake
Consider the second programming assignment: Who Gets the Cake.
Suppose at the beginning, there are 7 people (marked as index 0 to 6), i.e., n is 7.
They count as 1, 2, 3 and the person marked 3 is removed, i.e., k is 3.
Please mark the indexes of the first four people that are eliminated.
Hint: The first answer should be 2.
Answer:
2
5
1
6
13, Version C