



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Terrell; Class: Programming Language Concepts; Subject: COMPUTER SCIENCE; University: University of North Carolina - Chapel Hill; Term: Spring 2008;
Typology: Exams
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Pledge: I have neither given nor received unauthorized aid on this exam. Signature: __________________________ Name: __________________________ Begin: 3:30pm End: 5:00pm (Total: 100 points + 10 points bonus)
1. (10 points) Recall that structured flow control is almost always accomplished with blocks (e.g. indentation in Python), whereas unstructured flow control uses goto statements. At the lowest level, a machine uses unstructured flow control. Convert the following Python code to an unstructured representation (i.e. one that does not use blocks). Assume that Python uses short-circuited conditionals. You may use labels for your goto statements, or you may simply draw arrows. For conditional goto 's, include the condition in the same line, e.g. goto Label if a == b. if x > y && x != 0: print "foo" else: **print "bar" print "\n"
5. (15 points) C supports a do/while loop , which looks like this: do {
(8) How many dead-ends will Prolog reach before finding an answer to the query f (X).? Show your work for partial credit. (Use the back of the page if you run out of room.) (7) Now, give a different definition for the f(X) rule that (1) gives the same answers, and (2) reaches no dead-ends, given the above ordering of facts.
7. (10 points) In the Prolog tic-tac-toe game that we saw in class, we have the following definitions: same(X,X). different(X,Y) :- + same(X,Y). (Recall that + is the Prolog "not" operator.) Also, x(A) is true if there is a slot A on the board with an X in it. Given a predicate line(A,B,C) that tests whether three numbers form a line on the board, consider this predicate to find a winning move A : win(A) :- x(B), different(B,C), x(C), line(A,B,C). The idea is that, if there's an X in slot B , and slots B and C are different, and there's an X in slot C , and A,B,C forms a line, then A is a winning move for player X. Unfortunately, when testing this predicate, we find that it never returns a winning move, even when it should. (5) What is wrong with this definition? (5) Redefine the win(A) predicate to fix this problem.