



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
The university of california, berkeley - college of engineering, department of electrical engineering and computer science - cs61c midterm exam from fall 2004. The exam covers topics such as c programming, circular lists, mips assembly language, memory management, and numerical representation.
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!




All the work is my own. I had no prior knowledge of the exam contents nor will I share the contents with others in CS61C who have not taken it yet. (please sign)
x This booklet contains 6 numbered pages including the cover page. Put all answers on this pages, don’t hand in any stray pieces of paper.
x Please turn off all pagers, cell phones & beepers. Remove all hats & headphones. Place your backpacks, laptops and jackets at the front. Sit in every other seat. Nothing may be placed in the “no fly zone” spare seat/desk between students.
x Question 0 (1 point) involves filling in the front of this page and putting your name & login on every front sheet of paper.
x You have 180 minutes to complete this exam. The times listed by each problem will allow you to finish with 60 (!) minutes left to check your answers. The exam is closed book, no computers, PDAs or calculators. You may use one page (US Letter, front and back) of notes.
x There may be partial credit for incomplete answers; write as much of the solution as you can. We will deduct points if your solution is far more complicated than necessary. When we provide a blank, please fit your answer within the space provided. You have 3 hours…relax.
Problem 0 1 2 3 4 5 6 7 8 Total
Minutes 30 5 5 10 20 10 20 20 120
Points 1 22 6 3 4 16 5 10 8 75
Score
We’re writing a circular linked list to keep numbers. The idea is very similar to a single-linked list, but the last element points to the first. Our circular linked list is made up of elements of type pair (a data type from CS61A and project 1). Assume when the list is empty we initialize the global variable head to NULL. Here’s an example on the left, with the pair definition on the right:
The pair structure is defined as follows:
struct pair { int car; // “number” struct pair *cdr; // next “pair” } *head;
In the figure above, we can see 4 elements linked. When we insert an element, it goes after the first element. E.g., if we represent the distinct elements list in the example above before insertion as {1 2 3 4}, then after a call to insert(5) it would be {1 5 2 3 4}.
a) Help us to write the insert function by adding only 3 statements.
void insert(int d) { /* create the new node */
tmp->car = d;
/* Insert the new node in the right place / if ( head == NULL ) { / The struct was empty… link the itself and we’re done / tmp->cdr = tmp; head = tmp; } else { / There were already elements in the linked list. Link the new node after the first element. */
b) Instead we’d like you to link it after the “second” element. (If we only have 1 element, do the same as before. Can it be done by only modifying the 2nd^ and 3rd^ statements? If so, do it below. If not, explain why. c) /* Link the new node after the “second” element */
Or
head 1 2 3 4
Fill in the following C function that creates and returns the pointer diagram below. You can use only one variable in your code, p. Don’t use more than 5 statements, including the two we show below. You may
modify line 1 (between int and p), but you’re not allowed to touch line 5. Please specify the function’s return type.
Structure returned by malloc_masters()
int** malloc_masters () {
int ** p; /* 1
st (^) statement */
return p;
}
/* 5th^ statement */
In at most one sentence each, describe 2 advantages and 1 disadvantage of dynamically-linked (vs statically-linked) programs.
Advantage #1:
Advantage #2:
Disadvantage:
Question 4: Raw Bits (4 points, 10 min.)
0x
Interpret the four characters word on the right as… an instruction
P
a) Translate the following MIPS function into the C in the boxes on the right. Fill in the arguments (& their data types) and return types for foo & bar.
Main: …
jal foo …
foo: li $a1, 0 bar: addi $sp, $sp, - sw $ra, 0($sp) bnez $a0, else mv $v0, $a j end else: srl $a0, $a0, 1 addi $a1, $a1, 1 jal bar end: lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra If you can tighten the body of bar to be just “return ___;” (it’s possible) you’ll receive full credit. Otherwise you’ll lose one point.
b) What math function does foo compute?
c) What’s the biggest number that foo will ever return?
d) What does foo((unsigned int) –x) return if x is a single-digit integer [1,9]?
a) How many different instructions can we specify in MIPS given our standard 32-bit encoding? Assume we only have R-, I- & J-format instructions.
b) (This question has nothing to do with MIPS) Assume we have enough bits to byte-address 16 10 exbibytes. We want to define some number of the most- significant bits to encode 12 10 x 2^10 things, and some number of the least- significant bits to encode 200,000 10 things. How many things can we encode with the remaining bits? Use IEC language, like “16 kibithings,” or “ mebithings.” Show your work below.
Bush and Kerry are debating about which is better for representing integers with 32 bits, a float or an int; you’re going to provide them with data to support their argument. We define two acronyms here: NICTO = “Negative Integer Closest To 0” and PICTO = “Positive Integer Closest To 0.”
a) What are the NICTO and PICTO that float can represent but int cannot. Show all work and the 32-bit hex number that corresponds to both.
Show all of your work (^) NICTO PICTO
Decimal Value (you may leave as an expression)
32-bit Hexadecimal Value
b) What are the NICTO and PICTO that int can represent but float cannot. Show all work.
Show all of your work (^) NICTO PICTO
Decimal Value (you may leave as an expression)