Programming Paradigms Written Exam (6 CPs)

Similar documents
Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam

Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam (6 CPs)

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

YOUR NAME PLEASE: *** SOLUTIONS ***

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

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

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

Functional Programming Mid-term exam Tuesday 3/10/2017

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

Csci 102: Sample Exam

CSE341 Spring 2016, Final Examination June 6, 2016

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Programming Paradigms

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

Database Management Systems Written Exam

CS3360 Design and Implementation of Programming Languages Final Exam May 16, pm to 12:45pm (2 hour and 40 minutes) NAME:

Shell CSCE 314 TAMU. Haskell Functions

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

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

Exercise 1 ( = 22 points)

1.2 Adding Integers. Contents: Numbers on the Number Lines Adding Signed Numbers on the Number Line

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

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

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

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

CSE413 Midterm. Question Max Points Total 100

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

ECE264 Spring 2013 Final Exam, April 30, 2013

CSE Programming Languages Final exam - Spring Answer Key

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

CIS Introduction to Computer Programming. 5 October 2012 Midterm

PLT Fall Shoo. Language Reference Manual

Csci 102: Sample Exam

In addition to the correct answer, you MUST show all your work in order to receive full credit.

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

Lecture 8: Summary of Haskell course + Type Level Programming

York University AK/ITEC OBJECT-BASED PROGRAMMING. Midterm Test Sample. Examiner: S.Y. Chen Duration: One Hour and Fifteen Minutes

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CMSC330 Fall 2017 Final Exam

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

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

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

Structure and Interpretation of Computer Programs

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

QUIZ: What value is stored in a after this

CMSC330 Fall 2015 Midterm #1 12:30pm/2:00pm/5:00pm

The Haskell HOP: Higher-order Programming

Decision Making -Branching. Class Incharge: S. Sasirekha

CSE 130, Fall 2005: Final Examination

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE 130, Fall 2006: Final Examination

Database Management Systems Written Examination

CSE341, Fall 2011, Midterm Examination October 31, 2011

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

You have 90 minutes to complete the exam of InformatikIIb. The following rules apply:

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

CS 1301 Exam 1 Fall 2010

A Constant Rate of Change Name Part 1

ETHZ D-INFK Final Exam Booklet

REPETITION CONTROL STRUCTURE LOGO

CSE 1223: Exam II Autumn 2016

CS4215 Programming Language Implementation

CS115 - Module 9 - filter, map, and friends

CSE wi: Practice Midterm

Comp Intermediate Programming EXAM #1 February 16, 2004 Rice University - Instructors: Cox & Nguyen

CSCI Compiler Design

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm

Lecture 19: Functions, Types and Data Structures in Haskell

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CMSC 330, Fall 2018 Midterm 1

CSE341 Spring 2017, Final Examination June 8, 2017

Computer Science E-22 Practice Final Exam

First Haskell Exercises

Structure and Interpretation of Computer Programs

Computer Programming. Decision Making (2) Loops

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

An introduction introduction to functional functional programming programming using usin Haskell

CSCI Compiler Design

CSCE 314 Programming Languages

CSE341 Spring 2016, Midterm Examination April 29, 2016

CS 320 Midterm Exam. Fall 2018

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Shell CSCE 314 TAMU. Higher Order Functions

CS100J February 13, 2001

CMSC 330, Fall 2018 Midterm 1

CS32 Final Exam. E03, F15, Phill Conrad, UC Santa Barbara

Answer the following questions on the answer sheet that is provided. The computer must be switched off while you are busy with Section A.

Introduction to C Language (M3-R )

Transcription:

Programming Paradigms Written Exam (6 CPs) 31.01.2018 First name Student number Last name Signature Instructions for Students Write your name and student number on the exam sheet and on every solution sheet you hand in and also sign them. This is a closed book exam: the only resources allowed are blank paper and pens (do not use pencils). Write neatly and clearly. The clarity of your explanations will affect your grade. The duration of the exam is 2 hours. Good luck! Do not write in this space Exercise Marks Achieved 1 20 2 12 3 8 4 10 5 16 6 14 7 10 8 10 Total 100 1

Exercise 1 (20 marks) a. (4 marks) Briefly describe the main differences, advantages and disadvantages of static typing and dynamic typing. b. (4 marks) Briefly describe the concept of mixins in Ruby. c. (4 marks) What is the following Prolog program doing? foo :- repeat, read(x), write(x), nl, X = quit,!. d. (4 marks) What is the following Haskell list comprehension producing? [(4-x,y) (x,y) <- [(1,2), (2,3), (3,1)]] e. (4 marks) The following figure shows a hierarchy of linked Erlang processes, also called supervision tree. What happens if process I crashes? A B C D supervisor E F G H worker I K 2

Exercise 2 (12 marks) Write a Ruby function that implements counting sort for sorting an array of numbers, which works as follows: Given an array A of positive integers n 1,..., n k [0, k], create an array A of size k + 1. Each element of A is associated with an index in A. The algorithm counts then the number of occurrences of each element in A and stores it in the corresponding cell in A. Finally, with a scan of A the elements can be retrieved in ascing (or descing) order. For example, for the input array A = [6,5,1,7,8,1,2] we obtain A = [0,2,1,0,0,1,1,1,1]. The element A [0] represents that the number 0 occurs zero times, A [1] represents that the number 1 occurs two times, etc. Scanning A allows to retrieve the sorted array [1,1,2,5,6,7,8]. Exercise 3 (8 marks) The following Ruby function computes log 2 n recursively: def log( n ) return 0 if n == 1 if n.even? n = n / 2 return log( n ) + 1 else n += 1 return log( n ) Rewrite this function into a tail-recursive function. Exercise 4 (10 marks) Write a Prolog program pythagoras(a,b,c) using the generate and test pattern, which for a given value C computes all possible integer values of A and B for which the theorem of Pythagoras holds, i.e., A 2 + B 2 = C 2. For instance, pythagoras(a,b,5) returns A=3, B=4 and A=4, B=3, whereas pythagoras(a,b,6) fails. Hint: You can use the predicate between(l,u,x), which generates all integers between L and U, e.g., between(0,2,x) generates X = 0 ; X = 1 ; X = 2. 3

Exercise 5 (16 marks) Write a program in Prolog that goes through a list of numbers and selects numbers (starting from the beginning of the list) whose sum is smaller than a given capacity. So as long as there is still enough capacity left, the program keeps selecting numbers (skipping numbers that are too large). The program should return the result in a list. For example, given the list [2,5,3,8,1,12] and the capacity 14, the program should return the list [2,5,3,1] as this sums up to 11, which is less than 14 (when 8 and 12 are reached, they are too large to be included). The order of the items in the result does not matter. Exercise 6 (14 marks) Write a function isbalanced in Haskell that checks whether a string containing open and closed parentheses is balanced. A string of parentheses is balanced when every open one has a corresponding closed one and at any point there are not more closed ones than open ones. For example, the strings "", "(())" and "(())()" are balanced, whereas the strings "()(()" and "())" are not balanced. Exercise 7 (10 marks) Look at the following recursive Haskell program: mystery :: [a] -> Integer mystery [] = 1 mystery (h:t) = 2 * (mystery t) a. (4 marks) Briefly describe what the program does. b. (6 marks) Transform the program into a tail-recursive one. Exercise 8 (10 marks) Write a function loop for an Erlang process that receives messages consisting of a single parameter and does the following: if the parameter is a number, it outputs to the console whether the number is positive, negative, or zero; if the parameter is bye, the process terminates; otherwise, an error message is printed, e.g., Unexpected message. Show also how to start the process. (Hint: you can use a function is number(n), which is true if N is a number, and false otherwise) 4

Solution 1 a. Static typing: types and their constraints are checked before executing the program pro: less error-prone con: sometimes too restrictive Dynamic typing: type checking is done during program execution pro: more flexible con: harder to debug b. A mixin in Ruby is a combination of modules and classes. More specifically, a module can be included in a class definition. By doing so, all methods of the module are added and available to the class. Mixins have some similarity to the concept of multiple inheritance. c. This is a simple echo program. It reads from the standard input and shows the input on the standard output. When the user input is quit, the program terminates. d. [(3,2), (2,3), (1,1)] e. This causes process G to terminate, which in turn terminates process K. Process D traps the exit signal and restarts processes G, I, and K. Solution 2 def counting_sort(myarray) newarray = Array.new(myarray.max+1,0) finalarray = Array.new myarray.each do x newarray[x] = newarray[x] + 1 for i in 0...(newarray.length) newarray[i].times do finalarray.push(i) return finalarray 5

Solution 3 def log_tail_recursive( n ) return log_tr( n, 0 ) def log_tr( n, r ) return r if n == 1 if n.even? n = n / 2 return log_tr( n, r+1 ) else n += 1 return log_tr( n, r ) Solution 4 pythagoras( A, B, C ) :- between( 1, C, A ), between( 1, C, B ), X is C * C, Y is A * A + B * B, X = Y. Solution 5 Solution with accumulator fit( L, C, R ) :- fit_acc( L, C, [], R ). fit_acc( [], C, L, L ). fit_acc( [H T], C, L, R ) :- H > C, fit( T, C, L, R ). fit_acc( [H T], C, L, R ) :- H <= C, N is C - H, fit_acc( T, N, [H L], R ). Solution without accumulator 6

fit( [], _, [] ). fit( [H T], C, R ) :- H > C, fit( T, C, R ). fit( [H T], C, [H R] ) :- H =< C, C1 is C - H, fit( T, C1, R ). Solution 6 module IsBalanced (isbalanced) where isbalanced :: [Char] -> Bool isbalanced s = isbalan s 0 isbalan :: [Char] -> Int -> Bool isbalan [] 0 = True isbalan [] nonzero = False isbalan (h:t) x = if x < 0 then False else if h == ( then isbalan t (x+1) else isbalan t (x-1) Solution 7 a. The program computes the exponential function 2 l, where l is the length of the array that is passed as parameter. b. Tail-recursive version: pass the length of the array seen so far as a second parameter; when the list is empty, the second parameter contains the length of the list. Call the function as expfun [...] 1 expfun :: [a] -> Integer -> Integer expfun [] x = x expfun (h:t) x = expfun t (x * 2) 7

Solution 8 -module(sign). -export([loop/0]). loop() -> receive N when is_number(n) -> if N < 0 -> io:format("number is negative~n"); N > 0 -> io:format("number is positive~n"); N == 0 -> io:format("number is zero~n"), loop(); "bye" -> io:format("bye~n"); _ -> io:format("unexpected message~n"), loop(). P = spawn( fun sign:loop/0 ). 8