Programming Language Concepts - Old Exam 2008 | COMP 524, Exams of Programming Languages

Material Type: Exam; Professor: Terrell; Class: Programming Language Concepts; Subject: COMPUTER SCIENCE; University: University of North Carolina - Chapel Hill; Term: Spring 2008;

Typology: Exams

Pre 2010

Uploaded on 03/16/2009

koofers-user-n50
koofers-user-n50 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP 524:
Programming Language Concepts
Exam 2
Thursday, April 3rd, 2008
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)
pf3
pf4
pf5

Partial preview of the text

Download Programming Language Concepts - Old Exam 2008 | COMP 524 and more Exams Programming Languages in PDF only on Docsity!

COMP 524:

Programming Language Concepts

Exam 2

Thursday, April 3

rd

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"

  1. (10 points) (4)** Python is strongly typed. Explain what this means. (3) Give an example of some Python code that is illegal because of Python's strong type system. (2) Python is dynamically typed. Explain what this means. (1) Does Python use explicit or implicit type declarations?

5. (15 points) C supports a do/while loop , which looks like this: do { } while ( ); Assume the corresponding grammar rule is: do_while := DO block WHILE OPAREN cond CPAREN SEMI Also, assume the parse tree for a do/while loop looks like this: ['do_while', Token('DO'), , Token('WHILE'), Token('OPAREN'), , Token('CPAREN'), Token('SEMI')] ...where and are themselves parse trees. Write Python code to evaluate a do/while parse tree. The semantics of a do/while loop is that the body of the loop is always done at least once, and is repeated for as long as the condition holds. Introduce your code as part of an eval(ptree) function, which takes a parse tree as its only argument. In other words, start your code with elif ptree[0] == .... You may assume that eval() can accept any type of parse tree, evaluating the tree and returning the result (if applicable). 6. (15 points) Given the following Prolog knowledge base: a(i). a(j). a(k). b(m). b(k). c(k). f(X) :- a(X), b(X), c(X).

(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.