Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS 361

Fall 2022

MIDTERM 1

Problem 1: Programming paradigms

1. Functional programming 

Cite 2 examples of functional programming languages.

What are 3 of the main features of functional programming languages?

Examples

Example 1

 

 

Example 2

 

 

Features

Feature 1

 

 

Feature 2

 

 

Feature 3

 

 

2. Logic programming

Cite 1 example of logic programming language.

What are 3 of the main features of logic programming languages?

Example

Example

 

 

Features

Feature 1

 

 

Feature 2

 

 

Feature3

 

 

Problem 2: Object-oriented languages

Explain and provide an ILLUSTRATIVE example to illustrate each of the 3 most important features of object-oriented languages. Using classes, A, B etc., parent, child, children etc. will give you 0. The example needs to be illustrative.

· Encapsulation

 

 

 

 

 

· Single and multiple inheritance

Single inheritance

 

 

 

 

 

Multiple inheritance

 

 

 

 

· Polymorphism (OF CLASSES NOT METHDODS)

 

 

 

 

 

Problem 3: Memory Management

1. Complete the table below. Describe each part of the memory used during the execution of a program.

Memory Part

Description

Ordered / Not Ordered

What is saved in that part of the memory?

What data structure is the most appropriate to be used to represent that part of the memory?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. Consider the code below.

function phil(int a, int b, int c)

begin

b := b + 5;

b := a + c + 4;

print a, b, c;

end

function main begin

int j := 10;

ink k := 15;

phil(j, j, j + k);

print j, k;

end

Draw the state of the memory using the specifications below and provide answers to the question. You will use the drawing conventions that we saw in class. Each step must be visible on the drawing. You will not get credit if you do not show the steps.

· All parameters are passed by reference.

· The language uses static scoping.

Memory

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

What are the values of j and k at the end of the execution of the program?

Problem 4: Evaluation of recursive functions

Let us consider the following function for x >= 1 and y >= 0:

f(x,y) = if x = 0 then 0 else f(x-1, f(x,y))

Is it true that the computation of f (1, 1) always terminates? Explain.

 

 

 

 

Problem 5: ”Older Languages”

We consider the following code snippets in C and Pascal.

C language

 

Pascal language

 

Compare Pascal, C and Java on the 3 provided criteria: 1) parameter passing; 2) garbage collection; and 3) Syntax.

 

Pascal

C

Java

Parameter passing

 

 

 

 

 

 

Garbage collection

 

 

 

 

 

Syntax

 

 

 

 

 

 

Problem 6: Garbage Collection

We consider the following memory snapshot.

 

(a) Trace the Mark and Sweep algorithm on the provided memory snapshot.

PHASE 1: MARK

 

 

 

 

 

 

 

 

 

PHASE 2: SWEEP

 

 

 

 

 

 

 

 

 


(b) Compare Mark-and-Sweep and Reference Counting over 3 criteria that you will determine.

Criteria

Mark-and-Sweep

Reference Counting