PRACTICE MIDTERM EXAM #2

Similar documents
PRACTICE MIDTERM EXAM #1

PRACTICE FINAL EXAM #4 (DRESS REHEARSAL)

PRACTICE FINAL EXAM 2

PRACTICE FINAL EXAM 3

CS106X Handout 25 Autumn 2012 October 23 rd, 2012 CS106X Midterm Examination

CS106X Handout 24 Winter 2018 February 9 th, 2018 CS106X Midterm Examination

CS106X Handout 17 Winter 2015 January 28 th, 2015 CS106X Practice Exam

CS106X Handout 39 Autumn 2012 November 28 th, 2012 CS106X Midterm Examination II

CS106X Handout 35 Winter 2018 March 12 th, 2018 CS106X Midterm Examination

Winter 2018 March 12 th, 2018 CS106X Midterm Examination Solution

Practice Final Examination #2

CSE 143 Sample Midterm Exam #4

Section Handout #4: Classes, Pointers, and Dynamic Memory

CSE 143 Sample Midterm Exam #1

CS 106X Sample Final Exam #2

Programming Abstractions

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Practice Final Examination #2

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Practice Midterm Exam #1

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Programming Abstractions

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

CSE 143 Section Handout #13 Practice Midterm #5

Programming Abstractions

CS106B Sprint 2017 April 27 th, 2017 CS106B Practice Midterm (KEY)

CS 314 Midterm 2 Fall 2012

CSE 143, Winter 2009 Sample Midterm Exam #2

CSE 333 Midterm Exam July 24, Name UW ID#

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

cameron grace derek cameron

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

The examination is open-book, and you may make use of any texts, handouts, or course notes. You may not, however, use a computer of any kind.

UNIVERSITY REGULATIONS

CSE 333 Midterm Exam 5/10/13

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

PROBLEM 1 : (I'll huff and I'll puff and I'll... (10 pts)) The Huffman tree below is used for this problem. e s r o t Part A(4 points) Using this tree

ECE242 Data Structures and Algorithms Fall 2008

Programming Abstractions

Points off Total off Net Score. CS 314 Final Exam Spring 2016

CS106B Midterm Review

"apple" "grape" "grape" "grape" "apple"

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Largest Online Community of VU Students

CS 314 Exam 2 Spring

CSC 210, Exam Two Section February 1999

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

CSE 333 Final Exam 3/19/14

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Tutorial Letter 102/1/2012 Programming: Data Structures

Chris and Chris Fall 2016 Nov 3 rd, 2016 CS106B Midterm

COS 226 Algorithms and Data Structures Fall Midterm

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Created by Julie Zelenski, with edits by Jerry Cain, Keith Schwarz, Marty Stepp, Chris Piech and others.

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

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

UNIVERSITY REGULATIONS

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

CS 106B Practice Final Exam #8 (15au) ANSWER KEY

Midterm I Exam Principles of Imperative Computation Frank Pfenning. February 17, 2011

1 Short Answer (10 Points Each)

CIS 110 Introduction to Computer Programming Summer 2018 Midterm. Recitation ROOM :

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

CIS 110 Introduction to Computer Programming Summer 2016 Midterm. Recitation # (e.g., 201):

CSE 333 Midterm Exam Sample Solution 5/10/13

CS106X Handout 26 Winter 2018 February 12 th, 2018 Section Handout

This examination has 11 pages. Check that you have a complete paper.

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

1) Holiday Lights 30 Points

CS 314 Midterm 1 Spring 2014

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 3

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Midterm Exam 2 CS 455, Spring 2015

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Winter 2017 March 2, 2017 CS107 2 nd Midterm Examination

Data Structure (CS301)

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

a) Write the signed (two s complement) binary number in decimal: b) Write the unsigned binary number in hexadecimal:

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

Do not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL. Maximum possible points:

CMSC 202 Midterm Exam 1 Fall 2015

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

COS 226 Algorithms and Data Structures Spring Midterm Exam

Transcription:

This practice exam is based on the actual midterm exam from Cynthia s Spring 2014 class. It did not include a classes problem (which you should expect this quarter), and the memory/pointers problem covered material that we covered differently this quarter and needed to be replaced. So I added a classes problem and a linked nodes problem from one of Marty Stepp s practice exams. Because the exam now has one more problem than it did before, this exam is somewhat longer than what you should expect for the actual midterm exam. --CBL CS106B Spring 2016 Instructor: Cynthia Lee Practice Exam PRACTICE MIDTERM EXAM #2 NAME (LAST, FIRST): SUNET ID: @stanford.edu Problem 1 2 3 4 4 Linked Short Topic ADTs Recursion Classes Nodes Answer TOTAL Score Possible 20 20 20 20 20 100 Instructions: The time for this exam is 2 hours, or 120 minutes. There are 100 points possible, so about 1-1.5 minutes per point. This should give you a general sense of pacing, though each person will have different problems that challenge them more or less than others. Use of anything other than a pencil, eraser, pen, one 8.5x11 page (two sides) of notes, and the official textbook is prohibited. In particular, no computers or digital devices of any kind are permitted. Blank scratch paper may be provided by proctors and does not need to be turned in. PLEASE rip off the pages of library reference in the back of the exam and do not turn them in. Please do NOT staple or otherwise insert new pages into the exam. Changing the number of pages in the exam confuses our automatic scanning system. Thanks. SCPD and OAE: Please call or text Cynthia @ 760-845-7489 if you have a question. Please sign before you begin: I agree to abide by the spirit and letter of the Honor Code, and to follow the instructions above. (Signature) (Date)

1. ADTs (20pts). For this problem, you will write an autocomplete feature for typing on a phone or in the Google search box. You will suggest words that complete what the user has typed so far, like in this example: You should write two functions, with the following signatures: void calculateprefixes(lexicon& english, Map<string,Set<string>>& prefixes); Given a Lexicon, you will populate a Map (you may assume it is initially empty) with all prefixes found in all the words in the Lexicon. Each prefix maps to a Set of strings that are all the words starting with that prefix. (This is a very space-inefficient way to store this information we ll learn something better on that count later in the quarter but it will be fast.) Usually we say that it s not a good idea to loop over a Lexicon, but in this problem, because we are processing all the words, you will want to loop over the Lexicon. We will not consider the empty string to be a valid prefix. Example: Assume (for this example only) that the Lexicon only contains these 3 words: bat cat bag. Then when the function returns, the prefixes map should contain the following mappings: b { bat, bag, c { cat, ba { bat, bag, ca { cat, bat { bat, bag { bag, cat ->{ cat. void printsuggestions(map<string,set<string>>& prefixes, string typing); Given the map (as created in the calculateprefixes function), and a string typing that is what the user has typed so far, print to cout a list of words starting with the prefix typing (if there are none, do not print anything to cout). Each word should print on its own line. Example: Using the same example as above, if typing is ba, then the output of your program should be as follows: bag bat You may print the words in any order. Note: one page of space is provided for you to write each function, but this is likely much more space than you will actually need.

void calculateprefixes(lexicon& english, Map<string,Set<string>>& prefixes){

void printsuggestions(map<string,set<string>>& prefixes, string typing){

2. Linked Nodes (20pts) [by Marty Stepp]. Examine the following code. Write the code that will turn the "before" picture into the "after" picture by modifying links between the nodes shown and/or creating new nodes as needed. There may be more than one way to write the code, but you are NOT allowed to change any existing node's data field value. You also should not create new ListNode objects unless necessary to add new values to the chain, but you may create a single ListNode* pointer variable to point to any existing node if you like. If a pointer variable does not appear in the "After" picture, it doesn't matter what value it has after the changes are made. If a node does not appear in the "After" picture, you must free its memory to avoid a memory leak. To help maximize partial credit in case you make mistakes, we suggest that you include optional comments with your code that describe the links you are trying to change, as shown in the solution code for the linked list section handout. Before After Assume that you are using the ListNode structure as defined in lecture and section: struct ListNode { int data; // data stored in this node ListNode* next; // a link to the next node in the list ListNode(int data, ListNode* next) {... // constructor

3. Recursion (20pts). After the midterm is over, you plan to drive to Reno with some friends to relax for the weekend. You haven t ever been to a casino, and you d like to make some notes for yourself to help you get started playing the card game Blackjack. On the most basic level, the objective in Blackjack is to gather cards that sum to 21 points. Each card with a number contributes that many points to the sum, and the Ace can be either 1 point or 11 points (your choice). We will ignore face cards (the Jack, Queen, and King). For this problem, we will represent a card as an int, and the values will be between 1 and 10 as follows: Card Represented by int value Points Ace 1 1 or 11 2-10 2-10 2 = 2 points, 3 = 3 points, etc. Write a function with the following signature: void sum21(vector<int>& availablecards, Vector<Vector<int>>& waystosum) It will find all the ways of making a sum of 21 with the cards provided in availablecards. availablecards is a list of the cards available to you. You may only use each card once, but the same number may appear in the vector more than once to reflect more than one card of that number is available o Example: if the vector contains 2, 2, 2, 2, 3, 10, then you could return the whole list as one way of summing to 21, but you could not return a solution of 2, 2, 2, 2, 2, 2, 2, 2, 2, 3. The latter sums to 21, but uses more than the available 2 cards. o If you wish, you may destructively modify availablecards. In other words, it doesn t need to be in the same condition you received it in when you return. waystosum is a vector, initially empty, that you should populate as the output of your function. Your output should include every combination of the available cards that sums to 21. o It is acceptable for there to be repetition in your waystosum result. In other words, your result may contain two vectors that are exactly the same or some permutation (order rearrangement) of each other, but you do not need to go out of your way to generate every permutation. Your solution must use recursion. The provided function signature may be used as a wrapper that calls a separate helper recursive function. You may not use any global variables. Example: Input: o availablecards = {2,1,5,5,2,3,3 Output: Notes: o o waystosum = {{5,5,1, {1,2,2,5,5,3,3, {1,2,2,3,3, {5,5,1 o Two of the combinations use the Ace as 11 points, and one combination uses the Ace as 1 point. There is a repetition of one of the combinations, which is neither banned nor required.

void sum21(vector<int>& availablecards, Vector<Vector<int>>& waystosum){

4. Classes (20pts) [by Marty Stepp]. Write a member function maxcount to be added to the ArrayList class from lecture. Your function should examine the elements of the list and return the number of occurrences of the most frequently occurring value in the list of integers. For this problem, you should assume that the elements in the list are in sorted order. Because the list will be sorted, all duplicates will be grouped together, which will make it easier to count duplicates. For example, suppose that an ArrayList called list stores the following sequence of values: {1, 3, 4, 7, 7, 7, 7, 9, 9, 11, 13, 14, 14, 14, 16, 16, 18, 19, 19, 19 This list has some values that occur just once or twice, some values that occur three times (14, 19) and a single value that occurs four times (7). Therefore, the client's call of list.maxcount() should return 4 to indicate that the most frequently occurring value occurs 4 times. It is possible that there will be a tie for the most frequently occurring value, but that doesn't affect the outcome because you are just returning the count, not the value. If there are no duplicates in the list, then every value will occur exactly once and the maximum would be 1. If the list is empty, you should return 0. You may call other member functions of ArrayList if you like, but your function should not modify the state of the list. Its header should be declared in such a way as to indicate to the client that this function does not modify the list. In the space below, write the member function's body as it would appear in ArrayList.cpp. You do not need to write the function's header as it would appear in ArrayList.h. Write only your member function, not the rest of the class. Remember that you are adding this as a member function to the ArrayList class from lecture: class ArrayList { public: void add(int value); void clear(); int get(int index) const; void insert(int index, int value); void remove(int index); void set(int index, int value); int size() const; string tostring() const; private: int* elements; int mysize; int capacity; void checkcapacity(); void checkindex(int i, int min, int max);

5. Short Answer (20pts). a. Which of the following data structures allow you to retrieve any element in the data structure at any time? (circle ALL that apply) Vector Stack Queue Grid b. T/F: You cannot have a pointer that points to memory on the stack. (circle ONE) TRUE FALSE c. T/F: One of the base cases in our recursive Binary Search algorithm from class (recall it looks for a number in a sorted Vector of numbers) was that there are only three numbers left to search, so we check each one to see if it is the number we re looking for. (circle ONE) TRUE FALSE d. What is the best Big-O characterization of the performance of this code, in terms of N, where N is the size of the vector? (Write your answer on the line.) for (int i=3; i<data.size()/5; i++){ cout << data[i] << endl; O( ) e. What is the best Big-O characterization of the performance of this code, in terms of N, where N is the size of the vector? (Write your answer on the line.) for (int i=0; i<data.size(); i+=(data.size()/5)){ cout << data[i] << endl; O( )

f. What is the best Big-O characterization of the performance of this code, in terms of N, where N is the size of the vector data? (Write your answer on the line.) for (int i=data.size()-1; i>=0; i-=3){ for (int j=0; j<data.size(); j+=3){ cout << data[i] << data[j] << endl; O( ) g. (6pts) What are two different uses for the & symbol in C++ code? Write one short sentence each, naming and explaining the use. (Please only list two.) h. What are two different reasons you might use pass by reference in when writing a function in C++? Write 1-2 short sentences each, explaining the use. (Please only list two.) i. (0pts - Optional) What is the most interesting thing you ve learned in class so far?

Summary of Relevant Data Types We tried to include the most relevant member functions and operators for the exam, but not all are listed. You are free to use ones not listed here that you know exist. You do not need to do #include for these. class string { bool empty() const; int size() const; int find(char ch) const; int find(char ch, int start) const; string substr(int start) const; string substr(int start, int length) const; char& operator[](int index); const char& operator[](int index) const; class Vector { int size() const; void add(const Type& elem); // operator+= used similarly void insert(int pos, const Type& elem); void remove(int pos); Type& operator[](int pos); class Grid { int numrows() const; int numcols() const; bool inbounds(int row, int col) const; Type get(int row, int col) const; // cascade of operator[] also works void set(int row, int col, const Type& elem); class Stack { void push(const Type& elem); Type pop(); class Queue { void enqueue(const Type& elem); Type dequeue();

class Map { int size() const; void put(const Key& key, const Value& value); bool containskey(const Key& key) const; Value get(const Key& key) const; Value& operator[](const Key& key); Example for loop: for (Key key : mymap){ class Set { int size() const; void add(const Type& elem); // operator+= also adds elements bool contains(const Type& elem) const; Example for loop: for (Type elem : mymap){ class Lexicon { int size() const; void clear(); void add(std::string word); bool contains(std::string word) const; bool containsprefix(std::string prefix) const; Example for loop: for (string str : english){