+ All Categories
Home > Documents > dProgSprog final exam, August...

dProgSprog final exam, August...

Date post: 30-Apr-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
35
“Born on the wings of time, it seems the answers were so easy to find.” – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID Name Remember to show us an ID when returning the present exam form. We will then mark your name on our master list. This document contains 88 questions (some of which are identical, to give them weight in the overall grade) and is structured in 10 groups of questions. Strategically, you should first scan it quickly in its entirety, and identify the groups of questions that seem easiest to you. You should answer those first and then move on to the others. All the Scheme (resp. Schelog) definitions are in the standard initial environment of Scheme (resp. Schelog). All the Scheme and Schelog programs are syntactically correct (in particular, they all are well parenthesized). All the unit tests are correct – there is no bug in any of them. For each question, there is one single correct answer. If somehow you rip out the front or the back page(s) of your exam copy, there is no need to panic: we have a stapler. Happy working!
Transcript
Page 1: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

“Born on the wings of time,it seems the answers were so easy to find.”

– Supertramp (Fool’s overture)

dProgSprog final exam, August 2015

ID

Name

Remember to show us an ID when returning the present exam form.We will then mark your name on our master list.

This document contains 88 questions (some of which are identical, to give them weight in theoverall grade) and is structured in 10 groups of questions. Strategically, you should first scan itquickly in its entirety, and identify the groups of questions that seem easiest to you. You shouldanswer those first and then move on to the others.

• All the Scheme (resp. Schelog) definitions are in the standard initial environment of Scheme(resp. Schelog).

• All the Scheme and Schelog programs are syntactically correct(in particular, they all are well parenthesized).

• All the unit tests are correct – there is no bug in any of them.

• For each question, there is one single correct answer.

• If somehow you rip out the front or the back page(s) of your exam copy, there is no need topanic: we have a stapler.

Happy working!

Page 2: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Reminders:

• The infix sign “≡” is read as “is defined to be”, as in “N ≡ the set of natural numbers”, “N+ ≡the set of positive natural numbers”, and “Z ≡ the set of integers.”

• Given a function h : N → Z, here are two notations for summing and for multiplying itsresults from a given integer a to a given integer b:

b∑i=a

h(i) ≡ h(a) + h(a+ 1) + h(a+ 2) + . . .+ h(b− 2) + h(b− 1) + h(b)

b∏i=a

h(i) ≡ h(a)× h(a+ 1)× h(a+ 2)× . . .× h(b− 2)× h(b− 1)× h(b)

In particular,

∀a : N,∑a

i=a h(i) = h(a) and∏a

i=a h(i) = h(a)

∀a : N, ∀b : N,∑b

i=a h(i) = 0 and∏b

i=a h(i) = 1 whenever a > b

• ∀a : Z, ∀b : Z, ∀c : Z, a < b < c ≡ a < b and b < ca < b ≤ c ≡ a < b and b ≤ ca ≤ b < c ≡ a ≤ b and b < ca ≤ b ≤ c ≡ a ≤ b and b ≤ c

• ∀a : Z, ∀b : Z, a < b ⇒ ∀c : Z, a+ c < b+ ca ≤ b ⇒ ∀c : Z, a+ c ≤ b+ c

• The predefined procedure random, in Petite Chez Scheme, when applied to a positive integer,returns a random integer that is strictly smaller than this integer and greater or equal to 0:

> (random 5)

4

> (random 5)

1

> (random 5)

2

> (random 5)

1

> (random 5)

2

> (random 5)

0

> (random 0)

Exception in random: invalid argument 0

Type (debug) to enter the debugger.

>

• For any given natural number n, the notation “random(n + 1)” stands for a random numberbetween 0 and n, and the notation “random(0)” is undefined.

Page 3: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

– 4 questions about a proof by induction –Given two functions f : N+ → Z and g : N+ → Z, Sigbjørn the Fearless wants to prove whetherthe following equality holds:

∀n : N,n∏i=1

(f(i)× g(i)) =

(n∏i=1

f(i)

(n∏i=1

g(i)

)To this end, Sigbjørn fearlessly defines the following property, indexed by the positive naturalnumber n:

P(n) ≡n∏i=1

(f(i)× g(i)) =

(n∏i=1

f(i)

(n∏i=1

g(i)

)He then sets out to prove that P(n) holds for any positive natural number n, by induction on n.

NB. 2 out of the 4 following questions are identical, to give you time to think and to giveweight to your answer in your overall grade. So make sure to give the same answer 2times to these 2 identical questions.

Question 1Base case: does P(1) hold?

a Yes.b No.

Question 2Base case: does P(1) hold?

a Yes.b No.

Question 3Induction case: assuming that P(k) holds for some k : N, does P(k+ 1) follow logically?

a Yes.b No.

Question 4Induction case: assuming that P(k) holds for some k : N, does P(k+ 1) follow logically?

a Yes.b No.

Page 4: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

– 6 questions about proofs by induction –All of the following questions occur twice (and are tagged “(A)” / “(B)” / “(C)” to help you spotthem), to give you time to think and to give weight to your answer in your overall grade. So makesure to give the same answers to the same questions.

Question 5Which of the following inequalities holds? (A)

a ∀n : N, 0 ≤∑9

i=0 random(11) ≤ 81

b ∀n : N, 0 ≤∑9

i=0 random(11) ≤ 99

c ∀n : N, 0 ≤∑9

i=0 random(11) ≤ 90

d ∀n : N, 0 ≤∑9

i=0 random(11) ≤ 100e none of these inequalities holds

Question 6Which of the following inequalities holds? (A)

a ∀n : N, 0 ≤∑9

i=0 random(11) ≤ 81

b ∀n : N, 0 ≤∑9

i=0 random(11) ≤ 99

c ∀n : N, 0 ≤∑9

i=0 random(11) ≤ 90

d ∀n : N, 0 ≤∑9

i=0 random(11) ≤ 100e none of these inequalities holds

Question 7Which of the following inequalities holds? (B)

a ∀n : N, 0 <∑n

i=0(2× random(i+ 1)) < n× (n+ 1)b ∀n : N, 0 <

∑ni=0(2× random(i+ 1)) ≤ n× (n+ 1)

c ∀n : N, 0 ≤∑n

i=0(2× random(i+ 1)) < n× (n+ 1)d ∀n : N, 0 ≤

∑ni=0(2× random(i+ 1)) ≤ n× (n+ 1)

e none of these inequalities holds

Question 8Which of the following inequalities holds? (B)

a ∀n : N, 0 <∑n

i=0(2× random(i+ 1)) < n× (n+ 1)b ∀n : N, 0 <

∑ni=0(2× random(i+ 1)) ≤ n× (n+ 1)

c ∀n : N, 0 ≤∑n

i=0(2× random(i+ 1)) < n× (n+ 1)d ∀n : N, 0 ≤

∑ni=0(2× random(i+ 1)) ≤ n× (n+ 1)

e none of these inequalities holds

Page 5: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 9Which of the following inequalities holds? (C)

a ∀n : N, n+ 1 <∑n

i=0(2× random(i+ 1) + 1) < (n+ 1)2

b ∀n : N, n+ 1 <∑n

i=0(2× random(i+ 1) + 1) ≤ (n+ 1)2

c ∀n : N, n+ 1 ≤∑n

i=0(2× random(i+ 1) + 1) < (n+ 1)2

d ∀n : N, n+ 1 ≤∑n

i=0(2× random(i+ 1) + 1) ≤ (n+ 1)2

e none of these inequalities holds

Question 10Which of the following inequalities holds? (C)

a ∀n : N, n+ 1 <∑n

i=0(2× random(i+ 1) + 1) < (n+ 1)2

b ∀n : N, n+ 1 <∑n

i=0(2× random(i+ 1) + 1) ≤ (n+ 1)2

c ∀n : N, n+ 1 ≤∑n

i=0(2× random(i+ 1) + 1) < (n+ 1)2

d ∀n : N, n+ 1 ≤∑n

i=0(2× random(i+ 1) + 1) ≤ (n+ 1)2

e none of these inequalities holds

Page 6: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

– 10 questions about language processors –In direct contravention to the recommendation of the Ministry of Dental Health and Compilation,Sigtryg the Square implemented, in Scheme, a compiler that resugars Scheme programs into theBNF used for Mimer’s self-interpreter and for his own self-applicable syntax checker (i.e., forPart II/II, with case-expressions, variadic lambda-abstractions and everything). For example, asspecified in the lecture notes, Sigtryg resugars:

• nested if-expressions into cond-expressions,

• Boolean-valued nested if-expressions into and-expressions,

• applications of a lambda-abstraction to actual parameters into let-expressions,

• etc.

For the rest, Sigtryg remembers from the definition of a compiler that Loki found on the web thata compiler “should only compile those things that need to be compiled and ignore those otherthings that should not be compiled.” Therefore, he made it a point that the parts of a source programthat already use syntactic sugar are left as they are.

Sigtryg then verifies that the text of his compiler is accepted by Mimer’s online syntax checker,which is known to be correct.

The following questions assume that both of Sigtryg’s compiler and syntax checker are correct.

Question 11Sigtryg compiles his compiler. Is the result accepted by Mimer’s online syntax checker?

a Yes.b No.c One cannot say, because as is well known, self-application is ill-typed and can diverge.

Question 12Sigtryg compiles the self-interpreter that was handed in at the first lecture and that he used asa test for his own syntax checker, in the 5th week of the course. Is the compiled self-interpreteraccepted by Mimer’s online syntax checker?

a Yes.b No.c One cannot say, because as shown in the first week of the course, the self-interpreter

incurs an exponential slowdown. Therefore compiling the self-interpreter takes so longthat we will be given our dProgSprog grade before this process completes – unlessPetite Chez Scheme runs out of memory first (remember: exponential), in which casewe won’t know for sure.

Question 13Sigtryg compiles his own self-applicable syntax checker. Is the result a syntax checker?

a Yes.b No.

Page 7: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 14Sigtryg compiles his own self-applicable syntax checker. Is the result self-applicable?

a Yes.b No.

Question 15Sigtryg compiles his own self-applicable syntax checker. Can he use the result to syntax-check hisself-applicable syntax checker?

a Yes.b No.

Question 16Sigtryg compiles the self-interpreter. Is the result an interpreter?

a Yes.b No.

Question 17Sigtryg compiles the self-interpreter. Is the result self-applicable?

a Yes.b No.

Question 18Sigtryg compiles a program p0, resulting into a compiled program p1. Is p1 the same as p0?

a Yes.b No.c Sometimes yes, sometimes no: one cannot say in general.

Question 19Sigtryg compiles a program p0, resulting into a compiled program p1, and then he compiles p1,resulting into a compiled program p2. Is p2 the same as p1?

a Yes.b No.c Sometimes yes, sometimes no: one cannot say in general.

Page 8: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 20Sigtryg compiles a program p0, resulting into a compiled program p1, and then he compiles p1,resulting into a compiled program p2, and then he compiles p2, resulting into a compiled programp3, and then he compiles p3, resulting into a compiled program p4, etc. So what Sigtryg has hereis not a tower of interpreters, but a train of compilers. As the program goes from wagon to wagonin the train, will compilation be faster or slower?

a By Moore’s law, it will be faster and faster, because the speed of hardware processorsdoubles every two years.

b There will definitely be a speedup at first, because we compile programs to optimizethem, but then the speedup will slowly decrease, because there will strictly be less andless to optimize: the more we compile, the less there will be to compile. Eventuallythere will be nothing left to compile and therefore no speedup and no slowdown: wewill have reached a fixed point.

c For any n ∈ N, compiling pn+1 into pn+2 is exactly as efficient as compiling pn+2 intopn+3.

d Information theory tells us that due to physical entropy and chemical decay, it will takelonger and longer to go from wagon to wagon.

e Sometimes faster, sometimes slower: one cannot say in general.

Page 9: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

– 12 questions about syntax checking –Heimdal the White is considering the grammar of fully parenthesized regular expressions fromthe lecture notes:

REGEXP ::= (empty)

| (atom ATOM)

| (any)

| (var NAME)

| (seq REGEXP REGEXP)

| (disj REGEXP REGEXP)

| (star REGEXP)

| (plus REGEXP)

ATOM ::= ...any Scheme integer...

NAME ::= ...any Scheme symbol...

He is wondering whether some of the regular expressions he wrote are syntactically correct. Thegoal of the following questions is to help Heimdal figure this out.

Question 21Is the following term a well-formed regular expression?

(empty)

a Yes.b No.

Question 22Is the following term a well-formed regular expression?

()

a Yes.b No.

Question 23Is the following term a well-formed regular expression?

(var empty)

a Yes.b No.

Question 24Is the following term a well-formed regular expression?

(var (empty))

a Yes.b No.

Page 10: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 25Is the following term a well-formed regular expression?

(seq (plus (star (plus (empty)))))

a Yes.b No.

Question 26Is the following term a well-formed regular expression?

(disj (seq (atom 9) (disj (atom 8) (empty))) (seq (disj (atom 7) (any)) (plus (empty))))

a Yes.b No.

Question 27Is the following term a well-formed regular expression?

(seq (seq (seq (seq (star) (atom 1)) (atom 2)) (atom 3)) (atom 4))

a Yes.b No.

Question 28Is the following term a well-formed regular expression?

(seq (seq (seq (seq (plus (empty)) (atom 1)) (atom 2)) (atom 3)) (atom 4))

a Yes.b No.

Question 29Is the following term a well-formed regular expression?

(seq (seq (empty) (seq (atom 1) (seq (atom 2) (star (var "x"))))) (seq (empty) (seq (atom 3) (atom 4))))

a Yes.b No.

Question 30Is the following term a well-formed regular expression?

(seq (seq (empty) (seq (atom 1) (atom 2))) (seq (empty) (seq (atom 3) (atom 4))))

a Yes.b No.

Page 11: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 31Heimdal is given access to 1500 positive tests and 500 negative tests, some of which have beengenerated by machine using the random procedure, and all of which have been proved to be correct. IfHeimdal’s syntax checker for regular expressions passes both the positive tests and the negativetests, does it mean that it is correct?

a Yes.b No.

Question 32If Heimdal’s syntax checker for regular expressions is accepted by Mimer’s syntax checker forScheme (which is known to be correct), does it mean that it is correct?

a Yes.b No.

Page 12: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

– 10 questions about scope and block structure:global (define), parallel (let), sequential (let*),

and recursive (letrec) declarationsas well as about parameter passing –

The following questions take place in the standard environment of Petite Chez Scheme, includingthe random procedure described on page 2 of the present exam set.

Question 33Using the standard call-by-value version of Scheme, what is the result of evaluating the followingexpression?

(+ (let ([a (let* ([a 0]

[a (+ a 1)])

a)])

(+ a 10))

(let ([a (let* ([a (+ a 2)]

[a 0])

a)])

(+ a 20)))

a 0

b 1

c 2

d 3

e 10

f 11

g 12

h 13

i 20

j 21

k 22

l 23

m 30

n 31

o 32

p 33

q 40

r 41

s 42

t 43

u a run-time errorv no result, because evaluation diverges

Page 13: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 34Using a call-by-name version of Scheme, what is the result of evaluating the following expression?

(+ (let ([a (let* ([a 0]

[a (+ a 1)])

a)])

(+ a 10))

(let ([a (let* ([a (+ a 2)]

[a 0])

a)])

(+ a 20)))

a 0

b 1

c 2

d 3

e 10

f 11

g 12

h 13

i 20

j 21

k 22

l 23

m 30

n 31

o 32

p 33

q 40

r 41

s 42

t 43

u a run-time errorv no result, because evaluation diverges

Question 35Using the standard call-by-value version of Scheme, what is the result of evaluating the followingexpression?

(let ([x (lambda () y)]

[y (lambda () 3)]

[z (lambda () z)])

(y))

a 3

b a run-time errorc no result, because evaluation diverges

Page 14: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 36Using a call-by-name version of Scheme, what is the result of evaluating the following expression?

(let ([x (lambda () y)]

[y (lambda () 3)]

[z (lambda () z)])

(y))

a 3

b a run-time errorc no result, because evaluation diverges

Question 37Using a call-by-need version of Scheme, what is the result of evaluating the following expression?

(let ([x (lambda () y)]

[y (lambda () 3)]

[z (lambda () z)])

(y))

a 3

b a run-time errorc no result, because evaluation diverges

Question 38Using the standard call-by-value version of Scheme, what is the result of evaluating the followingexpression?

(let ([x (1+ (random 2))])

(list x x))

a (2 2)

b (2 1)

c (1 2)

d (1 1)

e (2 2) or (1 1)

f (2 2) or (2 1)

g (2 2) or (1 2)

h (2 1) or (1 2)

i (2 1) or (1 1)

j (1 2) or (1 1)

k (2 2) or (2 1) or (1 2) or (1 1)

l a run-time errorm no result, because evaluation diverges

Page 15: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 39Using a call-by-name version of Scheme, what is the result of evaluating the following expression?

(let ([x (1+ (random 2))])

(list x x))

a (2 2)

b (2 1)

c (1 2)

d (1 1)

e (2 2) or (1 1)

f (2 2) or (2 1)

g (2 2) or (1 2)

h (2 1) or (1 2)

i (2 1) or (1 1)

j (1 2) or (1 1)

k (2 2) or (2 1) or (1 2) or (1 1)

l a run-time errorm no result, because evaluation diverges

Question 40Using a call-by-need version of Scheme, what is the result of evaluating the following expression?

(let ([x (1+ (random 2))])

(list x x))

a (2 2)

b (2 1)

c (1 2)

d (1 1)

e (2 2) or (1 1)

f (2 2) or (2 1)

g (2 2) or (1 2)

h (2 1) or (1 2)

i (2 1) or (1 1)

j (1 2) or (1 1)

k (2 2) or (2 1) or (1 2) or (1 1)

l a run-time errorm no result, because evaluation diverges

Page 16: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 41Using the standard call-by-value version of Scheme, what is the result of evaluating the followingexpression?

(let* ([f (lambda (x)

(lambda (y)

(+ x y)))]

[g (f 100)]

[y 10]

[x 1]

[f (lambda (x)

(lambda (y)

(- x y)))]

[+ (lambda (x y)

(list x y))]

[z (g 1000)]

[g (lambda (z)

(/ z 0))])

z)

a -999

b 0

c 11

d 90

e 101

f 110

g 1000

h 1001

i 1100

j (100 10)

k (100 1000)

l #f

m the error “division by zero”n no result, because evaluation diverges

Page 17: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 42Given

(define syracuse

(lambda (n_init)

(letrec ([iterate (lambda (n number-of-iterations)

(cond

[(even? n)

(iterate (/ n 2) (1+ number-of-iterations))]

[(= n 1)

number-of-iterations]

[else

(iterate (1+ (* n 3)) (1+ number-of-iterations))]))])

(if (and (integer? n_init)

(>= n_init 0))

(iterate n_init 0)

(errorf ’syracuse "not a non-negative integer: ~s" n_init)))))

what is the result of evaluating (syracuse 24), using the standard call-by-value version of Scheme?

a -1

b 0

c 2

d 4

e 6

f 8

g 10

h 12

i 14

j 16

k an odd numberl #f

m the run-time error “not a non-negative integer: 24”n the run-time error “stack overflow”o the run-time error “stack underflow”p It is impossible to say: nobody knows in general how many iterations it takes to reach

1, or even if 1 is always reached. Paul Erdos famously said about this procedure that“Mathematics may not be ready for such problems.” So Computer Science...

q no result, because evaluation diverges

Page 18: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

– 17 questions about programming with lists –Dyck the Unenlightened can only see the parentheses in a Scheme program. Unfortunately, anddespite the valiant efforts of his senpai, he can’t get his Scheme mode to work in Emacs. So hedevises a method to check whether the text of a Scheme program is well parenthesized, afterstripping everything that is not a parenthesis in this text.

To this end, Dyck writes an auxiliary procedure that takes a string and returns the list of paren-theses occurring in this string, in the order where they occur in the string. He also reads squarebrackets as parentheses. Here are his positive unit tests:

(define positive-test-strip-everything-but-parentheses

(lambda (candidate)

(and (equal? (candidate "")

’())

(equal? (candidate "Hello world!")

’())

(equal? (candidate "()")

’(#\( #\)))

(equal? (candidate "(lambda (x) x)")

’(#\( #\( #\) #\)))

(equal? (candidate "(cond [else 42])")

’(#\( #\( #\) #\)))

(equal? (candidate "(letrec ([f (lambda (x) (f x))]) f)")

’(#\( #\( #\( #\( #\( #\) #\( #\) #\) #\) #\) #\)))

;;; etc.

)))

Dyck then implements a procedure that maps a string into a proper list of opening and closingparentheses, starting from the end of the string and stripping all the characters that are not paren-theses:

(define strip-everything-but-parentheses

(lambda (s)

(letrec ([visit (lambda (i cs)

(if (< i 0)

DDDD

(case (string-ref s i)

[(#\) #\])

EEEE]

[(#\( #\[)

FFFF]

[else

GGGG])))])

(if (string? s)

(let ([length-of-s (string-length s)])

(if (= length-of-s 0)

AAAA

BBBB))

CCCC))))

Somewhat unavoidably, with this being the August exam and all, Dyck counts on you to helphim writing Scheme code in the stead of AAAA, BBBB, etc.

Page 19: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Moving on, Dyck flexes his biceps and whips out the following unit tests for his main proce-dure:

(define positive-test-matching-parentheses?

(lambda (candidate)

(and (candidate (strip-everything-but-parentheses ""))

(candidate (strip-everything-but-parentheses "()"))

(candidate (strip-everything-but-parentheses "(())"))

(candidate (strip-everything-but-parentheses "()()"))

(candidate (strip-everything-but-parentheses "(()())"))

;;; etc.

)))

(define negative-test-matching-parentheses?

(lambda (candidate)

(not (or (candidate (strip-everything-but-parentheses "("))

(candidate (strip-everything-but-parentheses ")"))

(candidate (strip-everything-but-parentheses "(()"))

(candidate (strip-everything-but-parentheses "(()))"))

;;; etc.

))))

(define test-matching-parentheses?

(lambda (candidate)

(and (positive-test-matching-parentheses? candidate)

(negative-test-matching-parentheses? candidate))))

And finally, here is Dyck’s grand oeuvre, where he traverses the given list of characters, threadinga counter that he increments when he meets an opening parenthesis and that he decrements whenhe meets a closing parenthesis:

(define matching-parentheses?

(lambda (cs_init)

(letrec ([iterate (lambda (cs i)

(cond

[(null? cs)

IIII]

[(pair? cs)

(case (car cs)

[(#\()

JJJJ]

[(#\))

KKKK]

[else

LLLL])]

[else

MMMM]))])

HHHH)))

Dyck the Unenlightened is struggling to write Scheme code in the stead of HHHH, IIII, etc.Give him a hand.

Page 20: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 43Which of the following candidates should Dyck pick in the stead of AAAA?

a ’()

b 0

c (errorf ’strip-everything-but-parentheses "empty string")

d None of these candidates.

Question 44Which of the following candidates should Dyck pick in the stead of BBBB?

a (visit (1- length-of-s) ’())

b (visit length-of-s ’())

c (visit (1+ length-of-s) ’())

d None of these candidates.

Question 45Which of the following candidates should Dyck pick in the stead of CCCC?

a (errorf ’strip-everything-but-parentheses "not a string: ~s" s)

b ’()

c ""

d None of these candidates.

Question 46Which of the following candidates should Dyck pick in the stead of DDDD?

a cs

b ’()

c s

d 0

e None of these candidates.

Question 47Which of the following candidates should Dyck pick in the stead of EEEE?

a (visit (1- i) (cons #\( cs))

b (visit (1- i) (cons #\) cs))

c (visit (1+ i) (cons #\( cs))

d (visit (1+ i) (cons #\) cs))

e None of these candidates.

Page 21: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 48Which of the following candidates should Dyck pick in the stead of FFFF?

a (visit (1- i) (cons #\( cs))

b (visit (1- i) (cons #\) cs))

c (visit (1+ i) (cons #\( cs))

d (visit (1+ i) (cons #\) cs))

e None of these candidates.

Question 49Which of the following candidates should Dyck pick in the stead of GGGG?

a (visit (1- i) cs)

b (visit (1+ i) cs)

c (visit (1- i) (cdr cs))

d (visit (1+ i) (cdr cs))

e None of these candidates.

Question 50Should Dyck add the following line as a positive unit test or as a negative unit test?

(candidate (strip-everything-but-parentheses "(()(()()))"))

a As a positive unit test.b As a negative unit test.

Question 51Should Dyck add the following line as a positive unit test or as a negative unit test?

(candidate (strip-everything-but-parentheses "(()))((()()))"))

a As a positive unit test.b As a negative unit test.

Question 52Which of the following candidates should Dyck pick in the stead of HHHH?

a (iterate cs init 0)

b (iterate 0 cs init)

c (iterate cs init)

d (iterate 0)

e (iterate cs)

f (iterate cs (length cs))

g (iterate cs init (length cs init))

h None of these candidates.

Page 22: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 53Which of the following candidates should Dyck pick in the stead of IIII?

a (< i 0)

b (<= i 0)

c (= i 0)

d (>= i 0)

e (> i 0)

f i

g 0

h ’()

i #t

j #f

k None of these candidates.

Question 54Which of the following candidates should Dyck pick in the stead of JJJJ?

a (iterate (cdr cs) (1+ i))

b (and (> i 0) (iterate (cdr cs) (1+ i)))

c (iterate (cdr cs) (1- i))

d (and (> i 0) (iterate (cdr cs) (1- i)))

e None of these candidates.

Question 55Which of the following candidates should Dyck pick in the stead of KKKK?

a (iterate (cdr cs) (1+ i))

b (and (> i 0) (iterate (cdr cs) (1+ i)))

c (iterate (cdr cs) (1- i))

d (and (> i 0) (iterate (cdr cs) (1- i)))

e None of these candidates.

Question 56Which of the following candidates should Dyck pick in the stead of LLLL?

a (iterate (cdr cs) (1- i))

b (iterate (cdr cs) (1+ i))

c #t

d #f

e (errorf ’matching-parentheses? "not a parenthesis: ~s" (car cs))

f None of these candidates.

Page 23: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 57Which of the following candidates should Dyck pick in the stead of MMMM?

a #t

b #f

c 0

d cs

e cs_init

f (errorf ’matching-parentheses? "not the tail of a proper list: ~s" cs)

g None of these candidates.

Question 58In the definition of strip-everything-but-parentheses, consider the string s containing the case-expression, i.e.,

(case (string-ref s i)

[(#\) #\])

EEEE]

[(#\( #\[)

FFFF]

[else

GGGG])

What is the result of evaluating the following expression?

(matching-parentheses? (strip-everything-but-parentheses s))

a #t

b #f

c an error

Question 59In the definition of matching-parentheses?, consider the string s containing the case-expression, i.e.,

(case (car cs)

[(#\()

JJJJ]

[(#\))

KKKK]

[else

LLLL])

What is the result of evaluating the following expression?

(matching-parentheses? (strip-everything-but-parentheses s))

a #t

b #f

c an error

Page 24: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

– 10 questions about object-oriented programming –Algot the Intrepid defines the following Boolean-object maker, creates three Boolean objects, andoperates on them through the successive definitions from aaa to eee.

(define make-boolean-object

(lambda (b_init)

(let ([state (not b_init)])

(lambda (message)

(case message

[(get)

(list (not state))]

[(negate)

(begin

(set! state (not state))

(list))]

[(set)

(lambda (b)

(begin

(set! state (not b))

(list)))]

[(initial)

(list b_init)]

[else

(list "Hvad siger du?")])))))

(define bo1 (make-boolean-object #t))

(define bo2 (make-boolean-object #f))

(define bo3 (make-boolean-object #t))

(define aaa (bo1 ’get))

(define bbb (bo1 ’negate))

(define ccc (bo2 ’not))

(define ddd (bo3 ’negate))

(define eee (bo3 ’negate))

Question 60What is the result of evaluating the following expression?

(bo0 ’get)

a #t

b #f

c "Hvad siger du?"

d (#t)

e (#f)

f ()

g ("Hvad siger du?")

h a run-time errori no result, because evaluation diverges

Page 25: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 61What is the result of evaluating the following expression?

(bo1 ’get)

a #t

b #f

c "Hvad siger du?"

d (#t)

e (#f)

f ()

g ("Hvad siger du?")

h a run-time errori no result, because evaluation diverges

Question 62What is the result of evaluating the following expression?

(bo2 ’get)

a #t

b #f

c "Hvad siger du?"

d (#t)

e (#f)

f ()

g ("Hvad siger du?")

h a run-time errori no result, because evaluation diverges

Question 63What is the result of evaluating the following expression?

(bo3 ’get)

a #t

b #f

c "Hvad siger du?"

d (#t)

e (#f)

f ()

g ("Hvad siger du?")

h a run-time errori no result, because evaluation diverges

Page 26: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 64What is the result of evaluating the following expression?

(bo3 ’initial)

a #t

b #f

c "Hvad siger du?"

d (#t)

e (#f)

f ()

g ("Hvad siger du?")

h a run-time errori no result, because evaluation diverges

Question 65Is it possible to re-initialize a Boolean object to its initial value?

a Yes.b No.

Question 66Remembering that in Scheme, everything that is not #f is interpreted as true, what is the result ofevaluating the following expression?

(let ([bo4 (make-boolean-object "initialized Boolean value")])

(begin

(bo4 ’negate)

(bo4 ’get)))

a #t

b #f

c "initialized Boolean value"

d "Hvad siger du?"

e (#t)

f (#f)

g ()

h (not "uninitialized Boolean value")

i ("initialized Boolean value")

j ("Hvad siger du?")

k a run-time errorl no result, because evaluation diverges

Page 27: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 67What is the result of evaluating the following expression?

(define bo5 (make-boolean-object #t))

(begin

((bo5 ’set) (and (car (bo1 ’get))

(car (bo3 ’get))))

(bo5 ’get))

a #t

b #f

c "Hvad siger du?"

d (#t)

e (#f)

f ()

g ("Hvad siger du?")

h a run-time errori no result, because evaluation diverges

Question 68What is the result of evaluating the following expression?

(define bo6 (make-boolean-object #f))

(begin

((bo6 ’set) (and (car (bo1 ’get))

(car (bo3 ’get))))

(bo6 ’get))

a #t

b #f

c "Hvad siger du?"

d (#t)

e (#f)

f ()

g ("Hvad siger du?")

h a run-time errori no result, because evaluation diverges

Page 28: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 69Does the following procedure successfully swap the content of the state of two Boolean objects?

(define swap-content-of-state-of-Boolean-objects

(lambda (bo1 bo2)

(let ([x1 (car (bo1 ’get))]

[x2 (car (bo2 ’get))])

((bo1 ’set) x2)

((bo2 ’set) x1)

(list))))

a Yes.b No.

Page 29: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

– 10 questions about logic programming –The goal of this group of questions is to study the following Schelog relations:

(define %binary-relation-a

(%rel (X1 X2 XS YS)

[(’() ’())]

[((cons X1 (cons X2 XS)) (cons X1 (cons X2 YS)))

(%binary-relation-a XS YS)]))

(define %binary-relation-b

(%rel (X1 X2 XS YS)

[(’() ’())]

[((cons X1 (cons X2 XS)) (cons X2 (cons X1 YS)))

(%binary-relation-b XS YS)]))

(define %binary-relation-c

(%rel (AS BS CS)

[(AS CS)

(%binary-relation-b AS BS)

(%binary-relation-b BS CS)]))

Question 70Is the following query satisfied?

(%which ()

(%binary-relation-a ’() ’()))

a Yes.b No.

Question 71Is the following query satisfied?

(%which ()

(%binary-relation-a ’(1) ’(1)))

a Yes.b No.

Question 72Is the following query satisfied?

(%which ()

(%binary-relation-a ’(1 2) ’(1 2)))

a Yes.b No.

Page 30: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 73Is the following query satisfied?

(%which ()

(%binary-relation-a ’(1 2 3 4 5) ’(1 2 3 4 5)))

a Yes.b No.

Question 74Is the following query satisfied, and if yes, for which value of X and Y?

(%which (X Y)

(%binary-relation-a (list 0 X 2 3 4 5 6 7) (list 0 1 Y 3 4 5 6 7)))

a Yes: the result is ((X 1) (Y 2)).b Yes: the result is ((X 2) (Y 1)).c No: the result is #f.

Question 75Is the following query satisfied, and if yes, for which value of ZS?

(%which (ZS)

(%binary-relation-b ’(1 2 3 4 5 6 7 8 9) ZS))

a Yes: the result is (ZS (1 0 3 2 5 4 7 6 9 8)).b Yes: the result is ZS.c No: the result is #f.

Question 76Is the following query satisfied, and if yes, for which value of A, B, C, and D?

(%which (A B C D)

(%binary-relation-b (list A B C D) (list 1 2 3 4)))

a Yes: the result is ((A 1) (B 2) (C 3) (D 4)).b Yes: the result is ((A 2) (B 1) (C 4) (D 3)).c Yes: the result is ((A 4) (B 3) (C 2) (D 1)).d Yes: the result is ((A 3) (B 4) (C 1) (D 2)).e No: the result is #f.

Page 31: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 77Is the following query satisfied, and if yes, for which value of A, B, C, and D?

(%which (A B C D)

(%binary-relation-c (list A 2 C 4) (list 1 B 3 D)))

a Yes: the result is ((A 1) (B 2) (C 3) (D 4)).b Yes: the result is ((A 2) (B 1) (C 4) (D 3)).c Yes: the result is ((A 4) (B 3) (C 2) (D 1)).d Yes: the result is ((A 3) (B 4) (C 1) (D 2)).e No: the result is #f.

Question 78Is the following query satisfied?

(%which ()

(%binary-relation-c ’(1 2 3) ’(1 2 3)))

a Yes.b No.

Question 79Does there exist two lists that belong

• in the binary relation defined by %binary-relation-a,

• in the binary relation defined by %binary-relation-b, and

• in the binary relation defined by %binary-relation-c?

a Yes.b No.

Page 32: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

– 5 questions about a logical approach to syntax checking –Brynja the Systematic has a treat for you: a revolutionary way to syntax-check basic imperativeprograms – using logic! Here is a collection of inference rules to this end:

I is a natural number

------------------------ LOCATION

|-reference (location I)

N is a number B is a boolean

------------------------- CONSTANT_NUMBER ------------------------- CONSTANT_BOOLEAN

|-expression (constant N) |-expression (constant B)

|-reference R |-expression E1 |-expression E2

---------------------------- DEREFERENCE ---------------------------------- PLUS

|-expression (dereference R) |-expression (plus E1 E2)

|-expression E1 |-expression E2 |-expression E1 |-expression E2

---------------------------------- MINUS ---------------------------------- TIMES

|-expression (minus E1 E2) |-expression (times E1 E2)

|-expression E1 |-expression E2 |-expression E1 |-expression E2

---------------------------------- EQUAL ---------------------------------- AND

|-expression (= E1 E2) |-expression (and E1 E2)

|-expression E1 |-expression E2 |-expression E

---------------------------------- OR -------------------- NOT

|-expression (or E1 E2) |-expression (not E)

|-command C1 |-command C2

---------------- SKIP ---------------------------- SEQUENCE

|-command (skip) |-command (sequence C1 C2)

|-reference R |-expression E

------------------------------- ASSIGN

|-command (assign R E)

|-expression E |-command C1 |-command C2 |-expression E |-command C

---------------------------------------------- IF ----------------------------- WHILE

|-command (if E C1 C2) |-command (while E C)

|-command C

----------------- TOP

|-program (top C)

To answer the following 5 questions, you will need to draw proof trees.

Page 33: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 80Is the following reference syntactically well formed?

(reference 0)

In other words, does the following judgment hold?

|-reference (reference 0)

a Yes.b No.

Question 81Is the following expression syntactically well formed?

(or (dereference (location 42)) (not (dereference (location 42))))

In other words, does the following judgment hold?

|-expression (or (dereference (location 42)) (not (dereference (location 42))))

a Yes.b No.

Question 82Is the following command syntactically well formed?

(if (constant 42)

(while (constant #f) (skip))

(while (constant #t) (skip)))

In other words, does the following judgment hold?

|-command (if (constant 42) (while (constant #f) (skip)) (while (constant #t) (skip)))

a Yes.b No.

Question 83Is the following command syntactically well formed?

(sequence (sequence (skip) (skip)) (skip))

In other words, does the following judgment hold?

|-command (sequence (sequence (skip) (skip)) (skip))

a Yes.b No.

Page 34: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

Question 84Is the following program syntactically well formed?

(top (times (constant 5)

(times (constant 4)

(times (constant 3)

(times (constant 2)

(times (constant 1)

(constant 1)))))))

In other words, does the following judgment hold?

|-program (top (times (constant 5)

(times (constant 4)

(times (constant 3)

(times (constant 2)

(times (constant 1)

(constant 1)))))))

a Yes.b No.

Page 35: dProgSprog final exam, August 2015users-cs.au.dk/danvy/dProgSprog16/Supplementary-material/dProgS… · – Supertramp (Fool’s overture) dProgSprog final exam, August 2015 ID

– 4 miscellaneous questions –

Question 85What is the result of evaluating the following expression?

(equal? ’(quote whatever) (quote ’whatever))

a #t

b #f

c an error

Question 86What is the result of adding 1 to 77 in base 8?

a an error: numerical underflowb an error: numerical overflowc 42d 78e 80f 100

Question 87Hans Christian Andersen meets Pinocchio, and they chit-chat amicably. (NB. Hans ChristianAndersen knows full well that Pinocchio’s nose grows whenever he tells a lie, and that it doesn’twhenever he tells the truth.)Pinocchio says: “Ah, my nose feels normal.” Facetiously, he takes his handkerchief and covershis nose with it. Without removing his handkerchief, he says: “Yup, it is not growing.”Should Hans Christian Andersen believe Pinocchio?

a Yes he should.b No he should not.c There is not enough information to say either way.d There is enough information to say both ways: it is a paradox.

Question 88Mimer asks Sigbjørn the Fearless whether there exists a word in English that contains all of thevowels “a”, “e”, “i”, “o”, “u”, and “y”, in another order than the alphabetical order. Does thereexist such a word in English?

a Yes, unquestionably.b No, undoubtedly.c I cannot possibly know this, and furthermore this question about reference is question-

ably, nay, nefariously out of scope for a first-year course on programming languages.


Recommended