Unformatted text preview:

Wawrzynek, WeaverFall 2021CS 61CMidtermPrint your name:(first) (last)Print your student ID:Read the following honor code and sign your name.I understand that I may not collaborate with anyone else on this exam, or cheat in any way. I am awareof the Berkeley Campus Code of Student Conduct and acknowledge that academic misconduct will bereported to the Center for Student Conduct and may further result in, at minimum, negative points onthe exam and a corresponding notch on Nick’s Stanley Fubar demolition tool.Sign your name:You have 110 minutes. There are 6 questions of varying credit (100 points total).For questions with circular bubbles, you may select only one choice.Unselected option (completely unfilled)Only one selected option (completely filled)For questions with square checkboxes, you may select one or more choices.You can selectmultiple squares (completely filled).Anything you write that you cross out will not be graded.If an answer requires hex input, make sure you only use capitalized letters! For example,0xDEADBEEFinstead of0xdeadbeef. Please include hex (0x) or binary (0b) prefixes in your answers. For all other bases,do not add the suffix or prefixes.Page 1 of 13This page is intentionally left blank.Midterm Page 2 of 13 CS 61C – Fall 2021Q1 Potpourri (10 points)Q1.1 (1.25 points) True or False: The compiler resolves define statements.True FalseQ1.2(1.25 points) True or False: The assembler is the step with the highest computational complexityamong CALL.True FalseQ1.3 (1.25 points) True or False: The assembler produces an executable.True FalseQ1.4(1.25 points) True or False: In the loader, the program is placed in memory in preparation of runningthe code.True FalseQ1.5 (1.25 points) Convert 0xDA71 to a 16-bit binary value, including the prefix.Q1.6(1.25 points) Convert0x85to decimal, assuming the data was stored as an unsigned one-byteinteger.Q1.7(1.25 points) Convert0x85to decimal, assuming the data was stored as a 2’s complement one-byteinteger.Q1.8(1.25 points) Convert0x85to decimal, assuming the data was stored as a sign-magnitude one-byteinteger.Midterm Page 3 of 13 CS 61C – Fall 2021Q2 Now, Where Did I Put Those Strings? (10 points)Consider the following code:char *foo() {char *str1 = "Hello World";char str2[] = "Hello World";char *str3 = malloc(sizeof(char) * X);strcpy(str3, "Hello World");// INSERT CODE FROM PARTS 5-7}Thechar *strcpy(char *dest, char *src)copies the string pointed to bysrc, including theterminating null byte ('\0'), to the buffer pointed to bydest. The strings may not overlap, and thedestination string dest must be large enough to receive the copy.Q2.1 (1 point) Where is *str1 located in memory?code static heap stackQ2.2 (1 point) Where is *str2 located in memory?code static heap stackQ2.3 (1 point) Where is *str3 located in memory?code static heap stackQ2.4 (1 point) What is the minimum value of X needed for the code to have well-defined behavior?Midterm Page 4 of 13 CS 61C – Fall 2021The code from the previous page has been copied here:char *foo() {char *str1 = "Hello World";char str2[] = "Hello World";char *str3 = malloc(sizeof(char) * X);strcpy(str3, "Hello World");// INSERT CODE FROM PARTS 5-7}Which of the following lines can be inserted into the function at the given line, with well-definedbehavior? Select all that apply.Q2.5 (1 point) Returning the string.return str1;return str2;return str3;None of the aboveQ2.6 (1 point) Modifying the string.str1[0] = 'J';str2[0] = 'J';str3[0] = 'J';None of the aboveQ2.7 (1 point) Freeing the string.free(str1);free(str2);free(str3);None of the aboveQ2.8 (1 point) Printing the string.printf("%s\n", str1);printf("%s\n", str2);printf("%s\n", str3);None of the aboveQ2.9 (2 points)If this code was run on a little-endian system, what would((uint32_t*) str1)[2]evaluate to? Express your answer in hexadecimal, with the necessary prefix. Note thatuint32_trefers to an unsigned 32-bit integer.Midterm Page 5 of 13 CS 61C – Fall 2021Q3 I C a Scheme (20 points)Consider the following C code:union ExtraStuff {char a[5];uint16_t b;int c;double d;};typedef struct ConsCell {void *car;void *cdr;union ExtraStuff extra;} cons;Consider the following function: cons *map(cons *c, (void *)(*f)(void *));map takes a pointer to a cons struct c and a function pointer f.If the cons struct pointer is NULL, map returns NULL. Otherwise, it does the following:1. Allocate a new cons struct. ret is a pointer to this new struct.2. Set the contents of the extra union in ret to be all zeros.3. Set the car field in ret to the result of calling f on the car pointer in c.4. Set cdr field in ret to the result of calling map recursively on the cdr pointer in c.Q3.1 (18 points)Complete the following code by filling in the blanks. This code should compile withouterrors or warnings. Each blank is worth 2 points.cons *map(cons *c, (void *) (*f) (void *)) {cons *ret;if ( ) return ;ret = malloc( );extra = 0;car = ;cdr = ;return ret;}Q3.2 (2 points) On a 32-bit architecture, what is sizeof(cons)?Midterm Page 6 of 13 CS 61C – Fall 2021Q4 To Float or Not to Float (20 points)Consider a floating point system that has 16 bits with 7 bits of exponent and an exponent bias of -63,which otherwise follows all conventions of IEEE-754 floating point numbers (including denorms, NaNs,etc.). In this question, we will compare this system to an unsigned 16-bit integer system.Q4.1 (4 points) What is the value of floating point number 0xC2C0 in decimal?Q4.2 (1 point)Which representation has more representable numbers? Count +0, -0,+∞, and−∞as4 different representable numbers.The floating point numberThe unsigned 16-bit integerBoth systems can represent the same number of valuesQ4.3 (3 points)How many more numbers can be represented? Write 0 if both systems can representthe same number of values.Q4.4 (4 points)Out of all numbers representable by this floating point system, what is the largestnumber that can also be represented as an unsigned 16-bit integer?Q4.5 (4 points)What is the smallest positive number representable by this floating point system thatisn’t representable by the unsigned 16-bit integer?Q4.6 (4 points)What is the smallest positive number representable by the unsigned 16-bit integer thatisn’t representable by this floating point system?Midterm Page 7 of 13 CS 61C – Fall 2021Q5 A RISC-y Program (20 points)In addition to storing theraregister and thesregisters, the stack can also store local variables. Youhave access to the following


View Full Document
Download Final
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Final and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Final 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?