









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Practice problems for the CS107 midterm exam. The problems cover topics such as integer representation, memory diagrams, strings, and pointers. instructions for each problem and space for students to write their answers. The problems are designed to help students prepare for the exam and test their understanding of the course material.
Typology: Exams
1 / 15
This page cannot be seen from the preview
Don't miss anything!










CS107 Cynthia Lee Autumn 2018
Problem 1: Integer Representation There is a small amount of scratch space between problems for you to write your work, but it is not necessary to show your work, nor will it be evaluated. (You may also use the blank back sides of each exam page as scratch space.) Please write your answer on the provided lines. a) Write the signed (two’s complement) binary number 11101100 in decimal:
b) Write the unsigned binary number 1100011011 in hexadecimal:
c) Write the hexadecimal number 0x1DEAD as an unsigned binary number:
d) Write the decimal number 21 as an 8-bit binary number:
e) Write the decimal number - 15 as an 8-bit signed (two’s complement) binary number:
Problem 2: Memory Diagram For this problem, you will draw a memory diagram of the state of memory (like those shown in lecture) as it would exist at the end of the execution of this code: int eleven = 11; char *stranger = "things"; int upside = malloc(3 * sizeof(int)); upside[0] = malloc(4); upside[0] = 2; upside[1] = &eleven; upside[2] = (int) ((char)stranger + 1); Instructions:
return_array[1] = strdup(three_strings[2]); return return_array; } Problem 4: Bitwise ops (6pts) As you know, an unsigned int on our system is 32 bits or 4 bytes. We can imagine separating the number into bytes and stacking the bytes vertically, like this (for 0xFEFAFE05): 11111110 11111010 11111110 00000101 We say a “column” in this view is “odd” if it contains an odd number of 1’s. Write a function odd_cols that takes an unsigned int and returns true if every column of the input number is odd, otherwise false (i.e., returns false if any column with zero, two, or four 1’s in it).
bool odd_cols(unsigned int n) { unsigned char ptr = (unsigned char)&n; unsigned char row0, row1, row2, row3; row0 = ___________________________________________________; row1 = ___________________________________________________; row2 = ___________________________________________________; row3 = ___________________________________________________; // Write the rest of the function here: }
(c) (3pts) Below is the same buggy code from above. Fix it so that it correctly takes an array of strings (array of char*) and returns the concatenation of them all together in sequence. You may cross out code, add code, or edit code, as needed (just please be as neat and clear as possible, thank you! J). char *multi_concatenate(const char *strs[], size_t num_strs) { size_t len = 1; for (size_t i = 0; i< num_strs; i++) { len += sizeof(strs[i]); } char *result = malloc(len); for (size_t i = 0; i< num_strs; i++) { memcpy(result + sizeof(strs[i]) * i, strs[i], sizeof(strs[i])); } result[sizeof(strs[0]) * num_strs] = '\0'; return result; }
Problem 6: The accumulate generic Consider the following two functions, noticing that they have very similar structure: int int_array_product(const int array[], size_t n) { int result = 1; for (size_t i = 0; i < n; i++) { result = result * array[i]; } return result; } double double_array_sum(const double array[], size_t n) { double result = 0.0; for (size_t i = 0; i < n; i++) { result = result + array[i]; } return result; } You are to implement a generic accumulate function that captures the shared structure of these two funcions. It takes the base address of an array and its effective length, the array element size, the function callback that should be repeatedly applied (above it would be multiply and add, but implemented as 2-argument functions rather than operators directly), the address of the default/starting value (to play the role of 1 and 0.0 in the above code), and the address where the overall result should be placed. The function type of the callback is as follows: typedef void (*BinaryFunc)(void *partial, const void *next, void result); Any function that can interpret the data at the first two addresses, combine them, and place the result at the address identified via the third address falls into the BinaryFunc function class. (The const appears with the second address, because it’s expected that the array elements—the elements that can’t be modified—be passed by address through the next parameter.) a) First, implement the generic accumulate routine. We’ve provided some of the structure that should contribute to your implementation. You should fill in the three arguments needed so that memcpy can set the space addressed by result to be identical to the space addressed by init , and then go on to fill in the body of the for loop. This first part was designed to expose basic memory and pointer errors very early on— e.g. to confirm that weren’t dropping & ’s and __ ’s where they weren’t needed. void accumulate(const void *base, size_t n, size_t elem_size, BinaryFunc fn, const void *init, void *result) {
Problem 7: Integer Representation a) Write the unsigned binary number 101100111010 in hexadecimal: b) Write the signed (two’s complement) binary number 101101 in decimal: c) Write the signed (two’s complement) binary number 001101 in decimal: d) Write the hexadecimal number 0x54BEEF as an unsigned binary number: e) Write the decimal number 28 as an 8-bit binary number: f) Write the decimal number 28 in hexadecimal: g) Write the decimal number - 33 as an 8-bit signed (two’s complement) binary number:
Problem 8: Integer Representation There is a small amount of scratch space between problems for you to write your work, but it is not necessary to show your work, nor will it be evaluated. (You may also use the blank back sides of each exam page as scratch space.) Please write your answer on the provided lines. a) (2pts) Write the unsigned binary number 1100101011111110 in hexadecimal:
b) (2pts) Write the decimal number 55 as an 8 - bit signed (two’s complement) number:
c) (2pts) Write the 8-bit signed (two’s complement) hexadecimal number 0xAA in decimal:
d) (2pts) Write the decimal number - 13 in 8 - bit signed (two’s complement):
A generic CMap type maps string keys to arbitrary values. It works with the following functions: // returns the first key in the map as a char *, // or NULL if there are no keys char *cmap_first(CMap *cmap); // returns a pointer to the value associated with that key, or NULL // if the key does not exist. void *cmap_get(cmap, char *key); // returns the next char * key in the map following this key, // or NULL if there are no more keys. char *cmap_next(cmap, char *key); // returns the number of elements (key/value pairs) in the map. int cmap_count(cmap); (d) (6pts) The code below has three buggy lines of code in it. The buggy parts of the code are noted in bold. The purpose of this function is to take a pointer to a CMap that maps each key (a string, as usual for CMap) to a value of type int. The function is supposed to put the map’s values in an array of integers (order of the values does not matter), and also make a copy of the first key (the key that comes back from cmap_first). The copy of the first key and the array are to be “returned” to the caller by setting two parameters that are pointers passed “by reference.”
void separate_map(CMap *cmap, char **first_key, int **values) { first_key = cmap_first(cmap); ________________________________________________________________; int nelems = cmap_count(cmap); values = malloc(nelems); ________________________________________________________________; int index = 0; for (const char *cur = cmap_first(cmap); cur != NULL; cur = cmap_next(cmap, cur)) { values[index] = cur; _______________________________________________________________; } }