Please note, you CANNOT petition to re-write an examination once the exam has begun.

Similar documents
UNIVERSITY OF TORONTO AT MISSISSAUGA April 2006 Examination CSC 324H5 S Instructor: Richard Krueger Duration 3 hours No Aids Allowed

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

CSE413 Midterm. Question Max Points Total 100

CSE341 Spring 2017, Final Examination June 8, 2017

Good Luck! Marking Guide. APRIL 2014 Final Exam CSC 209H5S

UNIVERSITY OF TORONTO Faculty of Arts and Science

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

CSE341 Spring 2017, Final Examination June 8, 2017

Assignment 1. Due Tuesday October 11 at 11pm. No late assignments will be accepted. What to do

UNIVERSITY OF TORONTO Department of Computer Science April 2014 Final Exam CSC373H1S Robert Robere Duration - 3 hours No Aids Allowed.

11 aid sheets., A non-programmable calculator.

CSE341 Autumn 2017, Final Examination December 12, 2017

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

Read this before starting!

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

by the evening of Tuesday, Feb 6

CS 3114 Data Structures and Algorithms READ THIS NOW!

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

CMSC 331 Final Exam Section 0201 December 18, 2000

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Sample Final Exam Questions

UMBC CMSC 331 Final Exam

BIG IDEAS. A.REI.D.10: Interpret Graphs as Sets of Solutions. Lesson Plan

York University. AP/ITEC Section M INTRODUCTION TO DATA STRUCTURES Winter Midterm Test

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

CS 3114 Data Structures and Algorithms READ THIS NOW!

PRACTICE FINAL EXAM 3

Solution READ THIS NOW! CS 3114 Data Structures and Algorithms

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

YOUR NAME PLEASE: *** SOLUTIONS ***

Lecture 3: Recursion; Structural Induction

CS 2316 Exam 4 Fall 2011

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

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

Scheme Basics > (butfirst '(help!)) ()

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

Read this before starting!

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

CPSC 121 Sample Final Examination December 2013

UNIVERSITY OF TORONTO Faculty of Arts and Science. December Examinations Duration 3 hours

CS1800 Discrete Structures Fall 2016 Profs. Aslam, Gold, Ossowski, Pavlu, & Sprague December 16, CS1800 Discrete Structures Final

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

UNIVERSITETET I OSLO

CSE341 Spring 2016, Final Examination June 6, 2016

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

CS 275 Name Final Exam Solutions December 16, 2016

Vector Geometry (12 points)

CSc 372, Fall 2001 Final Examination December 14, 2001 READ THIS FIRST

CSE341, Fall 2011, Midterm Examination October 31, 2011

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

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CSE 131 Introduction to Computer Science Fall Exam II

Principles of Programming Languages, Spring 2016 Assignment 5 Logic Programming

Programming with Patterns. ACM SIGPLAN Developer Tracks on Functional Programming (DEFUN 2009)

CSE341 Spring 2016, Midterm Examination April 29, 2016

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

Warm Up! Complete the warm-up questions on your warm-up paper. Solve each absolute value and represent them on a number line.

CS Introduction to Artificial Intelligence

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

University of Massachusetts Lowell

Second Examination Solution

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

Computer Science Foundation Exam

Questions Total Points Score

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

CSE 413 Winter 2001 Midterm Exam

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

CPSC 311, 2010W1 Midterm Exam #2

UNIVERSITY REGULATIONS

Programming Paradigms Written Exam

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Structure and Interpretation of Computer Programs

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

MATH 2100 / 2105 / 2350 EXAM 1

Finite Mathematical Structures A

Duration: 90 minutes. Page 1 of 14 Q2: /13 Q3: /13 TOTAL: /38. Bonus /1

(a) Explain how physical data dependencies can increase the cost of maintaining an information


XC Total Max Score Grader

Programming Paradigms Written Exam (6 CPs)

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

CSE3322 Programming Languages and Implementation

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

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

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

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

Midterm solutions. n f 3 (n) = 3

Stratford upon Avon School Mathematics Homework Booklet

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

Transcription:

NAME (PRINT): STUDENT #: Last/Surname First /Given Name SIGNATURE: UNIVERSITY OF TORONTO MISSISSAUGA DECEMBER 2013 FINAL EXAMINATION CSC324H5F Principles of Programming Languages Anthony Bonner Duration - 2 hours Aids: Open book (Textbook) The University of Toronto Mississauga and you, as a student, share a commitment to academic integrity. You are reminded that you may be charged with an academic offence for possessing any unauthorized aids during the writing of an exam. Clear, sealable, plastic bags have been provided for all electronic devices with storage, including but not limited to: cell phones, tablets, laptops, calculators, and MP3 players. Please turn off all devices, seal them in the bag provided, and place the bag under your desk for the duration of the examination. You will not be able to touch the bag or its contents until the exam is over. If, during an exam, any of these items are found on your person or in the area of your desk other than in the clear, sealable, plastic bag; you may be charged with an academic offence. A typical penalty for an academic offence may cause you to fail the course. Please note, you CANNOT petition to re-write an examination once the exam has begun. Write your answers on the examination sheet in the spaces provided. You may use the backs of pages if necessary. Concise, well-written answers will receive more points than long, rambling ones. Unless stated otherwise, all answers should be justified. If you do not know the answer to a question, and you write I don't know, you will receive 20% of the marks of that question. If you just leave a question blank with no such statement, you get 0 marks for that question.

This page is for marking purposes only Question Value Score 1 28 2 38 3 17 4 15 5 26 6 25 Total: 149

Name: Student No.: CSC324H5S page 2 of 20 1. Short Answers: 28 marks total. (a) (4 marks) A programmer wishes to write some facts in Prolog stating that all birds can fly, except for ostriches and penguins. To do this, he writes the following: notflies(bird) :- ostrich(bird). notflies(bird) :- penguin(bird). flies(otherbirds). Explain why this doesn t do what is wanted, and provide Prolog code that corrects the problem. (b) (2 marks) What is the di erence between the following two Prolog rules? p(u) :- q(v,u), r(v). p(u) :- q(_,u), r(_). (c) (4 marks) For each of the following pairs, state whether or not the atomic formulas unify. In each case, state what the most general unifier is, or show that unification is impossible. Here, U, V, W, X, Y, and Z are variables, and a is a constant symbol. continued on page 3

Name: Student No.: CSC324H5S page 3 of 20 i. p(v,g(f(x), V)) and p(h(u), g(w, W )) ii. q(y,f(y ),h(a)) and q(g(z),x,z). (d) (4 marks) Consider the following ML function: fun f [] = 0.0 f ((X,Y)::L) = X*Y + f(l); i. In plain English, what does this function do? ii. What is the type of this function? continued on page 4

Name: Student No.: CSC324H5S page 4 of 20 (e) (6 marks) Consider the following Scheme function: (define (f u v w) (lambda (x) (u v (u w x)))) i. What is the value of the following expression: ((f list 47 13) 26) ii. Redefine this function in ML. iii. What is the type of this ML function? (f) (8 marks) Suppose that the predicate flight(n,c1,c2) means that flight N goes from city C1 to city C2. Using only this predicate, write formulas in first-order predicate logic that express the ideas below. You may use non-horn formulas and constant symbols, but do not use any function symbols. i. There is a flight from Boston to Chicago. continued on page 5

Name: Student No.: CSC324H5S page 5 of 20 ii. There are no flights into Bloomington iii. AC309 is the one and only flight into Barrie. iv. AC517 and WJ2234 are the only flights out of Moosejaw continued on page 6

Name: Student No.: CSC324H5S page 6 of 20 2. ML: 38 marks total. In this question, your ML definitions should use pattern matching whenever possible. Simple functions are preferred to complex ones. You may define helper functions. We define a blue-green tree as follows: each node is coloured either blue or green; a blue node has at most two children, while a green node has at most three children; a blue node stores an integer, while a green node stores references to two real numbers. (a) (4 marks) Define an ML datatype called bgtree for blue-green trees. continued on page 7

Name: Student No.: CSC324H5S page 7 of 20 (b) (4 marks) Draw a picture of a blue-green tree, and show how it is represented in your ML datatype. Choose a tree that has at least one of each of the following: a blue node with two children, a green node with three children, a blue node with no children, a green node with one child, and a green node with no children. continued on page 8

Name: Student No.: CSC324H5S page 8 of 20 (c) (15 marks) Write an ML function bluemax(t) of type bgtree -> int that returns the maximum of all the integers stored in the blue nodes of a blue-green tree, T. Inthis question, the blue nodes are assumed to store store only non-negative numbers; so, raise an exception if T contains any blue nodes with negative numbers. If T contains no blue nodes, return 0. continued on page 9

Name: Student No.: CSC324H5S page 9 of 20 (d) (15 marks) Write an ML function greenadd1(t) of type bgtree -> unit that updates a blue-green tree, T, by adding 1.0 to all the real numbers stored in its green nodes. All updates should be done using assignment statements, not by creating a modified copy of T. continued on page 10

Name: Student No.: CSC324H5S page 10 of 20 3. Proving Properties of Programs: 17 marks total. Consider the following two Scheme functions: (define (f X) (if (null? X) X (cons (* 2 (car X)) (f (cdr X))))) (define (g Y Z) (if (null? Y) Z (cons (car Y) (g (cdr Y) Z)))) (a) (4 marks) Write down the basic properties of these functions (i.e., those properties needed to answer part (b) of this question). continued on page 11

Name: Student No.: CSC324H5S page 11 of 20 (b) (13 points) Use induction to prove that these functions have the following property: (f (g X Y)) = (g (f X) (f Y)) continued on page 12

Name: Student No.: CSC324H5S page 12 of 20 4. Scheme: 15 marks. Suppose P and Q are unary functions. Define a Scheme function (replace P Q E) that replaces z by (Q z) for every symbol or number z in s-expression E for which (P z) is true. For example, (replace number? square (2 a 3 b)) => (4 a 9 b) (replace number? square (2 (a (3 b)))) => (4 (a (9 b))) (replace number? even? (a (b 3) 4)) => (a (b #f) #t) (replace even? add1 ((1 2) (3 4))) => ((1 3) (3 5)) (replace even? add1 8) => 9 (replace symbol? list (a 1 (b 3) c 6)) => ((a) 1 ((b) 3) (c) 6) continued on page 13

Name: Student No.: CSC324H5S page 13 of 20 5. Prolog Databases: 26 marks total. Suppose you are given a Prolog database of Toronto banks, bank accounts, and people. The database consists of ground atomic formulas made from the following predicates: bank(bank_name,branch,manager,street,number) account(bank_name,branch,number,balance,person) works(person,bank_name,branch,salary) lives(person,street,number) where the attributes are as follows: bank name is the name of a bank, eg, cibc, td, scotiabank, royalbank, etc. branch is a constant identifying a branch of a bank, usually by describing its rough location within Toronto, eg, downtown, westside, harbourfront, yonge-and-bloor, bloor-west-village, etc. manager is the name of the manager of a particular bank branch. person is the name of a person. Here are typical atomic formulas for each of the four predicates: bank(cibc,westside,scrooge,danforth,1313) account(cibc,westside,456,725000,yeltsin) works(krachit,cibc,westside,25000) lives(krachit,steeles,5765) The first formula means that the westside branch of CIBC is managed by Scrooge, and is located at 1313 Danforth Ave. The second formula means that at the westside branch of CIBC, account number 456 has a balance of $725,000, and is held by Yeltsin. The third formula means that Krachit works at the westside branch of CIBC, and earns a salary of $25,000 a year. The fourth formula means that Krachit lives at 5765 Steeles Ave. With this in mind, answer the questions below. Your Prolog code should follow good logicprogramming style, as required in the homework assignments. continued on page 14

Name: Student No.: CSC324H5S page 14 of 20 (a) Database Queries: 12 marks total Without adding any rules to the Prolog database, pose queries to Prolog that retrieve the the information below (and no more). If necessary, you may use inequality predicates; e.g., x < yis true i x is less than y. Recall that values bound to the underscore variable ( ) are not printed. i. (2 marks) The names of all people working for CIBC. ii. (3 marks) The name, home address and account balance of everyone with an account balance of over $100,000. iii. (3 marks) The name and home address of all CIBC bank managers. iv. (4 marks) The employee name, home address, bank branch and bank address of all CIBC employees. continued on page 15

Name: Student No.: CSC324H5S page 15 of 20 (b) Deductive Databases: 14 marks total Add rules to the Prolog database that define the predicates below. i. (4 marks) empman(e,m), which means that E is the name of an employee and M is the name of his manager (i.e., the manager of the bank branch where the employee works). ii. (4 marks) streetperson(p,s), which means that P is the name of a person who has an account at a bank branch located on street S. iii. (6 marks) twoaccounts(name,street,number): the name and address of a person who has at least two di erent bank accounts at the same bank branch. continued on page 16

Name: Student No.: CSC324H5S page 16 of 20 6. Logic Programming: 25 marks total. We shall use function terms and lists to represent trees. In these trees, each internal node has a list of children, and each leaf node stores a number. Specifically, the function term intnode(l) represents an internal node, where L is a non-empty list of children. Likewise, the function term leafnode(x) represents a leaf node, where X is a number. In the questions below, your Prolog code should follow good logic-programming style, as required in the homework assignments. (a) (3 marks) Using this representation, how is the tree below represented as a function term? 3 2 7 5 continued on page 17

Name: Student No.: CSC324H5S page 17 of 20 (b) (4 marks) Define a Prolog predicate leftmost(t,n) that is true i N is the number stored in the left-most leaf of tree T. Thus,ifT represents the tree drawn in part (a) above, then N=3. (c) (8 marks) Define a Prolog predicate rightmost(t,n) that is true i N is the number stored in the right-most leaf of tree T. Thus,ifT represents the tree drawn in part (a) above, then N=5. For full marks, your solution should use mutual recursion. continued on page 18

Name: Student No.: CSC324H5S page 18 of 20 (d) (10 marks) Define a Prolog predicate squarepos(t1,t2) that is true i tree T2 is identical to tree T1 except that every positive number in T1 has been squared (multiplied by itself). All other numbers must remain the same. For full marks, your solution should use mutual recursion. You may use the inequality predicate, X>Y. continued on page 19

Name: Student No.: CSC324H5S page 19 of 20 Use this page for rough work. continued on page 20

Name: Student No.: CSC324H5S page 20 of 20 Use this page for rough work Total marks = 149 END OF EXAM