Slide 1

Slide 1 text

jgs CSE 240 Introduction to Programming Languages Lecture 27: Final Review Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

jgs The following slides shows some examples related to some topics This is NOT a comprehensive list of topics Topics in the exam can be found Weeks 1 to 8

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 3 jgs Topics § Programming Languages Concepts § C § C++ § LISP § Prolog 2 or 5 questions per topic. Most of them involve programming

Slide 4

Slide 4 text

jgs Test Yourselves C/C++

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 5 jgs C recursion #include void fun(int x) { if (x>0) { printf("%d", x); fun(x-1); printf("%d", x); } } int main() { fun(5); }

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 6 jgs C/C++ pointers § What delete expression correspond to the expression ptr=new int[100]

Slide 7

Slide 7 text

jgs Test Yourselves Lisp

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 8 jgs LISP § What is the output of the following code? (let ((x '(1 2)) (y '(9 10))) (print (+ (first x) (first y)) ) )

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 9 jgs LISP 1. Create a function that receives a list and return the lowerst value 2. Create a function that receives a list and an integer and return “true” if the number exist in the list 3. Create a function that receives a list and return “true” if the list contains a palindrome. such as (a b c d e d c b a) 4. Create a variable in Lisp to store a tic-tac-toe, such as x E x 0 X 0 E x E

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 10 jgs LISP 5. Create a function that receives the variable with the tic-tac-toe and return “true” if there is an empty space (an “E” letter). 6. The following list represent a tree '(a (b () ()) (c (d () () ) (e () ()) ) ) Create a function (recursive) that traverse the tree and print all the elements as A B C D E. 7. What is printed by the following code? (setf route '(boston cambridge lincoln concord)) (print (first (rest route)))

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 11 jgs LISP 8. What is the output of the following code? (let ((x '(1 2)) (y '(9 10))) (print (+ (first x) (first y)) ) ) 9. What is printed by the following code? (defun fun(n) (if (= n 0) () (fun (- n 1)) ) n ) (print (fun 5)) 10. What is equivalent code in C++ for the following LISP statement (if (< 1 2) (+ 3 4) (- 5 6) )

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 12 jgs LISP 1. A function test-min that returns the lower value in a List of numbers (test-min L) 2. A function test-average that returns the average of all values in a List of numbers (test-average L) 3. A function test-fibonacci that prints the first n elements of the fibonacci series (test-fibonacci n) 4. A recursive function that returns the leftmost even element in a list of numbers (test-even L) 5. A functions that traverse a list and returns how many of its elements are 0 (test-count-zeros L) 6. A recursive function that return the N member of a list L (assuming that the elements are numbered from zero onwards): (test-get n L) 7. A recursive function that returns non-NIL if x is a member of the list L. (test-is-in-list x L)

Slide 13

Slide 13 text

jgs Test Yourselves Prolog

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 14 jgs Q1 What is the predicate logic expression equivalent to "Charlie owns a computer" owns(charlie, computer).

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 15 jgs Q2 instructor(john,cs365). instructor(mary,cs311). enrolled(danielle,cs365). enrolled(danielle,cs446). § Which query provides a list of all the students enrolled in cs365? enrolled(Who, cs365).

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 16 jgs Q3 If weather is cold, everyone likes it. Write this statement as a Prolog clause. likes(X, weather) :- cold(weather). %notice “weather” in lowercase

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 17 jgs Q5 § Given the following facts § cat(tom). § black_spots(tom). § dog(pluto). § white_spots(pluto) § Which rule defines in Prolog the following sentence: § "mary owns a Pet if it is a cat and it has black spots" owns(mary, Pet):- cat(Pet), black_spots(Pet).

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 18 jgs Q6 Which rule defines in Prolog the following sentence: "If someone owns something, he loves it" loves(Who, What):-owns(Who, What).

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 19 jgs Q7 1 2 3 4

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 20 jgs Q7 1 2 3 4

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 21 jgs Test Yourselves § A recursive function rec is defined as follows Write a set of rules in Prolog to define the solution to this function. ï î ï í ì ³ - - + - = £ = 3 ) 3 ( * ) 2 ( ) 1 ( 2 1 1 0 ) ( n n rec n rec n rec n n n rec

Slide 22

Slide 22 text

jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.