Unformatted text preview:

CS 61C Great Ideas in Computer ArchitectureSummer 2020 Midterm 1INSTRUCTIONSThis is your exam. Complete it either at exam.cs61a.org or, if that doesn’t work, by emailing course staff with yoursolutions before the exam deadline.This exam is intended for the student with email [email protected]. If this is not your email address,notify course staff immediately, as each exam is different. Do not distribute this exam PDF even after the exam ends,as some students may be taking the exam in a different time zone.For questions with circular bubbles, you should select exactly one choice.# You must choose either this option# Or this one, but not both!For questions with square checkboxes, you may select multiple choices.2 You could select this choice.2 You could select this one too!You may start your exam now. Your exam is due at <DEADLINE> Pacific Time.Go to the next pageto begin.Exam generated for [email protected] 2PreliminariesPlease complete and submit these questions before the exam starts.(a) What is your full name?Solutions(b) What is your student ID number?dQw4w9WgXcQ (This is a YouTube video)(c)If an answer requires hex input, make sure you only use capitalized letters! For example, 0xDEADBEEFinstead of 0xdeadbeef. You will be graded incorrectly otherwise! Please always add the hex (0x) andbinary (0b) prefix to your answers or you will receive 0 points. For all other bases, do not add the suffix orprefixes.Some of the questions may use images to describe a problem. If the image is too small, you can click anddrag the image to a new tab to see the full image. You can also right click the image and download it orcopy its address to view it better. You can use the image below to try this. You can also click the star bythe question if you would like to go back to it (it will show up on the side bar). In addition, you are ablesee a check mark for questions you have fully entered in the sidebar. Questions will auto submit about 5seconds after you click off of them, though we still recommend you click the save button.Good luck!Exam generated for [email protected] 31. A Generic C QuestionIn object-oriented programming languages such as Java, the concept of a Generic data type exists. This meansthat, in a class definition of an object, we can leave the data types of chosen variables as an “unknown” typethat is instead expected to be provided during instantiation of the object. In this problem, we will implementgenerics in C for a LinkedList. Remember, though, that we do not have objects to instantiate in C, so insteadour GenericLinkedList should simply be versatile enough to accept any given data type without error or compilerwarnings.A user should not need to do any form of explicit or implicit casting when workingwith this new data type, except for when dealing with the void* pointer returned by the allocfunctions. For the following, assume we have included the correct includes.(a) (3.0 pt) You may assume that our GenericLinkedList only has to account for 3 data type choices: char,uint16_t,uint32_t, where the # inuint#_trepresents the number of bits the data type contains. Italso supports structs and unions. In addition, we are working on a 32-bit addressable memory space,structs are word-aligned and padded appropriately, and all calls tomalloc(),calloc(), andrealloc()succeed. Fill in the skeleton for aGenericLink.Your solution must use the minimum amount ofspace possible. A sub-optimal solution may not receive credit. You may not use void* inyour approach.typedef struct {<YOUR CODE HERE>} GenericLink;(b) (1.0 pt) What does sizeof(GenericLink) evaluate to?(c)I now want to store a String as a GenericLinkedList, i.e. each link should hold one char of the string, withthe links ordered the same way as the chars in the string. You may assume that the length of the stringis > 1. You do not need to worry about storing the null terminator. Please fill in the following functionimplementations:Exam generated for [email protected] 4i. (2.0 pt)GenericLink* store_char(char c) {/* store_char takes in a char, and returns apointer to a link containing this char */<YOUR CODE HERE>}ii. (6.0 pt)GenericLink* store_string(char* str) {/* store_string takes in a string, and returns apointer to the “head” of the GenericLinkedListholding the string, i.e. the link containing the first char*/<YOUR CODE HERE>}Exam generated for [email protected] 52. Doubly Linked Trouble!For this problem, assume all pointers and integers arefour bytesand all characters areone byte. Considerthe following C code (all the necessary #include directives are omitted). C structs are properly aligned inmemory and all calls to malloc succeed.For all of these questions, assume we are analyzing themright before main returns.typedef struct node {void *data;struct node *nxt;struct node *prv;} node;void push_back(node *list, void *data) {node *n = (node *) malloc(sizeof(node));n->data = data; n->nxt = list; n->prv = list->prv;list->prv->nxt = n; list->prv = n;}int main() {char *r = "CS 61C Rocks!";char s[] = "CS 61C Sucks!";node sentinel; sentinel.nxt = &sentinel; sentinel.prv = &sentinel;push_back(&sentinel, r);push_back(&sentinel, s);push_back(&sentinel, &sentinel);push_back(&sentinel, calloc(sizeof(s) + 1, sizeof(char)));}(a)Each of the following evaluate to an address in memory. In other words, they “point” somewhere. Wherein memory do they point?i. (0.75 pt) &sentinel# Heap# Stack# Static# Codeii. (0.75 pt) sentinel.nxt->nxt->data# Heap# Static# Stack# Codeiii. (0.75 pt) &push_back# Stack# Code# Heap# StaticExam generated for [email protected] 6iv. (0.75 pt) sentinel.nxt->data# Stack# Heap# Static# Codev. (0.75 pt) sentinel.prv->prv->data# Static# Heap# Stack# Codevi. (0.75 pt) sentinel.prv->data# Static# Heap# Stack# Codevii. (0.75 pt) sentinel.prv->prv# Code# Heap# Stack# StaticExam generated for [email protected] 7(b) (3.0 pt)How many bytes of memory are allocated but notfree()d by this program, if any? (assumingwe have not calledfree_list) (Leave your answers as an integer. Do not include the units, we are tellingyou it’s bytes after all!)(c) (1.75 pt) Say we had this free function:void free_list(node *n) {if (n == NULL) return;node *c = n->nxt;for (; c != n;){node *tmp = c; c = c->nxt;free(tmp);}}Given this free function, if we calledfree_list(&sentinel) after allthe code inmainis executed, thisprogram would have well defined behavior.# False# TrueExam generated for [email protected] 83. RISC-V!For each


View Full Document

Berkeley CS 61A - Great Ideas in Computer Architecture

Download Great Ideas in Computer Architecture
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 Great Ideas in Computer Architecture 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 Great Ideas in Computer Architecture 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?