CSE 240 Final Exam Review

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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 a integer"))) (guess 10)

"I'm a number"

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

#\o

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))

'(2 4 6)

Which of the followings is a Scheme pair? Select all that apply.

'(x . y) '(x . x) '(() ())

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

('+ 2 3) (define a a)

Which of the following is a valid Scheme expression?

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

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

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

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

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

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))

(1 0 1 0 1 0)

Given the Scheme code, answer the following questions. ((lambda (x) ((lambda (x y) (+ x y)) 4 (* 6 x))) 3) (1) The value to be passed to the parameter y is 18 . (2) The final output of the piece of code is 22 .

(1) 18 (2) 22

Given the Scheme code, answer the following two questions. ((lambda (x) ((lambda (x y) (+ x y)) 5 (* 7 x))) 3) (1) The value to be passed to the parameter y is 21 . (2) The final output of the piece of code is 26 .

(1) 21 (2) 26

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 (1) What line of code defines the stopping condition and the return value? Line 2 (2) What line of code contains the size-M problem, where M < N? Line 3 (3) What lines of code define the step that construct the solution to size-N problem? Choose. Lines 3 and 4

(1) Line 2 (2) Line 3 (3) Line 3 and 4

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

(4 6 8 10 12 14)

What statements contain non-functional features of Scheme? Select all that apply.

(begin (write x) x) (display x)

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

(cadr '(1 2 3 4 5))

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

(cons 1 2)

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

(number? #\7)

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

2

What type of values can a throw-statement throw (return)? Primitive type. String type. Object types.

All of the above.

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

Any element in the current list can be selected.

Prolog allows to dynamically adding facts into the factbase through the following operations. Select all that apply.

Asserta(fact). Assertz(fact).

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

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

How can recursive algorithms be converted to a more iterative form in Scheme?

By adding extra parameters to the recursive call that mimic state variables in a loop.

What programming language features are supported in Prolog? Select all that apply.

Call-by-value Call-by-reference

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

Error Message

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

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.

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

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

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

It does not have a stopping condition.

Consider the main procedure for doing the binary addition: (define binary-add (lambda(L1 L2) (let ((len1 (length L1)) (len2 (length L2))) (if (> len1 len2) (binaryadd L2 L1) (if (< len1 len2) (binary-add (append '(0) L1) L2) (recursive-add (append '(0) L1) (append '(0) L2) 0))) ) )) Why is this procedure required before the main recursive one (recursive-add)?

It prepares the input (by padding them to the same length), and sets the initial parameters for recursion.

Given the following snippet of C++ code: string name = "Hello";ofstream myFile; myFile.open(myFile);myFile << name;myFile.close(); What does it do?

It saves the word Hello into a file in the file system.

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

Merge Sort

What goal will return a "no" answer? member(cat, [cat | [dog | [pig | []]]]). member(cat, [cat | [dog | [pig | []]]]). member(cat, [cat | [dog | [pig | []]]]).

None of them

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?

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

Which of the following mechanisms can make Prolog search more efficient? Select all that apply

Place the stopping condition (facts) before recursive rules. Use tail recursion if possible. Use cut if only one answer is needed.

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

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

Which sorting algorithm has the best average execution time?

Quick Sort

What does the built-in predicate cut (!) do?

Remove all existing backtracking points.

What does the following set of the recursive rules do? bar([ ],[ ]). bar([X | L],F) :- bar(L, G), append(G, [X], F).

Reverse a list.

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:

Scheme

In a Prolog recursive rule, what components are required? Select all that apply.

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

Consider a path from the root to a leaf of a class tree based on the inheritance. Which class has the most class members?

The class at the leaf of the tree.

What statement is accurate and correct for Scheme lists and pairs?

There exists a list that is not a pair.

Which of the following are typical advantages of using a REPL for software development?

They let you interact with a program after it has populated the environment to understand what is happening. They let you experiment with small pieces of code to understand how they work.

What is the best way of writing a Prolog rule that gives you all the possible coin combinations adding to a dollar?

Use membership rules and arithmetic conditions.

What are the possible answers of the following goal (question)? Select all that apply. ?- member([2,X], [[1,a], [2,m], [2,z], [3,p], [4,v]]).

X = m; X = z;

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

[cat, dog, pig]

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

an unnamed/lambda procedure.

Filter is a higher-order function that

applies a predicate to all elements of a list.

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

bar(L, G).

Consider C++'s typing system. C++ uses (Select all correct answers)

both value semantics and reference semantics for its primitive types . both value semantics and reference semantics for its object types.

The statement "a function is a first-class object" means that a function

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

Defining a virtual function in class means that the function

can be redefined in its child classes.

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

compute x!

If the relation between two C++ classes can be best described as "is-a" relation, we should

derive one class from the other (inheritance).

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

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

Given the "Dupliate-Removal" reules 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?

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

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)))

error message

A prolog program usually contains: (Select all that apply.)

facts, rules, and queries.

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

fail

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 help summarize all desserts that contains fruits:

fruit_dessert(X) :- is_dessert(X), is_fruit(Y), contains(X, Y).

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

have side-effects.

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

if the current search fails.

What notation does Prolog use for expressing arithmetic operations?

infix notation

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

infix notation

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

instantiation_error

Which of the following predicates in logic programming matches most closely with this statement? "Bill likes chocolate and cookies."

likes(bill, chocolate), likes(bill, cookies).

A let-form in Scheme defines a set of

local names.

A circular definition of a rule

may cause an infinite loop for certain goals.

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? Select all that apply.

move(M, S, D, C) move(M, C, S, D)

How many arguments can be defined in a fact?

n, where n >= 0.

It does not cause memory (storage) overhead to create multiple

names (alias) for a variable.

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

no

A higher order function is a function that takes the

operator of a function as an argument.

The semantics of multiple inheritance becomes complex and error prone, if the base classes have

overlapped members.

A Prolog variable represents a

place holder.

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

probably a list. Really: we shouldn't know or care.

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

procedures are first class objects.

Using a cut (!) in a rule can

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

What type casting mechanism should be used with caution as it may truncate a larger object to a smaller one while casting an object of one class to an object of a different class?

reinterpret_cast

A Prolog program finds a solution by

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

What type casting mechanism should be used if you want to cast an integer value to a double value?

static_cast

A throw statement is associated with a specific exception handler through the

the data type implied by the throw value.

If you declare a pointer to the object of the parent class and use the pointer to access the object of a child class, you can access

the members that exist in the parent class.

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? Select all that apply.

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

The Scheme form (char? #\5) will return

true (#t)

Cut (!) is a clause that always returns

true.

The try-throw-catch exception handling mechanism is at the level of

user program.

Functional programming languages do NOT allow us to define:

variables whose value can be modified.

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

what the problem is.

The scope of a Prolog variable is

within a single fact, rule, or query.

If A is the base class and B is a class derived from A, and x and y are pointers to objects of A and B, respectively, which assignment statement can pass the compiler check?

x = y;

Given the following class definition and the variable declaration: class employee { public: char *name; long id; }; class manager { public: employee empl; char* rank; } x; Which of the following assignment statement is correct?

x.empl.id = 12345;


संबंधित स्टडी सेट्स

East Asian Art & Architecture Quiz 2

View Set

Macro Gross Domestic product Module 4

View Set

Chapter 4: States of Consciousness Quiz #4

View Set

Splunk Advanced Power User Part 1

View Set

You're welcome B*tches!! A Sociology of the Family Inquisitive

View Set