CSE 333 Midterm Exam 2/14/14

Similar documents
CSE 333 Midterm Exam 5/10/13

CSE 333 Midterm Exam July 24, Name UW ID#

CSE 333 Midterm Exam 7/25/16. Name UW ID#

CSE 333 Midterm Exam Sample Solution 5/10/13

CSE 333 Midterm Exam 7/29/13

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

CSE 333 Midterm Exam 5/9/14 Sample Solution

CSE 333 Midterm Exam 7/22/12

CSE 333 Midterm Exam 7/27/15 Sample Solution

CSE 333 Midterm Exam 2/12/16. Name UW ID#

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

CSE 333 Midterm Exam July 24, 2017 Sample Solution

CSE 303 Midterm Exam

CSE 333 Final Exam 3/19/14

CSE 333 Midterm Exam Nov. 3, Name UW ID#

CSE 374 Final Exam Sample Solution 3/17/11

CSE 333 Midterm Exam Sample Solution 7/28/14

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

CSE 331 Midterm Exam 2/13/12

CSE 374 Final Exam 3/15/17. Name UW ID#

Solution to CSE 250 Final Exam

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

Part I: Short Answer (12 questions, 65 points total)

CSE 374 Midterm Exam 11/2/15. Name Id #

CSE351 Winter 2016, Final Examination March 16, 2016

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

CSE 303, Winter 2007, Final Examination 15 March Please do not turn the page until everyone is ready.

CSE 303 Final Exam. March 16, 2009

Signature: ECE 551 Midterm Exam

CSE 303, Winter 2007, Final Examination 15 March Please do not turn the page until everyone is ready.

Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

ECE 15B COMPUTER ORGANIZATION

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Computer Science Foundation Exam

Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

Creating a String Data Type in C

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

C Programming Basics II

Computer Science Foundation Exam

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

CSE 331 Final Exam 12/9/13

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CSE 303, Spring 2006, Final Examination 6 June Please do not turn the page until everyone is ready.

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CS 241 Data Organization Binary Trees

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Second Examination Solution

CSE 374 Final Exam 3/15/17 Sample Solution. Question 1. (8 points) Suppose we have the following two statements in a C program:

CSE wi Midterm Exam 2/8/18. Name UW ID #

Computer Science Foundation Exam

CSE 374 Midterm Exam Sample Solution 2/11/13. Question 1. (8 points) What does each of the following bash commands do?

Common Misunderstandings from Exam 1 Material

Trees, Part 1: Unbalanced Trees

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

G52CPP C++ Programming Lecture 13

CS 237 Meeting 18 10/22/12

CSE 331 Final Exam 3/12/12

COP 3502 Section 2 Exam #2 Version A Spring /23/2017

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

CSE413 Midterm. Question Max Points Total 100

CSE 333 Midterm Exam Sample Solution 7/29/13

CSE 333 Final Exam June 6, 2017 Sample Solution

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

CS 237 Meeting 19 10/24/12

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Largest Online Community of VU Students

ECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly)

CS61C : Machine Structures

ESC101N: Fundamentals of Computing End-sem st semester

Computer Science Foundation Exam

CS 104 (Spring 2014) Final Exam 05/09/2014

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

AIMS Embedded Systems Programming MT 2017

Computer Science Foundation Exam

CSE 333 Autumn 2013 Midterm

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CS2102, D15 Exam 1. Name:

CS 223: Data Structures and Programming Techniques. Exam 2

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

CMSC 341 Lecture 10 Binary Search Trees

ECE264 Fall 2013 Exam 3, November 20, 2013

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Kurt Schmidt. October 30, 2018

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Fast Introduction to Object Oriented Programming and C++

Short Notes of CS201

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

DAY 3. CS3600, Northeastern University. Alan Mislove

CSE P 501 Exam 12/1/11

CSE P 501 Exam 8/5/04

Fall 2018 Discussion 2: September 3, 2018

WHAT POINTERS REALLY ARE

Transcription:

Name There are 4 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes, closed electronics, closed telepathy, open mind. If you don t remember the exact syntax for something, make the best attempt you can. We will make allowances when grading. Don t be alarmed if there seems to be more space than is needed for your answers we tried to include more than enough blank space. Relax, you are here to learn. Please wait to turn the page until everyone is told to begin. Score / 100 1. / 36 2. / 34 3. / 28 4. / 2 Page 1 of 10

Question 1. (36 points) C programming. Recall from CSE143 that a Binary Search Tree (BST) is a binary tree where each node contains a value, and for each node n, every value in the left subtree of n is less than the value stored in n, and every value in the right subtree of n is greater than the value stored in n. For this problem we will use the following typedef to define the nodes of a BST of C strings: typedef struct bst_node { char *str; // heap-allocated string for this node struct bst_node *left; // left subtree with strings < str struct bst_node *right; // right subtree with strings > str BST_Node, *BST_NodePtr; Each node in the BST is allocated on the heap. The string pointer str in the node points to an array of characters on the heap that is allocated when the node is created and holds a private copy of the string data associated with that node. You may make the following simplifying assumptions while answering this question: You may assume that malloc always succeeds when it is called. All string values are properly terminated with a \0 byte and are not NULL. You may use the unsafe but simpler string functions like strcpy and strcmp. Assume that all necessary header files like string.h have been #included. Reminders about a few possibly useful string functions. All string arguments have type char*. strlen(s) returns the number of characters (bytes) in s, not including the \0 at the end. strcpy(dst,src) copies src to dst. strcat(dst,str) appends a copy of src to the end of dst. strcmp(x,y) returns 0 if strings x and y are the same, some negative integer if x<y, and some positive integer if x>y. Complete the functions on the following pages. You may remove this page for reference if you wish. Hint: recursion is your friend (really!). (continued next page) Page 2 of 10

Question 1. (cont.) (a) (12 points) Complete the following function so it frees all dynamically allocated storage associated with the BST whose root node is r. Note that r may be NULL if the BST is empty. Do not be alarmed if there is a lot more space here than you need for your answer. void free_tree(bst_nodeptr r) { Page 3 of 10

Question 1. (cont.) (b) (24 points) Complete the implementation of insert below so that insert(str,r) adds a copy of str to the BST with root r if str is not already present in the tree, and returns a pointer to the root of the modified tree. If str is already in the tree, insert should just return the pointer to the root of the original tree. For example, the following code adds pqr to the BST whose root node is words if pqr is not already present. words = insert( pqr, words); Note that the tree (words in this example) may be empty (NULL) initially. When new words are added, the code should preserve the binary search tree property that left subtrees have values less than their parent node, and right subtree values are greater. For full credit your answer should only traverse the necessary nodes in the tree. BST_NodePtr insert(char *s, BST_NodePtr r) { Page 4 of 10

Question 2. (34 points) The traditional twisted C program. This code does compile and execute without warnings or errors. #include <stdio.h> void Two(int **p, int *q, int *r) { (*p)++; (*q)++; (*r)++; // HERE (see instructions for part (a)) (**p)++; printf("two: **p = %d, *q = %d, *r = %d\n", **p, *q, *r); void One(int *v, int *x, int n) { Two(&v, &n, x); (*v)++; (*x)++; printf("one: *v = %d, *x = %d, n = %d\n", *v, *x, n); int main() { int a[] = { 1, 3, 7, 11 ; int b = 17; One(a, &b, 41); for (int k = 0; k < 4; k++) { printf("%d ", a[k]); printf("%d\n", b); return 0; Answer questions about this program on the next page. You may remove this page for reference if you wish. Page 5 of 10

Question 2. (cont.) (a) (20 points) Draw a boxes n arrows diagram showing state of memory when control reaches the comment containing HERE in the middle of function Two. Your diagram should have three boxes showing the stack frames for functions main, One, and Two. The stack frames should include values of integer variables and an arrow from each pointer to the location that it references. Then answer part (b) at the bottom of the page. (b) (14 points) What output does this program produce when it is executed? Page 6 of 10

Question 3. (28 points) Dept. of Computational Entomology. The following code declares a C++ class that acts like a C++ vector containing strings. New strings can be added to the end of a StringList using the push_back function, and the StringList is supposed to expand as needed to hold new values. (This class omits many useful operations so we can keep it fairly small for this exam. However, the code on this page is correct and compiles successfully.) #include <string> using namespace std; class StringList { public: // Default constructor: Initialize a new, empty StringList // with default capacity StringList(); // copy constructor StringList(const StringList &other); // destructor ~StringList(); // add new string s to the end of this StringList, expanding // the list as needed void push_back(const string &s); private: // StringList representation: // strings_ : an array allocated on the heap holding strings. // capacity_ : the number of elements in the array strings_. // size_ : the number of items currently in the list, where // 0 <= size_ <= capacity_. The items are stored in // strings_[0..size_-1]. string *strings_; int capacity_; int size_; // initial capacity of a new empty StringList static const int default_capacity_ = 4; ; (Continued on the next page. You may remove this page for reference.) Page 7 of 10

Question 3. (cont.) A separate file contains the following implementations of the functions declared on the previous page. Unfortunately there are bugs lots of bugs. Mark the code below to identify the problems, and write in corrections so the code will work as intended. Keep your notes brief and to the point. You can write additional comments or changes on the next page, but please help the graders by making it easy to follow your changes and figure out where they fit. You may cross out some of the code on this page and rewrite it on the next page if that is easier. // Default constructor StringList() : capacity_(default_capacity_), size_(0) { strings_ = new string[capacity_]; // copy constructor StringList::StringList(const StringList &other) : capacity_(other.capacity_), size_(other.size_), strings_(other.strings_) { // destructor StringList::~StringList() { delete strings_; // add string s to the end of this StringList void StringList::push_back(const string &s) { strings_[size_] = s; size_++; Page 8 of 10

Question 3. (cont.) Additional space for your corrections to the code on the previous page, if needed. Page 9 of 10

Question 4. (2 points) Draw something interesting below. By definition, all drawings made in answer to this question are interesting and will receive the free points. Page 10 of 10