CS360 Midterm Exam. February 20, 2014. James S. Plank

Answers and Grading


Question 1: 15 Points

The easiest thing here is to turn i, j and k into bits, operate on the bits, and then turn the bits back into hex.

Line 1: i is 1101 0000 0000 1111, so (i >> 1) is 0110 1000 0000 0111, which is 0x6807.

Line 2: j is 0111 1000 0000 0001, so (j << 2) is 0001 1110 0000 0000 0100, which is 0x1e004.

Line 3: We are shifting by four bits, which is one hex digit. So (i << 4) is equal to 0xd00f0. When you "or" it with 0x7801, you'll note that we're or-ing non-zero digits with zero digits:

d00f0
07801
-----
d78f1
So the answer is 0xd78f1.

Line 4: Right-shifting k by 12 turns it into 0x7. Therefore, performing the "and" of 0x7 and 0xd00f equals the "and" of 0x7 and 0xf. That equals 0x7.

Line 5: (i XOR i) equals zero, regardless of what i is. Therefore, (i ^ j ^ i) equals j, and you don't have to try to calculate what i is. The answer is 0x7801.

The output:

Line 1: 0x6807
Line 2: 0x1e004
Line 3: 0xd78f1
Line 4: 0x7
Line 5: 0x7801

Grading

Three points per line.


Question 2: 16 Points

You set every 7th character of x to the null character. I'm going to represent that in the string below with an asterisk. I'm also going to color every fifth character red. Those are the beginnings of each string that you print out. I'm also including the final null character at the end of the string:

*123456*890123*567890*234567*901234*6789*

So, the output is: The output:

()
(56)
(0123)
(567890)
(0)
(567)
(01234)
()
()

Grading

Two points per line.


Question 3: 10 Points

Here are the two strings with the asterisks in x, and every tenth character colored red:

012345*789012345678901234567890*23456*89012*
you don't get money woof woof but I do I do*

So, here's the output:

012345
012345678901234567890
01234567890
0
012

Grading

Two points per line.


Question 4: 8 Points

Line 1: a + 21 is a + 16 + 5, which is a + 0x10 + 0x5 = 0xbfffda75. That's pointing to the character 'y'. When we print out the string, we print out all characters from that 'y' until the null character: "yo' trick bag.".

Line 2: Since s2 points to (char *)'s, and pointers are four bytes, the statement "s2 += 4" actually adds 16 to a. Therefore, s2 = 0xbfffda70. That means *s2 is 0xbfffda84, and when we print it as a string, it prints all characters from that 'e' to the null character: "ease on."

Line 3: Setting s to a + 8 sets its value to 0xbfffda68. This is also the value of s2, so s2 is equal to the contents of 0xbfffda68: 0x20616e6e.

Line 4: Incrementing a[0] sets the 's' to a 't'. If you look at the contents of 0xbfffda60 in hex, it will be 0x79206f74, because you just incremented the least siginificant bit of the number. That is the value of i.

The output:

1: yo' trick bag.
2: ease on
3: 0x20616e6e
4: 0x79206f74

Grading

Two points per line.


Question 5: 16 Points

Fortunately, in the first four lines, you can use the answer of the previous line in hex to find where the next pointer goes: You do the same thing for the next four lines, however you have to look four bytes past each pointer: The output:

1: 1048932
2: 1048936
3: 1048892
4: 1048904
5: 1048896
6: 1048908
7: 1048888
8: 1048904

Grading

Two points per line.


Question 6: 14 Points

Line 1: p+3 equals 0x572b2c, so p[3] equals 0x00572b38. When you print that as a string, prints the characters starting at 0x00572b38 to the null character: "@+W".

Line 2: Incrementing s[0] turns the 'T' into a 'U'. Next, we replace the null character with 'T'. When we print s as a string, it prints "U+WT0+W".

Line 3: In hex, those four bytes were 0x00572b54. Incrementing the 'T' turns the 0x54 into 0x55, and setting the null character to 'T' turns the 0x00 into 0x54. So the answer is 0x54572b55.

Line 4: we are printing (0x00572b30 | 0x00572b4c). Since the first six digits of these two numbers are identical, that is the result of the OR. The last two digits are (0x30 | 0x4c), which equals 0x7c. So the answer is 0x00572b7c.

Line 5: we are printing (0x00572b38 ^ 0x00572b44). Once again, the first six digits of these numbers are identical, so their XOR is zero. The last two digits are (0x38 ^ 0x44), which is 0x7c. So the answer is 0x0000007c.

Line 6: &(p[5]) is equal to p+5, which is 0x572b34. Its contents are 0x00572b3c, so that is printed in Line 6.

Line 7: Printing 0x00572b3c as a string gives us "$+W".

The output is:

1. @+W
2. U+WT0+W
3. 0x54572b55
4. 0x00572b7c
5. 0x0000007c
6. 0x00572b3c
7. $+W

Grading

Two points per line.


Question 7: 16 Points

When we set z to p, we get the following:

Line 1: Because &(z->x) equals 0x552c2c, z->x equals the contents of the memory there, printed in decimal: 5581892

Line 2: Because &(z->jp) equals 0x552c24, z->jp equals 0x00552c3c. When we print it as a string, we print all the characters from 0x00552c3c to the null character: "0,U".

Line 3: Because &(z->ip) equals 0x552c20, z->ip equals 0x00552c4c. Therefore *(z->ip) prints the four bytes starting at 0x00552c4c in decimal: 5581868

Line 4: z->ip equals 0x00552c4c, so z->ip+1 equals 0x00552c50. Therefore z->ip[1] is the four bytes starting at 0x00552c50, printed in decimal: 5581856

Now, we set z to z->kp, which is 0x00552c50. Therefore:

Line 5: From above, &(z->x) equals 0x00552c5c. We don't need to know its contents to print its value.

Line 6: z->jp equals 0x00552c38, so we print characters starting with the '$': "$,U".

Line 7: z->ip equals 0x00552c20, so we print the bytes starting at 0x00552c20 in decimal: 5581900.

Line 7: z->kp equals itself: 0x00552c58. So &(z->kp->ip) equals 0x00552c58, which means that (z->kp->ip) also equals 0x00552c58. Finally *(z->kp->ip) also equals 0x00552c58 = 5581912. We've chased some pointers here, but since the contents of memory equal the address, each pointer chase goes to the same place.

The output:

1. 5581892
2. 0,U
3. 5581868
4. 5581856
5. 0x00552c5c
6. $,U
7. 5581900
8. 5581912

Grading

Two points per line.