CSE 240

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan 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"

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"

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

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

(cons '(1 2) '(4 5)) returns '((1 2) 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 snippet of code, what is the value of x after executing the last statement? int x = 10, *y; y = &x; y = y + 1; *y = 100;

10

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

2

When do we need to use fflush() or cin.ignore()?

Between a formatted input and an unformatted input

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 aspect of computation in a recursive algorithm leads to slow performance?

Deferring work until computing the recursive result.

What is one potential purpose for being able to return a procedure from a procedure?

It can be used to create another procedure that is bundled with some information.

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.

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 3 (2) What line of code contains the size-M problem, where M < N? [ Select ] (3) What lines of code define the step that construct the solution to size-N problem? Choose.

Line 2 Line 3 Lines 3 and 4

The complexity of searching a balanced binary search tree with n nodes is in the order of

O(lg n)

What is the time complexity of the insertion sort algorithm?

O(n*n)

What is the difference between pass-by-address and pass-by-alias?

Pass-by-alias involves one variable only, while pass-by-address involves two variables.

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

an error message

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.

If a class needs to inherit from multiple parent classes, the parent classes must

be inherited as virtual classes.

Consider an array, a linked list, and a binary search tree. Which data structure requires fewest comparisons on average to search an element stored in the data structure?

binary search tree

What keyword is used for defining an exception handler?

catch

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

compute x!

C++ allows a programmer to create an object through ___________ (Select all that apply)

declaration in compilation phase. the "new" operation during execution.

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

facts. rules. queries.

In the hanoi towers function, what part of the code represents step 4: Construction of size-n problem from size-(n-1) problems?

hanoitowers(n-1, S, D, M); hanoitowers(1, S, M, D); hanoitowers(n-1, M, S, D);

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.

We need to write a destructor for a class, if

heap memory is used in the constructor of the class.

Given this snippet of code, identify the stopping condition and the corresponding return (based on the four step approach discussed in class). void deleteList(struct contact* node) { if (node == NULL) return; else { deleteList(node->next); free(node); } }

if (node == NULL) return;

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

For functional programming languages, the scope of a local name

is in the body part of the declaration or definition.

Given the snippet of code: int x = 5; int bar(int j) { int *k = 0, m = 5;k = &m; return (j+m); } void main(void) { static int i =0; i++; i = bar(i) + x; } Which variables obtain their memory from the stack? Select all that apply.

k m j

How many arguments can be defined in a fact?

n, where n >= 0.

Which C/C++ operations will acquire memory from heap? Select all that apply.

new malloc

Consider the following snippet of code in a 32-bit computer. #define MAX 10 struct contact { char name[30]; char email[30]; int phone; }; struct contact A[MAX]; int tail = 0; Which statement can read a phone number into the phone field of the structure?

scanf("%d", &contactbook[tail].phone);

You do not need to garbage-collect the memory, if it comes from (Select all that apply)

static memory stack memory global memory

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

static_cast

How do you properly delete the first node of a linked list pointed to by head? Assuming that the linked list is not empty and temp is another pointer of the same type.

temp = head; head = head->next; free(temp);

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.

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.

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

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

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

21 26

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

3

Assume this is a 32-bit environment, what is the size of x? (HINT: Don't forget about padding.) struct Terminal { char name[30]; char location[32]; struct Terminal* next; } struct Terminal x;

68 bytes

What is the result of this expression (in Scheme)? (let ((x 10)(y 10))(- (* x y) y))

90

Which of the following statements is true?

A constant cannot be used as the actual parameter for pass-by-alias.

If you want to create a linked list of Container nodes, which can contain Publication node, Book node, Thesis node, and Report node, what type of pointer should be declared in the Container to point to all these nodes?

A pointer to Publication node

The reason that we use a buffer between the disk and the memory is _______

Disk access speed is low, but large blocks of data can be read from and written to the disk.

What are the key features of object orientation in programming languages? Select all that apply.

Dynamic memory allocation Encapsulation of state

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

Error Message

If you want to change the insertion sort function with a size-(n-1) problem, as discussed in class, to a merge sort function, then where do you need to make changes?

In the definition of size-m problem. In the code that constructs the solution of size-n problem from the size-m problem

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 C code: char a[2][3] = { { 'c', 'a', 't'}, { 'd', 'o', 'g'} };int i, j;for (i = 0; i<2 ; i++) {for (j = 0; j<3; j++)printf("%c", a[i][j]);} What will happen?

It prints: catdog

Given the following code char a[] = {'c', 'a', 't', '\0'}; char *p = a; while (*p != 0) { *p = *p + 1; printf("%c", *(p++)); } What will happen?

It prints: dbu

Given the following code char *p = "hello", *s = "this is a string"; p = s; printf("%s\n", p); What will happen?

It prints: this is a string

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.

How is Java's garbage collection implemented?

It uses a reference counter to indicate if an object is still referenced by any variable.

Which of the following coding practices can lead to a higher memory efficiency (i.e., fewer padding bytes) in defining a structure?

Keep the character type variables together.

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.

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.

Assume that Publication is the root class of an inheritance tree. You want to form a linked list of different publications in the inheritance tree, including Book, Report, Newspaper, etc. What is the best way to create a linked list using PublListNode and Publication classes?

The PublListNode class contains the Publication class.

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 '()?

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

What statement is accurate and correct for Scheme lists and pairs? (Be sure to consider the special case for an empty list!)

There exists a list that is not a pair.

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

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

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

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

Which of the following problems require us to define more than one size-m problem in it's recursive formulation?

Towers of Hanoi Mergesort

What is the best way of deleting all the nodes in a binary tree pointed to by the root pointer?

Traverse the tree and delete each node individually.

What is the best way of deleting a linked list of objects in C++?

Use a loop to delete every object in the linked list.

When will the buffer be created?

When the file operation fopen is performed.

When is padding required for a structure type variable?

When the structure contains a word-type variable, such as integer, float, and pointer, and the total number of bytes is not a multiple of four.

How would Scheme implement a function such as: for (i = 1; i< 100, i++) {sum = sum + i;}

With recursion.

The search algorithm will be more efficient if a binary search tree is

a balanced binary tree.

A merge-sort is typically implemented using

a function with two recursive calls.

What type of values are stored within a pointer type?

a integer value, which represents the address.

The function searching a binary search tree can be easily implemented using

a recursive function with two recursive calls.

A tail-recursive function is structurally equivalent to

a while loop

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?

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

When inserting a data into a binary search tree, the data should be inserted

at the position that keeps the entire tree a binary search tree.

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.

Given the following definition and declarations: #define size1 10 const int size2 = 20; char a1[size1]; char a2[size2]; which line is most likely to cause a compilation error?

char a2[size2];

A Prolog variable represents a

constant.

Given this snippet of code, identify the size-m problem (based on the four step approach discussed in class). void deleteList(struct contact* node) { if (node != NULL) { deleteList(node->next); free(node); } else return; }

deleteList(node->next);

What is the best way of deleting an array created by "p = new StructType[size];" in C++?

delete[] p;

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

derive one class from the other (inheritance).

What parameters are required when performing file operations fread and fwrite? Select all that apply.

destination source item size number of items

If a function calls another function, the local variables in these two functions use the memory from

different stack frames.

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

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

The display form (display "Hello") violates the functional paradigm because it

does not have a return value.

Using the principle of information hiding in OOP, in an array-implemented queue class, what members should be declared as "public" members?

enqueue and dequeue functions

A piece of memory must be explicitly garbage-collected, if it comes from

heap

What memory must be garbage-collected by a destructor?

heap memory created in the constructors in the same class

The data stored in a binary search tree is sorted, if the tree is traversed in

inorder

The purpose of the scope resolution operator is to allow a function to be

laced outside the class.

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

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

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.

Given the information below, how do you insert a new node, p, to the beginning of a linked-list. Assume head is pointing to the first node in the linked-list. Make sure that no nodes are lost. struct Terminal { char name[30]; char location[32]; struct Terminal* next; } *head, *p;

p->next = head; head = p;

Given the snippet of codes, identify the passing mechanism used for y (in func). void func(int *x, int &y) { *x = *x + y; y = 2; }

pass-by-alias

What data types in C have the same data range, assuming that the computer used is a 32-bit computer?

pointer type and unsigned int

Given the declaration: char A[3] = {'C', 'a', 'r'}, *p = A; what statement prints character a?

printf("%c", *(++p));

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

Assume pointer variable p points to node x of a linked list, and node x's next pointer points to node y. What does free(p) operation mean?

return the memory held by x to the free memory pool.

Consider the following snippet of code in a 32-bit computer. #define MAX 10 struct contact { char name[30]; char email[30]; int phone; }; struct contact A[MAX]; int tail = 0; Which statement can read a name into the name field of the structure?

scanf("%s", contactbook[tail].name);

A Prolog program finds a solution by

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

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 try-throw-catch exception handling mechanism is at the level of

user program.

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

value semantics for its primitive types (such as integer and float) only. reference semantics for its object types only.

Functional programming languages do NOT allow us to define

variables whose value can be modified

Given this snippet of code, identify the size-n problem (based on the four step approach discussed in class). void deleteList(struct contact* node) { if (node != NULL) { deleteList(node->next); free(node); } else return; }

void deleteList(struct contact* node)

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 information below, how would you access the name for a terminal node pointed to by x? struct Terminal { char name[30]; char location[32]; struct Terminal* next; }; struct Terminal *x;

x->name;

Given this snippet of codes, what is the expected output? void func(int *x, int y) { *x = *x + y; y = 2; } void main() { int x = 10, y = 10; func(&x, y); printf("x: %d, y: %d", x, y); }

x: 20, y: 10


Set pelajaran terkait

Chapter 15: Drugs Affecting Inflammation and Infection-PHARM

View Set

Medication Safety & Quality Improvement

View Set

prepU ch 48 Assessment and Management of Patients with Obesity

View Set

Chapter 15 Absolutism and Constitutionalism

View Set

Accounting - Principle of Auditing Chapter 3 Professional Ethics

View Set