Quizzes Flashcards

1
Q

One of the major differences between the imperative and functional programming languages is that the functional programming languages do NOT (in general)…

A

have side-effects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Convert the following expression into prefix-p notation (a Scheme statement):
10 + (5 - 3) + 2/4

A

(+ 10 (- 5 3) (/ 2 4))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Convert the following expression into prefix-p notation (a Scheme statement):
-5 * (2 + 1/2) + 40

A

(+ (* (- 5) (+ 2 (/ 1 2))) 40)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The statement “a function is a first-class object” means that a function…

A

can be placed in a place where a value is expected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What notation requires parentheses in order to correctly define the order of computation?

A

infix notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In Scheme, the form (symbol-length? ‘James) will return:

A

an error message

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which of the following is a valid Scheme expression?

A

(* 9 (/ (- 4 2) 7))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the expected result for this expression (in Scheme)?
(string-ref “Hello World” 4)

A

/o

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Given an expression: x1 + x2 + x3 + x4
Which language allows us to evaluate the expression in this order: (1) x1 plus x2; (2) x3 plus x4; (3) sum of (x1 + x2) plus sum of (x3 + x4), without concern for producing a different result than other evaluation orders:

A

Scheme

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which of the following expression will return false (#f)?

A

(number? #\7)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The Scheme for (char? #\5) will return…

A

true (#t)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What data structure is used in Scheme for representing extremely large integers?

A

probably a list. Really, we shouldn’t know or care.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

A student is wondering if Scheme is a strong or weakly typed language. How might they check this?

A

They could execute a form like (+ #\a 10) in the REPL. If i works (like in C), then it is probably weakly typed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Given this procedure, what is the return result?

(define (guess value)
(cond ((number? value) “I’m a number”)
((char? value “I’m a character”)
((integer? value) “I’m an integer”)))

(guess 10)

A

“I’m a number”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What statements contain non-functional features of Scheme?

A

(begin (write x) x)

(display x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the result of this expression (in Scheme)?

(let ((x 10)
(y 10)
(- (* x y) y))

A

90

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What functional feature does the code below best exhibit?
(define start-engine (lambda ()
(error-detection (wheel-velocity (wheel-sensor)) (body-velocity))))

A

procedures are first class objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

A let-form in Scheme is equivalent (in giving names to values) to…

A

an unnamed/lambda procedure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Given the Scheme code, answer the following two questions.
((lambda (x)
((lambda (x y)
(+ x y))
5 (* 7 x)))
3)

1). What is the value passed to parameter y?
2). What is the final output?

A

y = 21

final output = 26

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Given the Scheme code, answer the following two questions.
((lambda (x)
((lambda (x y)
(+ x y))
4 (* 6 x)))
3)

1). What is the value passed to parameter y?
2). What is the final output?

A

y = 18

final output = 22

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Which of the following forms will return an error when run?

A

(‘+ 2 3)

(define a a)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Given this Scheme procedure:
(define foo (lambda (x)
(if (= x 0)
1
(* x (foo (- x 1)))
)
))

What does this procedure do?

A

compute x!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Which of the followings is a Scheme pair?

A

‘(x.y)

‘(x.x)

’(() ())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the return value of the code below? (min is a procedure that returns the minimum of two values.)

(define lst ‘(3 5 2 9))
(min (car lst) (cadr lst))

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the correct method for constructing a pair in Scheme?

A

(cons 1 2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Compare the following two Scheme forms:
(append ‘(1 2) ‘(4 5)) and (cons ‘(1 2) ‘(4 5)).

A

(cons ‘(1 2) ‘(4 5)) returns ‘((1 2) 4 5).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Which of the following is equivalent to the expression below?
(car (cdr ‘(1 2 3 4 5))

A

(cadr ‘(1 2 3 4 5))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Given this expression, what is the expected result?
(car (cdr ‘(1 2 3 4 5))

A

2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Why is it fair to use either recursion or iteration to solve a problem like factorial?

A

Factorial describes how the result should look (e.g., 6! = 6 * 5 * 4 * 3 * 2 * 1), it doesn’t require a specific algorithm to get the result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What aspect of computation in a recursive algorithm leads to slow performance?

A

Deferring work until computing the recursive result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the typical design approach used in Scheme algorithms (like cipher or mergesort)?

A

Break functionality into little pieces and then assemble them into something more complicated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Consider the first two procedures for doing the cipher encryption:

(define (string-encryption str) ;main procedure
(encryption-recursive str 0 (string-length str)))

(define (encryption-recursive str pos len)
(if (>= pos len)
“” ;empty string
(string-append
(character-encryption (string-ref str pos))
(encryption-recursive str (+ pos 1) len))))

Why does the true branch of the if-statement in encryption-recursive return “” instead of ‘()?

A

The result will eventually be used in string-append which needs a string.

33
Q

Given the Scheme code below, answer the following questions related to the Fantastic Four abstract approach.

(define dtob (lambda (N) ; line 1
(if (= N 0) (list 0) ; line 2
(append (dtob (quotient N 2)) ; line 3
(list (remainder N 2)))))) ; line 4

What line of code defines the stopping condition and the return value?

A

Line 2

34
Q

Given the Scheme code below, answer the following questions related to the Fantastic Four abstract approach.

(define dtob (lambda (N) ; line 1
(if (= N 0) (list 0) ; line 2
(append (dtob (quotient N 2)) ; line 3
(list (remainder N 2)))))) ; line 4

What line of code contains the size-M problem, where M < N?

A

Line 3

35
Q

Given the Scheme code below, answer the following questions related to the Fantastic Four abstract approach.

(define dtob (lambda (N) ; line 1
(if (= N 0) (list 0) ; line 2
(append (dtob (quotient N 2)) ; line 3
(list (remainder N 2)))))) ; line 4

What line of code defines the step that constructs the solution to the size-N problem?

A

Size 3 and 4

36
Q

Given the Scheme code as follows. What is the output?

(define not-gate (lambda (x) (if (= x 0) 1 0)))
(define onescomplement (lambda (a-list) (map not-gate a-list)))

(onescomplement ‘(0 1 0 2 0 3))

A

(1 0 1 0 1 0)

37
Q

A higher order function is a function that takes the…

A

operator of a function as an argument.

38
Q

Given this procedure, what is the expected result?
(map (lambda (x) (+ x x 2 )) ‘(1 2 3 4 5 (6 7)))

A

Error Message

39
Q

Given this procedure, what is the expected result?
(map (lambda (x) (+ x x 2 )) ‘(1 2 3 4 5 6))

A

(4 6 8 10 12 14)

40
Q

Given this procedure, and assuming filter is defined as in the slides, what is the expected result?

(filter (lambda (x) (= (remainder x 2) 0)) ‘(1 2 3 4 5 6))

A

‘(2 4 6)

41
Q

Filter is a higher-order function that…

A

applies a predicate to all elements of a list.

42
Q

The first implementation of mergesort only processed lists of numbers. How did higher-order programming help to make it generic and able to process more types of data?

A

Parameters were added to the algorithm to specify specific procedures for examining data.

43
Q

Which of the following predicates in logic programming matches most closely with this statement?
“Bill listens to music or news.”

A

listensto(bill, music); listnesto(bill, news).

44
Q

The key idea of logic programming paradigm is to solve a problem by describing…

A

what the problem is.

45
Q

A Prolog program finds a solution by…

A

searching the built-in database consisting of facts and rules.

46
Q

A Prolog variable represents a…

A

place holder.

47
Q

Waht notation does Prolog use for expressing arithmetic operations?

A

infix notation

48
Q

How many arguments can be defined in a fact?

A

n, where n >= 0.

49
Q

We apply an anonymous variable in the definition of a rule if we…

A

do not want to obtain a return value to the variable.

50
Q

A fact starts with a relationship followed by a list of arguments. The arguments of a fact…

A

can be variables or simple values or structures.

51
Q

A goal succeeds, if there are facts (rules) that match or unify the goal. What are required in order for a goal clause and a fact to unify?

A

Their predicates are the same,
they have the same number of arguments,
and
their corresponding arguments match.

52
Q

A Prolog program usually contains…

A

facts,
rules,
and
queries

53
Q

Given these facts:

is_dessert(cookie).
is_dessert(ice_cream).
is_dessert(pie).
is_dessert(cheesecake).
is_fruit(strawberry).
is_fruit(apple).
is_fruit(peach).
contains(cookie, chocolate_chips).
contains(ice_cream, cookie).
contains(ice_cream, strawberry).
contains(ice_cream, peach).
contains(pie, apple).
contains(pie, peach).
contains(pie, strawberry).
contains(cheesecake, strawberry).

Which of the following rule can identify the list of desserts that contains apple?

A

apple_dessert(X) :- is_dessert(X), contains(X, apple).

54
Q

In a Prolog recursive rule, what components are required?

A

Stopping condition.
Size-M problem, where M<N
Size-N problem

55
Q

What is wrong with this piece of Prolog code?
ancestor(A, D) :- ancestor (A, P), ancestor(P, D).

A

It does not have a stopping condition.

56
Q

If we try to use tail-recursive rules to implement non-tail-recursive rules, it will normally result in rules that are…

A

more complex and more difficult to understand.

57
Q

A circular definition of a rule…

A

may cause an infinite loop for certain goals.

58
Q

Assume that the rule factorial (N, Fac) will compute Fac = N!. What should be the output if the following question is asked?

?- factorial(2, 5).

A

no

59
Q

What programming language features are supported in Prolog?

A

Call-by-value
Call-by-reference

60
Q

The following Prolog program solves the hanoi tower problem:

hanoi(N) :- move(N, source, center, destination).
move(1, S, _, D) :- write(‘Move top from ‘), write(S), write(‘ to ‘), write(D), nl.
move(N, S, C, D) :- N>1, M is N-1, move(M, S, D, C), move(1, S, _, D), move (M, C, S, D).

What are the size-(N-1) problems in the program?

A

move(M, S, D, C)

move(M, C, S, D)

61
Q

Explain how the following rules check if there is a miscolor in a map factbase.

adjacent(X, Y) :- edge(X, Y); edge(Y, X).
miscolor(S1, S2, Color1) :- adjacent(S1, S2), color(S1, Color1), color(S2, Color1).

A

Prolog runtime will iteratively apply the rules to all the facts to find the matches and mismatches.

62
Q

Which of the followings is equivalent to this Prolog list: [cat | dog, pig]]?

A

[cat, dog, pig]

63
Q

What goal will return a “no” answer?

A

NONE OF THESE:

member(cat, [cat | [dog | [pig | []]]]).
member(cat, [cat | [dog | [pig | []]]]).
member(cat, [cat | [dog | [pig | []]]]).

64
Q

What are the possible answers of the following goal (question)?

?- member([2, X], [[1, a], [2, m], [2, z], [3, p], [4, v]]).

A

X = m;

X = z;

65
Q

Given a Prolog pair [H | T], which of the following statements are correct?

A

If T is a list, then [H | T] is a list.

If T is not a list, then [H | T] is not a list.

66
Q

The following rules will trigger a singleton variable warning. How do you fix the problem?

count([], 0) :- !.
count([X | Tail], S) :- count(Tail, S2), S is 1+S2.

A

Change X to _.

67
Q

Which sorting algorithm has the best execution time in the worse case scenario?

A

Merge sort

68
Q

According to the quick sort algorithm, how can the pivot value be selected?

A

Any element in then current list can be selected.

69
Q

In quicksort, what does the following clause mean?
split(Pivot, [X | T], [X | Le], Gt) :- X =< Pivot, split(Pivot, T, Le, Gt).

A

If x=< Pivot, add X to the beginning of Le.

70
Q

Given the following set of the recursive rules, what clause represents the size-(N-1) problem?

bar([],[]).
bar([X | L], F) :- bar(L, G), append(G, [X], F).

A

bar(L, G).

71
Q

Given the “Duplicate-Removal” rules below:

drm([], []) :- !.
drm([Head | Tail], NL) :- member(Head, Tail), drm(Tail, NL), !.
drm([H1 | T1], [H1 | T2]) :- drm(T1, T2).

Which line of the code actually excludes an element from the result list?

A

drm([Head | Tail], NL) :- member(Head, Tail), drm(Tail, NL), !.

72
Q

What does the following set of the recursive rules do?

bar([], []).
bar([X | L], F) :- bar(L, G), append(G, [X], F).

A

Reverse a list

73
Q

A backtracking point is a point, from which the Prolog runtime will re-start its search,…

A

if the current search fails.

74
Q

Cut (!) is a clause that always returns…

A

true.

75
Q

A cut interferes with a recursive process by removing the recursive exit points.

A

False.

76
Q

Using a cut (!) in a rule can…

A

reduce possible answers of a question that is asked using the rule.

77
Q

Prolog allows to dynamically adding facts into the factbase through the following operations.

A

Asserta(fact).

Assertz(fact).

78
Q

We can use a repeat clause to form a loop. What clause do we typically use to return to the repeat point?

A

fail

79
Q

Which of the following mechanisms can make Prolog search more efficient?

A

Place the stopping condition (facts) before recursive rules.

Use tail recursion if possible.

Use cut if only one answer is needed.