CSCE 4430 EXAM #2 SAMPLE QUESTIONS (SOLUTION) 1. In object-oriented programming languages polymorphism refers to the late binding of a call to one of several different implementations of a method in an inheritance hierarchy. 2. True or False. A virtual method table is used to keep track of all methods declared in a C++ class and those methods declared virtual in a Java class. False. All methods in Java classes are virtual by default while C++ methods must be declared as virtual. in both languages, a virtual method table is used in each class to keep track of the code for its virtual methods. 3. Consider the following λ-expressions: • 1 = λx . λy . (x y) • 2 = λx . λy . x (x y) • 3 = λx . λy . x (x (x y)) • succ = λn . λx . λy . x ((n x) y) • add = λm . λn . m succ n Show that add 1 2 → 3, including all steps in the computation. add 1 2 = (\m.\n.m succ n) 1 2 => (\n.1 succ n) 2 => 1 succ 2 = (\x.\y.x y) succ 2 => (\y.succ y) 2 => succ 2 = (\n.\x.\y.x ((n x) y)) 2 => \x.\y.x ((2 x) y) = \x.\y.x (((\x.\y.x (x y)) x) y) => \x.\y.x ((\y. x (x y)) y) => \x.\y.x (x (x y)) = 3 4. Consider the following Lisp function: (defun update (store id n) (prog (currentenv newbinding newstore) (setq currentenv (cdaddr store))) (setq newbinding (cons (list ’eq ’id (list ’quote id)) (list n))) (setq newstore (list ’lambda (list ’id) (cons ’cond (cons newbinding currentenv)))) (return newstore)))) Note that the (prog (list of local variables) (exp-1) (exp-2) ... (exp-n) (return exp)) construction declares a list of local variables used in the expressions with exp being the value returned. Let S represent the Lisp expression (lambda (id) (cond ((eq id (quote x)) 4) (t (quote undefined)))). (a) What is the internal list representation that Lisp uses for S? (b) What is (cdaddr S)? (((EQ ID ’X) 4) (T ’UNDEFINED)) (c) Trace the execution of (update S ’x 5), showing the values of currentenv, newbinding, and newstore. currentenv = (((EQ ID ’X) 4) (T ’UNDEFINED)) newbinding = ((EQ ID ’X) 5) newstore = (LAMBDA (ID) (COND ((EQ ID ’X) 5) ((EQ ID ’X) 4) (T ’UNDEFINED))) 5. Consider the Prolog program which solves the Towers of Hanoi problem. This problem is to move a stack of blocks from one location to another without ever changing the order in which blocks are placed on top of each other. A third stack may be used but only one block may be moved at a time. hanoi(N) :- move(N, left, center, right). move(0, X, Y, Z). move(N, X, Y, Z) :N > 0, M is N - 1, move(M, X, Z, Y), display([X,Y]), move(M, Z, Y, X). Trace the above program using the query hanoi(3). Show all queries which are generated by the execution and all moves which are printed. You may wish to draw a tree to show this clearly. Using trace in gprolog returns the following: 1 1 Call: hanoi(3) ? 2 2 Call: move(3,left,center,right) 3 3 Call: 3>0 ? 3 3 Exit: 3>0 ? 4 3 Call: _132 is 3-1 ? 4 3 Exit: 2 is 3-1 ? 5 3 Call: move(2,left,right,center) 6 4 Call: 2>0 ? 6 4 Exit: 2>0 ? 7 4 Call: _211 is 2-1 ? 7 4 Exit: 1 is 2-1 ? 8 4 Call: move(1,left,center,right) 9 5 Call: 1>0 ? 9 5 Exit: 1>0 ? 10 5 Call: _290 is 1-1 ? 10 5 Exit: 0 is 1-1 ? 11 5 Call: move(0,left,right,center) 11 5 Exit: move(0,left,right,center) 12 5 Call: display([left,center]) ? ’.’(left,’.’(center,[])) 12 5 Exit: display([left,center]) ? 13 5 Call: move(0,right,center,left) 13 5 Exit: move(0,right,center,left) 8 4 Exit: move(1,left,center,right) 14 4 Call: display([left,right]) ? ’.’(left,’.’(right,[])) 14 4 Exit: display([left,right]) ? 15 4 Call: move(1,center,right,left) 16 5 Call: 1>0 ? 16 5 Exit: 1>0 ? 17 5 Call: _480 is 1-1 ? 17 5 Exit: 0 is 1-1 ? 18 5 Call: move(0,center,left,right) 18 5 Exit: move(0,center,left,right) 19 5 Call: display([center,right]) ? ’.’(center,’.’(right,[])) 19 5 Exit: display([center,right]) ? 20 5 Call: move(0,left,right,center) 20 5 Exit: move(0,left,right,center) 15 4 Exit: move(1,center,right,left) 5 3 Exit: move(2,left,right,center) 21 3 Call: display([left,center]) ? ’.’(left,’.’(center,[])) 21 3 Exit: display([left,center]) ? 22 3 Call: move(2,right,center,left) 23 4 Call: 2>0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 23 4 Exit: 2>0 ? 24 4 Call: _671 is 2-1 ? 24 4 Exit: 1 is 2-1 ? 25 4 Call: move(1,right,left,center) 26 5 Call: 1>0 ? 26 5 Exit: 1>0 ? 27 5 Call: _750 is 1-1 ? 27 5 Exit: 0 is 1-1 ? 28 5 Call: move(0,right,center,left) 28 5 Exit: move(0,right,center,left) 29 5 Call: display([right,left]) ? ’.’(right,’.’(left,[])) 29 5 Exit: display([right,left]) ? 30 5 Call: move(0,center,left,right) 30 5 Exit: move(0,center,left,right) 25 4 Exit: move(1,right,left,center) 31 4 Call: display([right,center]) ? ’.’(right,’.’(center,[])) 31 4 Exit: display([right,center]) ? 32 4 Call: move(1,left,center,right) 33 5 Call: 1>0 ? 33 5 Exit: 1>0 ? 34 5 Call: _940 is 1-1 ? 34 5 Exit: 0 is 1-1 ? 35 5 Call: move(0,left,right,center) 35 5 Exit: move(0,left,right,center) 36 5 Call: display([left,center]) ? ’.’(left,’.’(center,[])) 36 5 Exit: display([left,center]) ? 37 5 Call: move(0,right,center,left) 37 5 Exit: move(0,right,center,left) 32 4 Exit: move(1,left,center,right) 22 3 Exit: move(2,right,center,left) 2 2 Exit: move(3,left,center,right) 1 1 Exit: hanoi(3) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
© Copyright 2024