Unformatted text preview:

Wawrzynek, WeaverFall 2021CS 61CMidtermSolutions last updated: Thursday, February 24, 2022Print 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 35This page is intentionally left blank.Midterm Page 2 of 35 CS 61C – Fall 2021Q1 Potpourri (10 points)Q1.1 (1.25 points) True or False: The compiler resolves define statements.True FalseSolution: True. In particular, the pre-processor, which is part of the compiler, examines the Ccode and replaces all instances of the defined variable with its value. This must be done on Ccode, so it happens in the compilation stage (later stages no longer use C code).Grading: 1.25 points for True.Q1.2(1.25 points) True or False: The assembler is the step with the highest computational complexityamong CALL.True FalseSolution: False. The compiler is more complex than the assembler.Grading: 1.25 points for False.Q1.3 (1.25 points) True or False: The assembler produces an executable.True FalseSolution: False. The assembler creates an object file. The linker creates an executable.Grading: 1.25 points for False.Q1.4(1.25 points) True or False: In the loader, the program is placed in memory in preparation of runningthe code.True FalseSolution: True. Executable files (the program instructions after passing through the compiler,assembler, and linker) are stored on the disk, and the loader will place the executable in memorybefore running it.Grading: 1.25 points for True.Midterm Page 3 of 35 CS 61C – Fall 2021Q1.5 (1.25 points) Convert 0xDA71 to a 16-bit binary value, including the prefix.Solution: 0b1101 1010 0111 0001Remember that one hexadecimal digit (16 possible values) can be represented by four binarydigits (4 digits, 2 possible values each,24= 16). Converting each hexadecimal digit to its binaryrepresentation, we get0xD = 0b1101,0xA = 0b1010,0xA = 0b0111, and0x1 = 0b0001.Note that the prefix is 0b to denote binary values.Other versions of this exam gave different hexadecimal values, but the conversion process isthe same:• 0x326A = 0b0011 0010 0110 1010• 0xB13C = 0b1011 0001 0011 1100• 0x126D = 0b0001 0010 0110 1101Grading: 1.25 points for the correct binary string. No partial credit, sorry.Q1.6(1.25 points) Convert0x85to decimal, assuming the data was stored as an unsigned one-byteinteger.Solution: 133This question asks you to convert the provided one-byte (8 bits = 4 nibbles = 2 hexadecimaldigits) hexadecimal number to a decimal number.Hexadecimal is base-16, so this hexadecimal number has a 5 in the ones place and an 8 in the16s place. In decimal, this is (8 × 16) + (1 × 5) = 133.Other versions of this exam gave different hexadecimal values, but the conversion process isthe same:• 0x86 = 134• 0x87 = 135• 0x88 = 136Grading: 1.25 points for the correct decimal number. No partial credit, sorry.Midterm Page 4 of 35 CS 61C – Fall 2021Q1.7(1.25 points) Convert0x85to decimal, assuming the data was stored as a 2’s complement one-byteinteger.Solution: -123This question asks you to convert a hexadecimal number to a two’s complement integer. Todo this, we start by writing out the hexadecimal number in binary (using the same process asQ1.5): 0x85 = 0b1000 0101.Recall that in 2’s complement, the most-significant bit tells us whether the number is positiveor negative. Here, the most-significant bit is 1, so the number must be negative.If the number is negative, we need to invert all the bits and add 1 to determine the value of thenumber. Inverting all the bits gives us 0b0111 1010, and adding 1 gives us 0b0111 1011.Now we can convert this binary number into a decimal number:20+21+23+24+25+26= 123.Remember that we determined the number is negative, so our final answer is -123.Other versions of this exam gave different hexadecimal values, but the conversion process isthe same:• 0x86 = -122• 0x87 = -121• 0x88 = -120Grading: 1.25 points for the correct decimal number. Half credit (0.675 points) only for thecorrect decimal number but the opposite sign (e.g. 123 instead of -123).Midterm Page 5 of 35 CS 61C – Fall 2021Q1.8(1.25 points) Convert0x85to decimal, assuming the data was stored as a sign-magnitude one-byteinteger.Solution: -5This question asks you to convert a hexadecimal number to a sign-magnitude integer. To dothis, we start with the binary version of the number (which we wrote out in the previous part):0x85 = 0b1000 0101.Recall that in sign-magnitude, the most-significant bit tells us whether the number is positiveor negative. Here, the most-significant bit is 1, so the number must be negative.In sign-magnitude, all the other bits of the number (except the most-significant bit) tell us thevalue of the number. Here, those bits are 0b000 0101.Now we can convert this binary number into a decimal number:20+ 22= 5. Remember thatwe determined the number is negative, so our final answer is -5.Other versions of this exam gave different hexadecimal values, but the conversion process isthe same:• 0x86 = -6• 0x87 = -7• 0x88 = -8Grading: 1.25 points for the correct decimal number. No partial credit, sorry.Midterm Page 6 of 35 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,


View Full Document
Download Midterm
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 Midterm 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 Midterm 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?