Final Sample
Final Sample
Final Sample
This is a sample final exam. The actual exam may/will differ from this one
in the number of questions and in topics covered by each question.
Name:
NetID:
Instructions:
Write your full name and your NetID on the front of this exam.
Make sure that your exam is not missing any sheets. There should be seven (6) double sided pages in the exam.
Write your answers in the space provided below each problem. If you make a mess, clearly indicate your final answer.
Answer all questions in this exam.
If you have any questions during the exam, raise your hand and we will try to get to you.
At the end of the exam, there are blank pages. Use them as your scrap paper. If you need additional scrap paper, raise your hand and we will get it for you.
This exam is closed books, closed notes, closed computers. You are allowed to have a single double sided sheet of paper with anything you wish on it. That sheet
should have your name on front and back.
You need to stay in your seat until the exam is finished. You should not leave the room even if you finish the exam. This distracts other students who are still
working.
Good luck!
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 201
Midterm Exam A
Page 3
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 201
Midterm Exam A
8. What is the content of array A after executing the following code snippet?
1
2
3
4
5
6
7
9. Consider the following C program. (For space reasons, we are not checking error return codes. You can assume that all functions return normally.)
1
2
3
4
5
6
7
void handler(sig)
{
val += 5;
return;
}
8
9
10
11
12
13
14
15
16
17
18
19
int main() {
int pid;
signal(SIGCHLD, handler);
if ((pid = fork()) == 0) {
val -= 3;
exit(0);
}
waitpid(pid, NULL, 0);
printf("val = %d\n", val);
exit(0);
}
(a) 10
(b) 15
(c) no output, since the parent process never returns
(d) cannot be determined, since it depends on scheduling of both processes by the kernel
10. For each point below circle the expressions that are always true (i.e. evaluate to 1 when they are executed in a C program).
We are running programs on a machine with the following characteristics:
Values of type int are 32 bits. They are represented in twos complement, and they are right shifted arithmetically. Values of type unsigned are 32 bits.
Values of type float are represented using the 32-bit IEEE floating point format, while values of type double use the 64-bit IEEE floating point format.
We generate arbitrary values x, y, and z, and convert them to other forms as follows:
/*
Create
some
arbitrary
values
*/
int x = random();
int y = random();
int z = random();
/*
Convert
unsigned
unsigned
double
double
double
ux
uy
dx
dy
dz
to
=
=
=
=
=
other
forms
*/
(unsigned) x;
(unsigned) y;
(double) x;
(double) y;
(double) z;
Page 4
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 201
Midterm Exam A
Header
Footer
31
2 1 0
__________________________________
|
Block Size (bytes)
|
|
|____________________________|_____|
|
|
|
|
|
|
|__________________________________|
|
Block Size (bytes)
|
|
|____________________________|_____|
Each memory block, either allocated or free, has a size that is a multiple of eight bytes. Thus, only the 29 higher order bits in the header and footer are needed to record
block size, which includes the header and footer. The usage of the remaining 3 lower order bits is as follows:
bit 0 indicates the use of the current block: 1 for allocated, 0 for free.
bit 1 indicates the use of the previous adjacent block: 1 for allocated, 0 for free.
bit 2 is unused and is always set to be 0.
Given the contents of the heap shown on the left, show the new contents of the heap (in the right table) after a call to free(0x400b000) is executed. Your answers
should be given as hex values. Note that the address grows from bottom up. Assume that the allocator uses immediate coalescing, that is, adjacent free blocks are merged
immediately each time a block is freed. If the content of a word did not change, you may leave it blank.
Address
Address
0x400b028
0x00000022
0x400b028
0x400b024
0x400b611c
0x400b024
0x400b020
0x400b512c
0x400b020
0x400b01c
0x00000d12
0x400b01c
0x400b018
0x00000014
0x400b018
0x400b014
0x400b511c
0x400b014
0x400b010
0x400b601c
0x400b010
0x400b00c
0x00000022
0x400b00c
0x400b008
0x00000013
0x400b008
0x400b004
0x400b601c
0x400b004
0x400b000
0x400b511c
0x400b000
0x400affc
0x00000013
0x400affc
0x400aff8
0x0000001B
0x400aff8
0x400aff4
0x400b601c
0x400aff4
0x400aff0
0x00000014
0x400aff0
0x400afec
0x0a0bc11d
0x400afec
0x400afe8
0x400b511d
0x400afe8
0x400afe4
0x0000001B
0x400afe4
Page 5
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 201
Midterm Exam A
/*
foo1 .c
*/
int main()
{
int x = 5;
return 0;
}
/*
bar1 .c
*/
int main()
{
int x = 15;
return 0;
}
Will the linker succeed or generate an error? If there is an error, explain which of the three rules above are violated.
/*
foo2 .c
*/
void f();
int x = 15;
int main()
{
f();
printf("x = %d",x);
return 0;
}
/*
bar2 .c
*/
int x;
void f()
{
x = 10;
}
Will the linker succeed or generate an error? If there is an error, explain which of the three rules above are violated. If there is no error, show the output generated
by this program.
Page 6
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 201
Midterm Exam A
int fun_1()
{
int B[N][N];
int i, j;
for(j = 0; j < N; j++)
for(i = 0; i < N; i++)
B[i][j] = 2*(B[i][j] + 2);
}
B.
typedef struct {
float exam1;
float exam2;
} grade;
grade compute_average( grade * g, int n) {
float exam1_average = 0;
float exam2_average = 0;
int i;
for(i = 0; i < n; i++)
exam1_average += g[i].exam1;
for(i = 0; i < n; i++)
exam2_average += g[i].exam2;
grade average = { exam1_average/n, exam2_average/n };
return average;
}
Page 7
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 201
Midterm Exam A
A. Using the template below (allowing a maximum of 64 bytes), indicate the allocation of data for a Node struct. Mark off and label the areas for each individual
element (there are 6 of them). Cross hatch the parts that are allocated, but not used (to satisfy alignment).
Assume the Linux alignment rules that were discussed in class (and the textbook).
0
1
2
3
4
5
6
7
8
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|
|
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|
|
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
B. For each of the C functions below, complete the blanks in its assembly code.
movslq
movq
%edi, %rdi
(%rsi,%rdi,8), %rax
movl
ret
___________________(%rax), %eax
movslq
movq
%edi, %rdi
(%rsi,%rdi,8), %rax
movq
___________________(%rax), %rax
movq
ret
___________________(%rax), %rax
Page 8
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 201
Midterm Exam A
In the following tables, all numbers are given in hexadecimal. The contents of the cache are as follows:
2-way Set Associative Cache
Index
Tag
Valid
Byte 0
Byte 1
Byte 2
Byte 3
Tag
Valid
Byte 0
Byte 1
Byte 2
Byte 3
09
86
45
60
30
3F
10
00
99
04
03
48
4F
E0
23
38
00
BC
0B
37
EB
2F
81
06
3D
94
FD
09
0B
8F
E2
05
BD
9B
F7
32
12
08
7B
AD
C7
06
78
07
C5
05
71
0B
DE
18
4B
6E
40
67
C2
3B
B0
39
D3
F7
91
A0
B7
26
2D
F0
0C
71
40
10
46
B1
0A
32
0F
DE
12
C0
88
37
A. The box below shows the format of a physical address. Indicate (by labeling the diagram) the fields that would be used to determine the following:
BO the block offset within the cache line
SI the set index
T the cache tag
12
11
10
B. For the given memory address, indicate the cache entry accessed and the cache byte value returned in hex. Indicate whether a cache miss occurs.
Memory address: 0E34
(a) Memory address format (one bit per box)
12
11
10
Parameter
Value
Byte offset
0x
Cache Index
0x
Cache Tag
0x
0x
Page 9
Joanna Klukowska
joannakl@cs.nyu.edu
CSCI-UA 201
Midterm Exam A
C. When a program with the following main function is executed, it ofter produces no output. Explain why this may happen and how to fix it. Describe the output for
your modified program.
void main() {
pthread_t th[3];
int i;
for (i = 0; i < 3; i++) {
pthread_create(&th[i], NULL, print_number, &i);
}
exit(0);
}
Page 10