CPSC 126 SAMPLE FINAL EXAMINATION ANSWERS April, 2003

Similar documents
CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

CPSC 121 Midterm 1 Friday October 14th, Signature: Section (circle one): 11:00 15:30 17:00

CPSC 121 Midterm 1 Friday February 5th, Signature: Section (circle one): Morning Afternoon

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

Question Marks 1 /11 2 /4 3 /13 4 /5 5 /11 6 /14 7 /32 8 /6 9 /11 10 /10 Total /117

Question Marks 1 /16 2 /13 3 /12 4 /15 5 /8 6 /15 7 /8 8 /5 9 /8 Total /100

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

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

CPSC 121 Sample Final Examination December 2013

CPSC 320 Midterm 2. July 13, 2007

UNIVERSITY REGULATIONS

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

UNIVERSITY REGULATIONS

Full Name: CS Account:

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

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 261: MIDTERM 1 February 14, 2017

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

THE UNIVERSITY OF BRITISH COLUMBIA CPSC 110: MIDTERM 1 Part B May 26, Important notes about this examination

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

CPSC 311, 2010W1 Midterm Exam #2

EXAMINATION INSTRUCTIONS

EXAMINATION INSTRUCTIONS

EXAMINATION INSTRUCTIONS

CS 216 Exam 1 Fall SOLUTION

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

CE161-4-AU UNIVERSITY OF ESSEX. Undergraduate Examinations 2014 DIGITAL SYSTEMS ARCHITECTURE. Time allowed: TWO hours

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

// a stack is printed from bottom (leftmost) to top (rightmost) System.out.println(stk);

CS 411 Midterm Feb 2008

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

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

! 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)

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

15110 PRINCIPLES OF COMPUTING SAMPLE EXAM 2

CPSC 410? Advanced Software Engineering Mid-term Examination (Term I ) SOLUTION Instructor: Gail Murphy

Problem 1. Multiple Choice (choose only one answer)

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

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

Structure and Interpretation of Computer Programs

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

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Computer Science 304

CISC 1600, Lab 2.1: Processing

CS1800 Discrete Structures Final Version A

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Section 1: True / False (2 points each, 30 pts total)

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

The University of British Columbia Final Examination - December 02, 2014 Mathematics 308. Closed book examination. No calculators.

Exercise 6.2 A generic container class

Structure and Interpretation of Computer Programs

COS 226 Algorithms and Data Structures Spring Midterm Exam

CISC 1600, Lab 3.1: Processing

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Final Examination 8:30 AM, Wednesday, April 18, 2012

Lecture Notes on Binary Decision Diagrams

UNIVERSITY REGULATIONS

COS 226 Algorithms and Data Structures Fall Midterm

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

1. Data types ( =13 points)

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

Data Structures and Algorithms

CS171 Midterm Exam. October 29, Name:

EXAMINATIONS 2016 TRIMESTER 2

ECE264 Spring 2013 Final Exam, April 30, 2013

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

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)

CS121/IS223. Object Reference Variables. Dr Olly Gotel

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Name CPTR246 Spring '17 (100 total points) Exam 3

The University of Oregon May 16, 2015 Oregon Invitational Mathematics Tournament:

Final Exam Principles of Imperative Computation Frank Pfenning, Tom Cortina, William Lovas. December 10, Name: Andrew ID: Section:

Lecture Notes on Tries

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

UW CSE 351, Winter 2013 Final Exam

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

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

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08025 INFORMATICS 1 - INTRODUCTION TO COMPUTATION

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

Subprograms, Subroutines, and Functions

Csci 102: Sample Exam

Final Exam Data Structure course. No. of Branches (5)

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination T09:00

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

CSE P 501 Exam 12/1/11

1 Getting started with Processing

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

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

Computer Science 1 Ah

Common Misunderstandings from Exam 1 Material

The University of Oregon May 16, 2015 Oregon Invitational Mathematics Tournament:

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Transcription:

CPSC 126 SAMPLE FINAL EXAMINATION ANSWERS April, 2003 Name: Student ID: Lab Section: You have 2 hours 45 minutes to write the 8 questions on this examination. A total of 105 marks are available. No notes, calculators, cellular phone, or other electronic equipment is allowed. Keeps your answers short. If you run out of space for a question, you have written too much. The number in square brackets to the left of the question number indicates the number of marks allocated for that question. Use these to help you determine how much time you should spend on each question. Use the back of the pages for your rough work. Good luck! Question 1 2 3 4 5 6 7 8 Total Marks UNIVERSITY REGULATIONS: No candidate shall be permitted to enter the examination room after the expiration of one half hour, or to leave during the first half hour of the examination. CAUTION: candidates guilty of any of the following, or similar, dishonest practices shall be immediately dismissed from the examination and shall be liable to disciplinary action. 1. Making use of any books, papers or memoranda, electronic equipment, or other memory aid devices, other than those authorised by the examiners. 2. Speaking or communicating with other candidates. 3. Purposely exposing written papers to the view of other candidates. The plea of accident or forgetfulness shall not be received. Smoking is not permitted during examinations.

page 2 out of 13 [10] 1. Short Answers [2] a. Briefly describe the difference between a vector and an array: A vector is an object which encapsulates an array and a size (at least). An array is just a pointer [2] b. Briefly describe the difference between a class and an object. A class defines a type. An object is a particular instance of a class. [6] c. Here is a function prototype: void foo(double *x, string & y, list<robot> a); Describe in words the types of each input argument, and what kind of values will each argument contain. double *x A pointer to double. x will contain the memory address of a double string & y A reference to string y is an alias of a string variable list<robot> a A list of Robot object a is an instance of the STL list class, with Robot as the type of variable in the list

page 3 out of 13 [10] 2. Representing values inside the computer [2] a. Convert the (decimal) number 58 to an 8-bit binary number. HINT: 58/2 = 29 and 29/2 = 14 + 1/2 00111010 [3] b. Calculate the 8-bit binary representation of 61 by adding 3 in binary (00000011) to your answer for (a). Show your work. Do not simply convert 61 to binary like you did for 58 in the part (a). 00111010 00000011 -------- 00111101 [2] c. Give the 8-bit binary two s complement representation of -61. Show your work 62 : 00111101 flip bits: 11000010 add one : 11000011 [3] d. Give the hexadecimal representation for 61, and for 58 61: 3d 58: 3a

page 4 out of 13 [8] 3. The R2D2 Robot Company Inc. has a new line of coffee delivery robots. These robots understand a simple programming language as specified by this EBNF: robot-program: motion-commands buy-command motion-commands "(deliver-coffee)" motion-commands: motion-command motion-command motion-command: "(" (move-command turn-command) digit digit ")" move-command: "move" ("forward" "backward") turn-command: "turn" ("right" "left") buy-command: "(buy-coffee" coffee-type size [ "cream" digit ] [ "sugar" digit ] ")" coffee-type: "dark" "light" size: "S" "L" digit: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" For each of the following programs, write either valid if its a valid program according to the EBNF given above, or invalid if it is not. If you write invalid, also give a reason. Don t worry about spaces or newlines. Remember that [a] means optional,... means zero or more repetitions, means or and "..." means as-is. (a) (move forward 10) (turn left 30) (buy-coffee dark L) (turn right 20) (move backward 300) (deliver-coffee) (jump-out-of-window) <--- invalid because of this (b) (turn right 200) (turn left 200) (buy-coffee dark S cream 2) valid (move forward 20) (deliver-coffee) (c) (move forward 20) (buy-coffee light L sugar 12) <-- invalid cannot have 12 sugars (move backward 20) (deliver-coffee) (d) (turn left 20) (move forward 300) (turn right 30) (move forward 10) (buy-coffee dark S 2) <-- invalid must have sugar or cream keyword (deliver-coffee) <-- invalid must have a motion-command

page 5 out of 13 [15] 4. A program for a day at the horse races at the Blue Bonnets race track might look like the following Race 1 9:30 am Fillies 3/4mi 4 14 32:1 H: C rules O: G. Lee J: R. St-Aubin 34 16:1 H: Assembly so cool O: B. Dow... Race 2 1:40 pm 1 yr olds 1 1/4mi $10,000 14 100:1 H: Schemers Delight O: P. Belleville 34 14:1 H: Prolog Clause O: D. Poole J: M. Horsch... A program consists of 1 to 5 races. Each race starts with the word Race and the race number. Then comes the time of the race (in 12-hour format: hh:mm where hh ranges from 1-12 and mm from 0-59 and the morning/afternoon indicator). This is followed by the type of horse and the length of the race, which is in miles (is followed by mi ), and can be a number (like 1) or a fraction (like 3/4) or a combination of both (like 1 3/4). The longest race possible is 9 miles, and fractions are always in 1/4 mile increments. At the end of the line comes either the purse (the amount the winner gets), or the number of a race for which this is the qualifying race. Purses can be any sum greater than 0 preceded by a $, but must be in the notation with sets of three digits separated by commas, and must not include any leading zeros. Thus, $10,900 and $9,999,999 are valid purses, but 10,990 and $01,393 are not. Following the header comes a list of 2 or more horses, each of which is a number, the odds (number : number), followed by the horse name (preceded by H: ), the owner name (preceded by O: ) and the jockey name (preceded by J: ). The jockey name is left out if the owner is the jockey. Write the EBNF for a horse race program at Blue Bonnets. Some of the categories you might want to use have been defined already. horse-type: Fillies 1 yr olds 2 yr old mares horse-name: C rules Assembly so cool Schemers Delight Prolog Clause owner-name: G. Lee B. Dow P. Belleville D.Poole jockey-name: R. St-Aubin M. Horsch R. Dearden one3: 1 2 3 one5: one3 4 5 six9: 6 7 8 9 one9: one5 six9 one12: one9 10 11 12 digit: 0 one9 number: one9 digit

page 6 out of 13 program: Race [Race] [Race] [Race] [Race] Race: Race one5 time horse-type length ( $ purse less5) horselist horselist: horse horse horse time: one12 : ( 0 one5) digit ( am pm ) length: one9 fraclen (one9 fraclen) fraclen: one3 /4 purse: ((one9 one9 digit one9 digit digit), threedig) threedig: digit digit digit horse: number odds H: horse-name O: owner-name [ J: jockey-name] odds: number : number [12] 5. A drawing program can display a variety of shapes including points, lines, circles and rectangles. A point represents the X and Y coordinates of a pixel (picture element) on the screen (coordinates are integer valued). A line is represented by a starting point and an ending point. A circle is represented by the center point, and the radius in pixels (which may be a fractional value). A rectangle is represented by four points, one for each corner. All shapes have a border colour. Colours are made up from a red, a green and a blue 2-digit hexadecimal number. Use the Object Oriented Design technique discussed in class to design the classes for this program For each class, draw a circle labeled with the name of the class. Then, link the circles up with arrows labeled with the type of relationship between the classes (has-a or is-a). Finally, look at each has-a relationship, and write inside each circle what the member variables and their types will be for each object of that class. If you use a predefined C++ class, you do not need to specify its member variables or their types.

page 7 out of 13 colour hex red hex blue hex green is a shape colour border line point start point end is a is a is a circle point center double radius hex rectangle point tl corner point tr corner point bl corner point br corner point integer x coordinate integer y coordinate integer double

page 8 out of 13 [15] 6. A Trie is a data structure for storing a list of words. A Trie is a graph which is structured like a tree, and in which every node is a character. Every path from the root of the Trie to a leaf is a word preceded by a $ and followed by a #. A word will only appear in a Trie once. For example, here is a Trie which stores the list of words $ D G O O # G A L # T F "DOG","GOAT","GOLF" and "DO": Tries are described by the following class: class Trie private: // omitted public: // returns the character at the root of the Trie char root(); // returns a list of Tries which are the // subtries of this Trie. If called on a leaf Trie, // this returns an empty list. list<trie> subtrees(); ; # # Write a function trie_search which takes a reference to a Trie and a string as arguments, and returns the boolean result of checking if the string is in the Trie. Thus, if t is the Trie shown above, trie_search(t,"do") will return true, while trie_search(t,"golfer") will return false. HINT: Your function can be recursive. Note that the empty string is in any Trie with a # at the root of one of its subtree, and that a string s is in a trie if (1) the first character of the string is at the root of one of the subtrees and (2) the rest of the string is in that subtree whose root is the first character.

page 9 out of 13 bool trie_search(trie & t, string & s) list<trie> st = t.subtrees(); list<trie>::iterator ist = st.begin(); char what( # ); if (s.size() > 0) what = s.at(0); while (ist!= st.end() && (*ist).root()!= what) ist++; if (ist == st.end()) return false; else if ((*ist).root() == # ) return true; else return trie_search(*ist,substr(s,1,s.size()-1));

page 10 out of 13 [25] 7. This question deals with vectors of integers. [5] a. It is easy to remove the last element from a STL vector using the pop_back() method. If we don t care about the order of the elements in the vecor, we can remove an element at an arbitrary position, pos, as follows: (1) move the value at the end of the vector into position pos (2) remove the last element of the vector. Write a function removefrommiddle which takes a reference to a vector<int>, and an integer position, pos, and removes and returns the element of the vector at position pos using this method. int removefrommiddle(vector<int> & x, int pos) int r = x[pos]; x[pos] = x[x.size()-1]; x.pop_back(); return r; [5] b. Write a function, shuffle, which takes a reference to a vector of int, and rearranges the elements of the vector in a random order. You should not do this by simply swapping random values in the vector some number of times, as this will not guarantee a good shuffle. Use the function int random(int mx), which generates a random integer between 0 and mx (including 0 and mx). HINT: Use your function removefrommiddle from the first part of this question (even if you haven t written it). void shuffle(vector<int> & x) int r,i(0); vector<int> y(x); while (!y.empty()) x[i++] = removefrommiddle(y,rand(y.size()-1));

page 11 out of 13 [8] c. Write a function sorted, which takes a reference to a vector of int, and returns true if the vector is sorted in ascending order, false otherwise. bool sorted(vector<int> & x) int i(0); bool sorted(true); while (sorted && i < x.size()-1) if (x[i] < x[i+1]) i++; else sorted = false; return sorted; [4] d. Rediculous Sort, invented by Yoshe J. Reeds, is a rediculous method of sorting a vector of integers which works as follows: (1) shuffle the vector, (2) check if its sorted. If it is, then stop, otherwise go back to (1). Yoshe cannot program in C++, and so has asked you to implement his sorting method. Write a function rediculoussort, which takes a reference to a vector of int, and sorts the vector using Yoshe s method. Your function should return the number of shuffles that were used to sort the vector. int rediculoussort(vector<int> & x) int n(0); while (!sorted(x)) shuffle(x); n++; return n; [3] e. What is the worst-case time complexity of rediculoussort? With some infinitely small non-zero probability, it will take infinite time.

page 12 out of 13 [10] 8. Consider the following LinkedList and Node classes, which are similar to (but not identical to) classes discussed in the lectures and tutorials: class Node public: Node(string elem,node* next); string element(); Node* next(); private: string currentelement; Node* nextnode; ; class LinkedList public: int size(); Node* first(); void deletefirst(); void insertinfront(string s); private: Node* firstnode; int numberelements; ; Write a function isrepeated that takes a LinkedList object as parameter, and returns true if the LinkedList consists of x strings followed by the same x strings in backwards order. For instance, your function should return true for the LinkedList containing the words ( I am hungry hungry am I ), and for the LinkedList containing the words ( stutter stutter ), but it should return false for the LinkedList containing the words ( I am I ), or the LinkedList containing the words ( I hate hate exams ). The marks given for this question will depend on the efficiency of your algorithm. To obtain full marks, you need to write a solution that runs in O(n) time, where n is the size of the input list. You are not allowed to modify the LinkedList object passed as parameter to your function. Hint: use a stack. bool isrepeated(const LinkedList& ls) std::stack<int> stk; Node *current = ls.first(); // // A list with an odd number of elements can not // be two copies of the same thing. // if (ls.size() % 2 == 1) return false;

page 13 out of 13 // // Push the first half of the list onto a stack. // for (int i = 0; i < ls.size() / 2; i++) stk.push((*current).element()); current = (*current).next(); // // Now compare the second half with the first half // that we pushed onto a stack. // for (int i = 0; i < ls.size() / 2; i++) if ((*current).element()!= stk.top()) return false; stk.pop(); current = (*current).next(); return true;