Download as pdf
Download as pdf
You are on page 1of 40
‘As defined in the course, whgtis 2 paradigin? (© “an approsch for programming a computer based on a coherent set of principles or a mathematical theory" v © “an approach for programming a computer based on a coherent set of © “an approach for programming a computer based on a mathematical theory only O “away to see the world” We use the Oz language mainly because: © Oz can help to understand practical languages, such as C++ or Java © zis designed to support many paradigms v CO Oz isa research language © The syntax is easy to learn Explanation {As this course is centred cn learning concepts and paradigms, itis appropriate ta choose a “multiparadig language, Our choice is Oz, 91234567898 Consider the following lines of code: declare xed io3 {Browse x) x(@) Lm {8rouse 2) declare yous {Browse Y} ‘% (b) declare {Browse x) Note: Information following "%" is not taken into account (this symbol signals that the rest of the line is 2 ‘comment). After feeding the first ragion, which value is printed for x? Bb Answer: 42 ‘The unary minus sign is denoted by the ~ (tide) character in Oz Which value is printed for 2? Answer: ~2 ‘Consider now the secand ragion, which value of Y is printed after feeaing? ‘Answer: 47 Now, proceed to the feading of the last region, which value is printed for X? ‘Answer: 1234567690 = —FPlease feed the line (a), whats printed? ‘Answer: 1234567890 ‘And finally, feed the line (b), which printed value do you observe? Answer: 47 Which statement is true? “Logie programming and funetional programming are both a kind of declarative programming” v © "Declarative programming and logic programming are both a kind of functional programming.” © "Declarative programming and functional programming are both a kind of logic programming. Complete this sentence: “Functional programs do not have any intern; ry, they are ‘Answer: stateless Ww ional paradigm is the most used one. We frst study the functional paradigm because: O The func (© B2sed on tis parecigm, we will ada concepts to reach other parecigms, U~ v © This paracigm is the best one. CConsier these four tines of code: {rowse 3) What happens? “Acompiltion erie raised ¥ U O Bis printed O 1s printed Explanation As the variable identified by the identifier X already has a value atthe second tine, you cannot change the value ofthe Variable atthe tried ln. Consider the following lines of code: local Xe1 in Jocal Xe2 in local X-3 in {Browse X} % (2) end Uu— end {Browse X} % (2) {a fend Scope test 2 points possible (eraded) ‘What is printed by the first call to Browse? ty Answer: 3 ‘What is printed by the second call to Browse? ‘Answer: 1 Why is Oz syntax so different from C-llke syntax? © Because itis shorter than most other languages Because O2 supports many paradigms that do not inte v cin her —_— O Because it is easier to read © Because C-like syntax is bad Which of the fotowing denotes an denier? (_-—~ Ux Ox v Ci wyarise 2 CD myldentifier CO Browse v D AveryveryLongidentifierButisitReallyanidentifierBecauseltisVeryLong v 0 trons Explanation The brackets indicates that this is not an identifier but a procedure or function call, The identifier is Int2Float without the brackets, Which characteris used to deppie arecord? (Try to think what the others are used fo, as an exercise) Pe ¥ Oo wo ‘Check the true statement(s): C Arecursive function is a function that calls itself. v D Recursive procedures exist. v C Arecursive function will never loop infinitely For the two following defini 3s, find the corresponding defi ‘A mathematical formula that is true at each recursive call: Answer: invariant ‘A mathematical formula that defines what the function calculates: Answer: specification “While loops" are strictly equivalent to "recursive functions" Ll: O Tue O False v Explanation ‘The recursive function has to be talluscussive, in order to be equivalent to wh ened comes from the fact that: The name "commupisating vase: O We can empty a variable, just ike vases. We can deceaseavedabl adn the same tieincrease anaes ke pouring water rom one vase to arfotfer. v © We can fill variables, just like vases. The two presented factorial functions consur © Asifferent amount of memory, the one using the communicating vases technic consumes more. © A.different amount of memory, the simpler one consumes more, v © The same amount of memory. Ff Sometimes, while using a taitrecursive function, a recursive call has to come back in a previous call to execute something (i.e. a mutipfication). Se O Tue O False v Explanation This is the main advantage of tail recursive function, the recursive calls executed in the last place! function has ngyaeto come back. — Ife pregram sing assaults, we aways us on} on@of them ger program x O Tne Explanation Some programs can be specified such that mare than one accumulator are used, How many? ‘point possibie (graded) Use Mozart on your computer to write the intuitive definition of Fibonacci Now, think about a new way to write Fibgnacc! using agcumulatots). How many accumulators do you use with this new way? ‘Compute {Fib 32} with your naive technique. What is the result? O 2178309 v © 2178310 O 27830 O 2178312 Keep in mind the time taken to compute this result with the naive technique. To observe the power of accumulators, use your efficient function to compute {Fib 42}. ‘What isthe result? © 267914295 © 267914296 v © 267914297 © 267914298 When will you have the answer of the naive {Fib 1000}? O Directly (© ina ong time v O Never Lists can be changed. |_—- O Tne O False v Explanation Usts ae values, they ae gonstants! (0) (J {3tlis: O Nota list Onecorie GC O Alist of integers Explanation Note that each element of the list isa list. Consider the following list: [[1] 2 O Thisis artist v a O This is not a list because the elements are not of the same type © This is not a list because it contains alist O This is not a list because there are not only integers. Explanation Lists can contain Js!s and elements of different type. Which word represents the empty list? (One word) Answer: il Explication ‘The empty list is represented by nil. The circulas definition of ist (definition of lists using ists) sa problem. Nn O True O False v Explanation This is not @ problem because lists are defined in terms of smaller lists and at one time, the lst wil not be reducible anymore. Which representations are equivalent to [ty {211? Note that here, as wellas elsewhere in Oz, you ae allowed to use parentheses to change the meaning of an expression. The expression 12/3} is equivalent to 1|(2|(3[ri), since the vertical bar associates tothe right, {as implied by the grammar rule defining lists). Ths isnot the same as ((2)|3)nl, whichis @ list of one element, namely (1/2), whichis tse nota ist (since it terminates with 3 instead of ni). C1 12hnit ara] (2 it O of nl(2laiiinn C1 talrin c2lniny v © taalnin|c2)niny O mien CG (alr en v Is ita list? + point possilé (grades) Explanation Even if (a|b) is not alist, (alb)c|ni i alist with two elements: (alb) and c. We can write tail recursive function with lists. a eoV7_ —| O The ¥ O False Consider the following code: proc {TestPattern L} case | of M then {Browse 1} [1 HIT then {Browse 2} [] nil then (Browse 3} —-—~ end end What is printed if we call (TestPattern nid? O1 v Os pe And if we call (TestPattern (a b]}? Os Explanation ‘They are directly bound to M because the pattern matches, = Consider the patter IT The pattern matches the list albl¢|nil. ‘What is bound to}? And to 7? Oo alble and T = ni O H=albandT = ¢| O Hs aandT = nil Explanation His the head (a) and T Is the tall (the rest) of the list (beni). Select the correct statement: © Aprocedure can be translated to a function with one additional argument, © There is no difference between functions and procedures. © Funetions return nothing. © You cannot give arguments to a procedure. © A function can be translated to procedure with one aditional argument. v ——————eerrooewreeeeee| Tail-recursive Append is possible because of... (two words, last plural), ‘Answer: Unbound variables Consider the following code: local A B C=1 D=2 in SO Al proc {Add E F G} 1 SS E=A+D+F end end Consider the procedure definition, —EoTve Which of these identifiers (A, B, C, D,E,F, G) are free identifiers? Oa v Oe Oc Oo v Oe OF Os Explanation ‘The free identifiers of any instruction are those indentifier occurrences inside the instruction that correspond to declarations outside the instruction. — Consider the following code: local 9=2 in Ade = eS ts a proc [Add BC E E=BHC end end =P Consider that the identifier Add refers to a variable a in memory. — What is the value of a? (orec {$B CE} E= B+ C + Dend, (Add + a, Bb, Cc, D3d, Ee!) a= (proc ($B CENE = B+ C + Dend, (020) v a= (proc [Add BC EJE= B+ C + Deend, (Add > 2, D->a) 2 = (proc {Add B C ELE = 8 + C + D end, {8->b, C-r¢, Dd, Ee) O a= (proc (Add BC EJE=B+C + Dend, (D>) a= (proc (SBCE)E=B+C + Dend, (B40, Cc, 044, E2}) D a= (proc {$B CE} E = B + C+ D end, {Add > 3, D->d)) O a= (proc (Add BC EJE = B+C + Deend, (Add > 2, B90, Cc, EH) a= (proc {Add BC ELE=B + C + Dend, {Add > 2, Bb, Cc, Dod, EH) O a= (proc {$B CEIE = B+ C + Dend, (8-90, 0-96, E>e}) O as (proc ($ BCE} E=B+C +Dend, (Add > a, 8-5b,C-¢, E>2)) a= (proc {Add BC ELE=B + C + Dend, (8-6, Cc, Ee) Explanation 1) "proc {Add 8 CED... end” is 3 syntatic sugar to represent “Add = proc {8 8 C E)", Therefore, the procedure code of the procedure value does not contain the identifier Add. 2) "Add + a is not contained in the contextual environment of a. 43) Only Dis a free indentifier. The procedure value only contains (Dah in tg contextual environment part. ly proc iy {Da} part What is printed if| write: (Browse Browse}? O browse © Anerroris printed ) ‘The procedure value refered by Browse v O Nothing because of an infinite loop O Browse Consider the following code: declare fun {M Int} fun {$} Int#{M Int-1} end end What is printed with: {Browse MP “ry tw answer by youself and then use Mozart to see ifthe resutis the one you expected, Answer: ⁢P/2 M> or

Explanation We have browsed M, a procedure of 2 arguments. ‘What is printed with: Test = M5} {Browse (Test)) Answer: S#8it;P/Taat; or Si#

Explanation The function Test returns a tuple containing two elements: § and the function returned by the call {M 4). What s printed with: (Browse (M SI} Answer: ⁢P/18at; or

Explanation Mretums a function without argument, which is a procedure with one argument. Consider the following record: Menus = menu_of_this_course( menu(entree: ‘salad’ meal: ‘steak’ cost: 10) menu(entree: ‘fruits’ meal: ‘salmon’ cost: 12) menu(mes cost: 9)) : hamburger! ‘What is the meal of the second menu? Answer: salmon ‘What is the type of this value (the meal of the second menu}? Answer: atom Consider that X persons take the first menu, Y persons take the second menu and Z persons take the third ‘menu. Use each cost value in the formula, not the way to accessit in the record. What formula represents the total cost? Answer: 10*K+12°Y+9*Z, How to access these costs in 02? (Use the dot operation) For instance, to access the meal ofthe first menu, the answer would be: Menus.1.meal How to access the cost of the first menu? Answer: Menus..cost How to access the cost of the second menu? Answer: Monus.2.cost How to access the cost of the thitd menu? ‘Answer: Menus.3.cost What is the width of the record identified by menus? Answer: 3 What is the width of the first menu? ‘Answer: 3 What is the width of the third menu? Answer: 2 Consider the two following programs: 1) First program: local Res in local Argi Arg2 in Argl = 7 Arg2 = 6 Res = Arg] * Arg2 end {Browse Res} end 2) Second program: local Res in local Argi Arg2 in Argl = 7 Res = Arg] * Arg2 Arg2 = 6 end {Browse Res} end You will be asked to write what is printed by both programs. I you think there is an infinite loop, write "-1" (without the quotes). I you think the program waits for something, write "-2" (without the quotes) ‘And finally, if you think the program has crashed, write "~3" (without the quotes). What is printed by the first program? Answer: 42 ‘And what is printed by the second program? Answer: -2 ‘You will be asked to give the created variables of each program. ‘Consider that a variable has the same name as its identifier, but beginning with a lowercase. Please write the variablo names in lexical order, separated by only one space. ‘Which variables are created by the first program? Answer: arg! arg? res Which variables are created by the second program? Answer: arg! arg2 res Consider the following tree: T = tree(42 leaf leaf leaf tree(42 leaf leaf leaf tree(42 leaf) leaf) leaf leaf) How man leaves has the tee T? Oo oO. O2 Os O4« Os ose O7 Oe Oe Ow Ou How many leaves has the troe 75.5? Oo On v Os O7 Oe Os Ow On Consider the fllowing grammar rule that defines a representaion of binary trees: = leat |tree(T leftictree T>rights) Which of these trees are binary trees according to the above rule? CO tree(42 leaf leaf leaf) ( tree(a2 lettiea rightieat) ¥ CO tree(42 right:leat leftieaf) v L treet tree(2 leaf) tree(3 leaf leaf leat) treett tree(2 leat leaf) tree(@ leat leaf) (1 treett lefttree(2leftieat rightieat) righttee(3 lefteatrightlea)) v CO tree(t lefttree(2 leftstree(3 leftieat right:leaf) rightsleaf) rightieaf) v DO teat v Consider these three trees: T1= tee(key value lfttrekey:6 value: lftleafrightleaf) Yighttree{key:4 value leftleaf rightieaf)) T2 = tree{key:5 value:6 lefttreetkey:3 value: leftstree(key:2 valued leftieet rightleat) right-tree|key-4 value-10 lef-leaf rightleaf)) rightitreelkey:6 value leftleafrightileaf)) 13 = treolkey:6 value:5lefttreetkey'S value left-tree(key:4 value? lftieaf rght-leat) right:tree(key:10 value: leftleat rightleaf) righttree|key:7 values leftlet rightleaf)) Which of these threes are ordered binary trees? on on v Ors Consider the follo\ \g ordered search tree: T = tree(key:horse value:cheval left:tree(key:dog value:chien left:tree(key:cat value:chat left:leaf right:leaf) right:tree(key:elephant value:elephant left:leaf right:leaf)) right:tree(key:mouse value:souris left:tree(key:monkey value:singe left:leaf right:leaf) right:tree(key:tiger valuestigre left:leaf right:leaf))) ‘What is printed after calling (Browse {Lookup mouse}}? (Do not forget the output format: found(..)) ‘Answer: foundisouris) How many calls to Lookup are done whan calling {Lookup tiger}? (Count the original one too) Answer: 3 How many calls to Lookup are done when calling {Lookup rabbit}? (Count the original one too) Answer: 4 ‘What is the tree T of the last call of Lookup when calling {Lookup rabbit}? Answer: leat Consider the following ordered search tree: T = tree(key:horse value:cheval left:tree(key:dog value:chien left:tree(key:cat value:chat left:leaf right:leaf) right:tree(key:elephant value:elephant left:leaf right:leaf)) right:tree(key:mouse value:souris left:tree(key:monkey value:singe left:leaf rightleaf) right:tree(key:tiger value:tigre lett:leaf right:leaf))) ‘What is printed if we call (Browse {Lookup elephant {Insert elephant mouche T)}}? (Do not forget the output format: found\..) ‘Answer: found(mouche) How many calls to Insert are done while calling {Insert elephant mouche T}? (Count the original call to Insert) Answer: 3 ‘What is printed if we call (Browse {Lookup elephant {Insert fly mouche T)}}? ‘Answer: found(elephant) How many calls to Insert are done while caling {Insert fly mouche T}? (Count the original call to Insert) Answer: 4 Let our ordered tree T be balanced. How many time units (in average) do we have while calling Lookup with 18 nodes in T? Answer: 4 ‘And with 31415 nodes in T? (approximately) Answer: 14.94 Consider the following ordered search tree: T = tree(key:horse value:cheval left:tree(key:dog value:chien left:tree(key:cat value:chat left:leaf right:leaf) right:tree(key:elephant value:elephant left:leaf right:leaf)) right:tree(key:mouse value:souris: left:tree(key:monkey value: right:tree(key:tiger value:tigre left: inge left:leaf right:leaf) af right:leaf))) Let us call T1 the result of (Delete dog T). What will be the key of TLieft? (Write eat if TLeft isa leat) ‘Answer: elephant Let us call 12 the result of (Delete mouse 1}. What willbe the key of T2.ight? (Write leaf if T2.right is a leat) Answer: tiger Let us call 73 the result of (Delete cat 7}. What will be the key of T3.leftleft? (Write eat if T3efteft is a leaf) Answer: leat Let us call T4 the result of (Delete tiger T). What will be the key of Té.right.right? (Write leaf if TAright-rght is a leat) ‘Answer: oat Let us call 75 the result of (Delete monkey {Delete horse T)}. What will be the key of TS? (Write leaf if TS is a leaf.) Answer: mouse ‘What is the key of the node {Delete monkey {Delete horse THt-left? (Write leaf if (Delete monkey (Delete horse T)}left isa leaf) Answer: dog Given the following program: local fun {MakeMakeFoo X} fun {$ Y} fun {$ Z} X+¥+Z end end end in {Browse {{{MakeMakeFoo 10} 20} 30}} end Answer the following questions: 1. Give the identitier(s) in the contextual environment of MakeMakeFoo: CD MakeMakeFoo Ox Qy Oz None v 2, Give the identifiers) in the contextual environment of the function that MakeMakeFoo returns when called: (CJ MakeMakeFoo Ox v Qy Oz U. None 3, Give the identifiers) in the contextual environment of the remaining function: CO MakeMakeFoo Qx v Oy v Qz CO None. Given the following prograi local A BC in A=1 B=2 C=3 local C DE in C=B D=5 E=A local AC E in A=B C=D E=9 {Browse A+B+C} end {Browse A+B+C} end {Browse A+B+C} end 1. What is displayed by the first Browse? 2S «eo Ow on Or Orn D 4 18 2, What is displayed by the second Browse? 2% Oa Os v Os O7 Oe Os Ow On On Ow Ow Ow 3. What is displayed by the third Browse? Oa Os Os ¥ O7 Os Oe Ow On Or On On Ow Which of the following is NOT a list: O ap v © alb|nit D nillbla v elev OD alcolnn CO (nila v O nilleolay v declare fun {Snake FN X} if then {F X} else {Snake fun {$ X} {Snake F N-1 X} end N-1 X} end end {Browse {Snake fun {$ X} X+1 end 2 2}} 1. What does it display? © Iewil terminate eventually, but the execution is too complex to reason about O Nothing (infinite toop). 2, Given the following call of the function Snake. {Browse {Snake fun {$ X) XI end 10 5)) ‘What does it display? O1 os Os v on On O 50 © 100000 © tewil terminate eventually, but the execution is too complex to reason about. O Nothing (intnte loop. declare Tetree(key:A left:tree(key:B left:leaf right: leaf) right:tree(key:C left:leaf right: leaf)) If Tis an ordered binary tree, which of the following conditions are true: Olea Oces v Gace Cl A>Bandarc (J c>Bandc>a v (] B>AandB>c The concept that alow the representation of the world in a program to change i... (two words, singular) Explicit state v ‘word, singular) What do you need to add to functional programming to get deterministic dataflow ? (not the concept, one Thread v ‘What concept do you need to add to deterministic dataflow to get multiagent dataflow ? (one word) Nondeterminism v According to the course, a state is a. (the sentence isin the slides; you do not have to put the final mark) (Be careful if you copy and paste the definition that there is no unwanted characters (spaces, line splits, etc.).) sequence of values calculated progressively, which contains the intermediate results of a computation v How many variables compose this new extension? Oo ‘Which of the following change through time? C1 the identity of this new extension (J) the value of the variable this new extension refers to the reference to the variable contained in this new extension v (Check the true statement(s): C Two cells are the same if they contain the same content ‘Two cells are the same if they are not created separately ‘Two lists are the same if they represent the same value C Two lists are different if they are created separately CO Aceti containing a list L is the same as the list L v ‘Check the true statements): cells are contained in the single-assignment store tho cols nama is Bound toa constant and can novor change the cell's content can be any variable in the single-assignment store Oi the variable being the ets content can change the cells content can be changed v (Check the true statements): In the functional paradigm, what works now will workin the future. inthe imperative paradigm, what works now will work in the future, 0 In the functional paradigm, a function can be updated without changing the interface. s In the imperative paradigm, a function can be updated without changing the interface. a ‘The imperative paradigm is always more useful than the functional paragigm because it extends this last paradigm, Check the true statements}: The encapsulation increases the danger while using the implementation through the corresponding Interface When using encapsulation, you are restricted to what the corresponding interface allows The complexity of the implementation is hidden, therefore its simpler for the user tthe user does not want to use the encapsulated implementation, the implementation itself always stays accessible v (Check the true statment(s): ii ADTs, values and operations are separated C Inobjects, values and operations are separated Every data abstraction can be implemented as an ADT Every data abstraction can be implemented as an object v ‘Concerning objects and functional objects. Check the true statement(s): Objects are mutables Cy Functional objects are mutables Objects combine values and operations together Functional objects combine values and operations together Objects are popular C Funetional objects are popular v Concerning ADTs and stateful ADTS. Check the true statement(s): 1 apts use celts ‘Stateful ADTS use cells CO ADTs, values and operations are combined 1 Instateful ATS, values and operations are combined ‘Stateful ADTs were often used in C v (Check the true statement(s): object-oriented programming is always the right paradigm C1 C++ provides encapsulation correctly Sg Java provides encapsulation correctly a Inheritance is a main principle of OOP for structuring programs s Polymorphism is a main principle of OOP for structuring programs a Function is a main principle of OOP for structuring programs 4 Consider the following schema: declare local Al ... An in proc {M1 ...} ... end proc {Mim end Does this schema correspond to an object? O Yes O No, because of AT O No, because of An No, because of At, .., and AN © No, because of M1 No, because of Mm No, because of Mt,..., and Mm O No, because of A, .., An, M1, ..., and Mm ‘Check the true statement(s): Let Computer be a class. Computer isa value. In Oz, class definition and object creation are separated. Aclass is a record. CO Actlass is a list. a Aclass isa tree, s | can get the methods of Computer by using Computermethods C Lonly get the first attribute of Computer by using Computer.attrs ‘The New function takes a class and returns an object, which is an instantiation of the input class. S, in the New function (in the previous video), is a record built from a list.

You might also like