sample-final

pdf

School

University of Waterloo *

*We aren’t endorsed by this school

Course

136

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

18

Report

Uploaded by BaronMagpieMaster993

Final Practice Set CS 136 Spring 2016 PREPARED BY CS 136 ISAS Disch tie glly! It is NOT meant to be a representation of the agtual 1 ) University of Waterloo This document is for the exclusive use of y87feng.
Instructions: The instructions and the cover for the final exam will be posted closer to the examination date CS 136 Final Practice Set . Spring 2016 | Page 2 of 18 This document is for the exclusive use of y87feng.
1. (8 points) Multiple Choice: circle the most correct answer. (a) (2 points) What strategy can we use to improve the average run-time when dynamically allocating an array of unknown length? a. Wrapper Strategy b. Functional Strategy c. Augmentation Strategy d. Doubling Strategy (b) (2 points) What is the difference between a C string and an array of chars in C? a. Cstrings are always stored in read-only memory, whereas an array of chars do not have this restriction. - b. An array of chars cannot be initialized using the double quot@max ie. char a[] = "Hello, World"). c. Anarray of chars stores its length. N Q s d. Cstrings are null terminated. - e. There is no difference. < ( 2) (¢) (2 points) Which approach pefm ith;gto write an O(1) add_to_back function for a singly linked list? a. Use the augmentation s‘ratgg{ b. Use recursion in our ad§_to\back function. c. Use iteration in ouba% o_back function. d. Use the wrapper strategy. CS 136 Final Practice Set . Spring 2016 | Page 3 of 18 This document is for the exclusive use of y87feng.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
(d) (2 points) What approach would be able to determine the height of the BST in O(1) time? a. Augment the BST node to store the height of the subtree and increase it during each insertion. b. Recurse through the longest subtree. ¢. Count the number of items stored in the tree, returning log,(n) + 1 where n is the number of items stored. d. Itis not possible to implement an O(1) height function for a BST. o~ CS 136 Final Practice Set ring 2016 Page 4 of 18 This document is for tfie exclusive use of y87feng.
2. (6 points) Short Answer: Provide a scenario for the following ADTs. For each question below, provide a scenario where the specified ADT may be utilized. Briefly justify your answer. (a) (2 points) Dictionary (b) (2 points) Stack & N Q - (¢) (2 points) Queue \ CS 136 Final Practice Set . Spring 2016 | Page 5 of 18 This document is for the exclusive use of y87feng.
3. (6 points) Multiple Choice: circle the answer with the lowest correct upperbound. Consider the runtime in the worst case. (a) (2 points) What is the runtime of the following function in terms of n, where 7 is the length of Lon? (define (list-max lon) (cond [ (empty? lon) -inf.0] [(> (first lon) (list-max (rest lon))) (first lon)] [else (list-max (rest lon))])) ae o p [SE<BeRs] (b) (2 points) What is the runtime of the following function in erims\c;%, wiée is the string length of str? char lastletter(char *str) { char retval = '\0'; for (int i = 0; 1 < strlengs ) if (((str[i] >= 'a') §& Astrli} z')) |l ((str[i] >= 'A') &l (stn[d z'))) | retval = str[i } . } ( \ . return retval; ) } ( a 0(1) : b. O(logn) \ c. O(n) d. O(n?) CS 136 Final Practice Set ring 2016 Page 6 of 18 This document is for tfie exclusive use of y87feng.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
(¢) (2 points) What is the runtime of the following function in terms of n, where n is the length of 1on? (define (part-sum lon) (cond [ (empty? lon) 0] [ (empty? (rest lon)) (first lon)] [ (empty? (rest (rest lon))) (+ (first lon) (second lon))] [else (+ (first lon) (second lon) (third lon))]) a. O(1) b. O(logn) c. O(n) d. O(n?) L () \ - CS 136 Final Practice Set ring 2016 Page 7 of 18 This document is for tfie exclusive use of y87feng.
4. (35 points) Answer the short questions below in one or two sentences. (a) (5 points) Recall that the five regions in memory are: code, read-only, global data, stack, and heap. Read the following code: #include <string.h> char *myvar; int main(void) { char *str = "CS 136"; char xxc = &str; myvar = str; char *newstr = strdup(str); } At the end of this program, which region of memory is each of the pointer referring to? o str: < (2}\5 s i (b) (3 points) Racket has functions that are equivalent to free and malloc functions found in C. However, they are rarely used in Racket. Why is the use of free and malloc less prevalent in Racket code? CS 136 Final Practice Set ring 2016 Page 8 of 18 This document is for tfie exclusive use of y87feng.
(¢) (3 points) Why is it invalid for a function to return a pointer to a variable in its own stack frame? (d) (3 points) We saw in lecture how we could use the wrapper strategy to obtain an O(1) length function for linked lists. Starting with the linked list structure below, how could you write an O(1) length function using the augmentation strategy (where each node is augmented)? Note; You do not need to write any code, just explain how you could do this. ( struct llnode { int item; ~ _ struct llnode x*next; N Vi (e) (3 points) Why should we use alwyays give malloc a sizeof (type) argument, rather than an integer? Example: malloc (sizeof (int)) as opposed tomalloc (4) CS 136 Final Practice Set ring 2016 Page 9 of 18 This document is for tfie exclusive use of y87feng.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
(f) (3 points) Sort the following list of orders in Big O notation, from smallest to largest: 0(2"),0(n), Olog(n)), O(n*), O(1), O(n log(n)) (g) (3 points) Consider the following C code: int i = 2; < int g = malloc(sizeof (int)); q = &i; Have we leaked memory yet? If so, explain where and if n wrl ss C code to free all of the dynamically allocated memory. K A & (h) (3 points) What's the difference\petween a data structure and an abstract data type? CS 136 Final Practice Set ring 2016 Page 10 of 18 This document is for tfie exclusive use of y87feng.
(i) (3 points) What’s the difference between a function declaration and a function definition? Provide an example of both. (j) (3 points) Describe what a pointer is in C. You may find it useful to use a picture of the memory model. Give an example of where pointers are useful. B X Q J / 7 2) (k) (3 points) What’s an opaque @uct e inC give an example (in code)? Provide an example of how opaque structures impact duia.ré:\(u’on. CS 136 Final Practice Set . Spring 2016 | Page 11 of 18 This document is for the exclusive use of y87feng.
5. (6 points) In the following code sample, identify (circle) three errors, and briefly explain why they are erroneous. 1 struct myarray { 2 void xxarray; 3 int size; Y 5 6 typedef struct myarray xMyArray; 7 8 MyArray construct_MyArray (int size) { [ = 9 MyArray newArray = malloc (sizeof (MyArray)) 10 newArray->size = size; 11 newArray->array = malloc(sizeof (void *) «* size); 12 13 return newArray; 14} 15 16 void destroy_MyArray (MyArray myArray) { 17 i myArray) { 18 & ee (myArray) ; i 19 } 20} 21 22 void insert_MyArray (MyArray myArray, i o:l. +xdata) { 23 assert (myArray) ; 24 if (myArray->size >=_index) { 25 myArray->array = |[(Dklloc (sizeo vo id # (index + 1)); 26 myArray->size = index + 12 27 28 else { 29 myArray->size++; 0 O 31 myArray-— >array[1ndex ta; 32} CS 136 Final Practice Set F]rm g 2016 Page 12 of 18 This document is for the exclusive use of y87feng.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
6. (10 points) Consider the following function declaration: #include <stdbool.h> struct bstnode { int key; int val; struct bstnode xleft; struct bstnode xright; Vi // bst_search (bst, i) determines if i exists in bst. // requires: bst is not NULL and bst is balanced // time: O(log(n)), where n is the number of nodes in bst. bool bst_search (struct bstnode *bst, int i); (a) (5 points) Write the implementation for bst_search recursively. (b) (5 points) Write the 1%0 lo)ost _search iteratively. F]rm g 2016 Page 13 of 18 CS 136 Final Practice Set e exclusive use of y87feng. This document is for t
7. (10 points) Write the C code for the following function: // filter(arr, n, pred) removes all elements from arr for which pred /7 evaluates to false and returns the size of the filtered array. // requires: arr is a dynamically allocated array of size n // effects: arr is mutated and resized to the minimum size to hold the // filtered elements. // time: O(n) int filter (int xxarr, int n, bool (*pred) (int)); O ) CS 136 Final Practice Set ring 2016 Page 14 of 18 This document is for tfie exclusive use of y87feng.
8. (10 points) Write the implementation for the following header file. #include <stdbool.h> // reverse_string (str) reverses the order of all the characters /7 /7 /7 inside of str. requires: str is valid effects: The order of str is mutated void reverse_string(char str); /7 /7 // char xcensor (char *str, char xword); censor(str, word) censors all occurrences of word in str. requires: str and word are not NULL, and str and word are valid C-strings effects: allocates a new block of memory on the heap that is the same size as the block of memory that str points to. Replaces all occurrences of word in str with asterisks ('+'). If there are nested occurrences, censor the first one (i.e. word is "bob" and str is "bobob" the result is "#*x0b"). caller must free resulting string. = time: O(nm), where n is the string length (of stx and m is the string length of word. ) s O\~ )& CS 136 Final Practice Set SF]ring 2016 This document is for the exclusive use of y87feng. Page 15 of 18
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
CS 136 Final Practice Set ring 2016 Page 16 of 18 This document is for tfie exclusive use of y87feng.
9. (15 points) In this question you will implement an ADT for controlling a robot. Initially, the robot is at the point (0,0). The robot can be given commands to move with store_instruction(), but will only execute them when go() is called. When go() is called, the robot will execute each action in the same order in which they were given via store_instruction(). After executing each action, go() should print out the current position followed by a newline (‘\n’) character (i.e., “(3,4)\n”). The instructions that can be sent to the robot are ‘u’, ‘d’, ‘I’, and ‘r’, which move the robot up, down, left, and right, respectively. Write the implementation for the header file given below. struct robot; typedef struct robot =*Robot; // create_Robot () creates a new robot. // effects: allocates a new block of memory for a new robot. The // new robot is at the origin facing north. // caller must make a call to destroy_ Robot. // time: O(1) < Robot create_Robot (void) ; // destroy_Robot (r) destroys the robot. // requires: r is a valid robot // effects: frees all of the memory ass 01at w11;h he robot, r. // time: O(n), where n is the number of tored instructions. void destroy_Robot (Robot r); // requires: r is a valid rgbot<and & is/one of { 'u', 'd', 'l1', 'r' } // effects: allocates a e 16chk of mefiory for an instruction. // time: 0(1) // store_instruction(r, i) st §16 ;Iytructlon for the robot. void store lnstructlon( i); // go(r) executes al the stored instructions for the robot. // requires: r is a obadt // effects: exec tes 11 Qf the stored instructions /7 frees al insfructions. // time: O(n), where\n is the number of queued instructions. void go (Robot r) , CS 136 Final Practice Set F]rm g 2016 Page 17 of 18 This document is for the exclusive use of y87feng.
CS 136 Final Practice Set ring 2016 Page 18 of 18 This document is for tfie exclusive use of y87feng.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help