Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CSE 240 Retrospective Practice Exams 2024-2025 Guide, Exams of Programming Languages

CSE 240 Retrospective Practice Exams 2024-2025 Guide

Typology: Exams

2024/2025

Available from 06/07/2025

Fortis-In-Re
Fortis-In-Re 🇺🇸

1

(1)

2.3K documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE 240 Retrospective Practice Exams 2024-2025 Guide
1 / 13
1. 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.
2. 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).
3. We apply an anonymous variable in the definition of a rule if we: do not want
to obtain a return value to the variable.
4. The key idea of logic programming paradigm is to solve a problem by
describing: what the problem is.
5. How many arguments can be defined in a fact?: n, where n >= 0.
6. What notation does Prolog use for expressing arithmetic operations?: infix
notation
7. A Prolog variable represents a: constant.
8. A Prolog program finds a solution by: searching the built-in database consist-
ing of facts and rules.
9. The scope of a Prolog variable is: within a single fact, rule, or query.
10. A prolog program usually contains: (Select all that apply.): facts. rules.
queries.
11. 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
12. Given these facts:
is_dessert(cookie).is_dessert(ice_cream).is_dessert(pie).is_dessert(cheese-
cake).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).
13. 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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download CSE 240 Retrospective Practice Exams 2024-2025 Guide and more Exams Programming Languages in PDF only on Docsity!

  1. 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.
  2. 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).
  3. We apply an anonymous variable in the definition of a rule if we: do not want to obtain a return value to the variable.
  4. The key idea of logic programming paradigm is to solve a problem by describing: what the problem is.
  5. How many arguments can be defined in a fact?: n, where n >= 0.
  6. What notation does Prolog use for expressing arithmetic operations?: infix notation
  7. A Prolog variable represents a: constant.
  8. A Prolog program finds a solution by: searching the built-in database consist- ing of facts and rules.
  9. The scope of a Prolog variable is: within a single fact, rule, or query.
  10. A prolog program usually contains: (Select all that apply.): facts. rules. queries.
  11. 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
  12. Given these facts:

is_dessert(cookie).is_dessert(ice_cream).is_dessert(pie).is_dessert(cheese- cake).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).

  1. 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.
  1. In a Prolog recursive rule, what components are required? Select all that apply.: Stopping condition Size-M problem, where M < N. Size-N problem.
  2. How can recursive algorithms be converted to a more iterative form in Scheme?: By adding extra parameters to the recursive call that mimic state vari- ables in a loop.
  3. What aspect of computation in a recursive algorithm leads to slow perfor- mance?: Deferring work until computing the recursive result.
  4. 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 (recur- sive-add)?: It prepares the input (by padding them to the same length), and sets the initial parameters for recursion.
  5. 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-re- cursive 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
  6. 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
  1. Which of the following forms using a quote will return an error when run?: ('+ 2 3) (define a a)
  2. Given this Scheme procedure:

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

  1. What is the correct method for constructing a pair in Scheme?: (cons 1 2)
  2. 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
  3. Which of the followings is a Scheme pair? Select all that apply.: '(x. y) '(x. x) '(() ())
  4. 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.
  5. Given this expression, what is the expected result? (car (cdr '(1 2 3 4 5)): 2
  6. 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).
  7. In Scheme, the form (symbol-length? 'James) will return:: an error message
  8. Convert the following expression into prefix-p notation (a Scheme state- ment):
  • 5 * (2 + 1/2) + 40: (+ (* (- 5) (+ 2 (/ 1 2))) 40)
  1. Functional programming languages do NOT allow us to define: variables whose value can be modified
  2. Convert the following expression into prefix-p notation (a Scheme state- ment): 10 + (5 - 3) + 2 / 4: (+ 10 (- 5 3) (/ 2 4))
  3. What notation requires parentheses in order to correctly define the order of computation?: infix notation
  4. One of the major differences between the imperative and functional pro- gramming languages is that the functional programming languages do NOT (in general!)...: have side-effects.
  5. Which of the following is a valid Scheme expression?: (* 9 (/ (- 4 2) 7))
  1. 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.

  1. How would Scheme implement a function such as: for (i = 1; i< 100, i++) {sum = sum + i;}: With recursion.
  2. Given an expression: x1 + x2 + x3 + x 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
  3. 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"
  4. 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"
  5. 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.
  6. Which of the following expression will return false (#f)?: (number? #\7)
  7. The display form (display "Hello") violates the functional paradigm be- cause it: does not have a return value.
  8. The semantics of multiple inheritance becomes complex and error prone, if the base classes have: overlapped members.
  9. If a class needs to inherit from multiple parent classes, the parent classes must: be inherited as virtual classes.
  10. 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;
  1. What are the key features of object orientation in programming languages? Select all that apply.: Dynamic memory allocation Encapsulation of state
  2. Using the principle of information hiding in OOP, in an array-implemented queue class, what members should be declared as "public" members?: en- queue and dequeue functions
  3. C++ allows a programmer to create an object through (Select all that apply): declaration in compilation phase. the "new" operation during execution.
  4. You do not need to garbage-collect the memory, if it comes from (Select all that apply): static memory stack memory global memory
  5. If a function calls another function, the local variables in these two func- tions use the memory from: different stack frames.
  6. How is Java's garbage collection implemented?: It uses a reference counter to indicate if an object is still referenced by any variable.
  7. Which C/C++ operations will acquire memory from heap? Select all that apply.: new malloc
  8. 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
  9. 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.
  10. A piece of memory must be explicitly garbage-collected, if it comes from- : heap
  1. What is the best way of deleting an array created by "p = new Struct- Type[size];" in C++?: delete[] p;
  2. What memory must be garbage-collected by a destructor?: heap memory created in the constructors in the same class
  3. 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.
  4. We need to write a destructor for a class, if: heap memory is used in the constructor of the class.
  5. Given this snippet of code, identify the stopping condition and the corre- sponding 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;
  6. 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)
  7. 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);
  8. A merge-sort is typically implemented using: a function with two recursive calls.
  9. A tail-recursive function is structurally equivalent to: a while loop

source item size number of items

  1. When do we need to use fflush() or cin.ignore()?: Between a formatted input and an unformatted input
  2. 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
  3. 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;
  4. 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.
  5. 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);
  6. 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;
  1. 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 vari- ables.
  2. Which of the following statements is true?: A constant cannot be used as the actual parameter for pass-by-alias.
  3. 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
  4. 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
  5. What type of values are stored within a pointer type?: a integer value, which represents the address.
  6. 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
  7. 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
  8. Given the declaration: char A[3] = {'C', 'a', 'r'}, *p = A; what statement prints character a?: printf("%c", *(++p));

int tail = 0; Which statement can read a name into the name field of the structure?: - scanf("%s", contactbook[tail].name);