14
Name _____________________________________ PID ______________________ COMP 524 Spring 2018 Final Tuesday, May 1 This exam is open note, open book and open computer. It is not open people. You are to submit this exam through gradescope. Resubmissions have been enabled. Download the fillable pdf and add your name and PID. Remember that editing inside of a browser does not work. Save the file and then make sure that the save worked properly. If you are having trouble saving, remember that you can print to file and save as a pdf. Another option some students prefer is to work in a file editor and then cut and paste when you finish each question. Remember to save your work often and confirm that it is being saved. If you are unable to open the pdf, use a paper test. If you do that, you are responsible for scanning it and uploading the scanned pdf to gradescope before 7:30 pm. There are 10 questions and each is worth 10 points. Do not, however, be surprised if they do not all take the same amount of time. The test is designed for 1.75 hours; you have the full 3 hours to complete it. By submitting this exam, you are pledging that you have neither given nor received unauthorized help.

COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

Name _____________________________________ PID ______________________

COMP 524 Spring 2018

Final

Tuesday, May 1

This exam is open note, open book and open computer. It is not open people.

You are to submit this exam through gradescope. Resubmissions have been enabled.

Download the fillable pdf and add your name and PID. Remember that editing inside of a browser does not work. Save the file and then make sure that the save worked properly. If you are having trouble

saving, remember that you can print to file and save as a pdf. Another option some students prefer is to work in a file editor and then cut and paste when you finish each question.

Remember to save your work often and confirm that it is being saved.

If you are unable to open the pdf, use a paper test. If you do that, you are responsible for scanning it

and uploading the scanned pdf to gradescope before 7:30 pm.

There are 10 questions and each is worth 10 points. Do not, however, be surprised if they do not all take the same amount of time.

The test is designed for 1.75 hours; you have the full 3 hours to complete it.

By submitting this exam, you are pledging that you have neither given nor received unauthorized help.

Page 2: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

1. You are given the following functions in an unknown language. theNumber is a globallyknown integer variable.

function double(x integer) returns(integer) {

theNumber = theNumber*2;

return(2*x);

}

function incrment(x integer) returns(integer) {

theNumber = theNumber+1;

return(x+1);

}

function mystery (x integer, y integer, z integer) returns(integer) {

if (x < 20 ) {

return(y);

} else {

return(z);

}

}

You are to determine what the following snippet prints out assuming the following parameter evaluation rules:

(1) Left-to-right parameter evaluation, passing by value(2) Right-to-left parameter evaluation, passing by reference for variables,passing by value for expressions(3) Lazy evaluation

You are welcome to include traces of the evaluation in order to facilitate partial credit.

theNumber = 10;

Print (“Mystery is “,

mystery(theNumber,double(theNumber),increment(theNumber)),

“. The number is “, theNumber);

Page 3: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer
Page 4: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

2. Define and implement a function called weightedAvg, which computes the weightedaverage of lists of values (doubles). That is, you average each list and multiply it by itslength; sum those values; and divide the result by the sum of the number of elements ineach list.

For example, weightedAvg([ [20.0, 10.0, 0.0], [8.0, 2.0] ]) = ((3*10.0) + (2*5.0)) / (3 + 2) =8.0.

You can use the following functions in your weightedAvg function:

sum, which returns the sum of the elements in a list: sum([1.0, 2.0, 3.0]) => 6.0

length, which returns the length of a list: length([1.0, 2.0, 3.0]) => 3

map, which applies a function to elements in a list: map(length, [ [1.0], [2.0, 3.0], [4.0, 5.0, 6.0] ]) => [1, 2, 3]

You can use any syntax for defining a function and defining variables. You cannot use any loops (for, while, etc.) in your implementation.

Page 5: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

3. Given the following definition of shape class in Java, define two subclasses, Circle andSquare. Both subclasses needs to have the implementation of calculateArea method: prints the computed the area and prints the message at the

entry of the method. In the case of Circle, the message is “Area of a circle”. As forthe Square, the message should be “Area of a square”

calculatePerimeter method: prints the computed perimeter and prints the messageat the entry of the method. In the case of Circle, the message is “Perimeter of acircle”. As for the Square, the message should be “Perimeter of a square”

Circle class needs to have one member variable to hold the value of the input radius. Similarly, Square class should have a member variable to hold the value of the length of the side of the square. Feel free to declare/define additional methods & members you deem necessary. Assume that Math library is already imported.

public class Shape {

public Shape() {

}

public void calculateArea() {

System.out.println(“Area of a shape: “ + -1);

}

public void calculatePerimeter() {

system.out.println(“Perimeter of a shape: ” + -1);

}

}

public class Circle extends Shape {

public Circle(double radius) {

}

public void calculateArea() {

}

public void calculatePerimeter() {

}

}

public class Square extends Shape {

public Square(double side) {

}

public void calculateArea() {

}

public void calculatePerimeter() {

}

}

Using these classes, given an array of shapes named shapes that contains circle whose radius is 4, square whose length of the side is 2, circle with radius 3, and a shape, what is the output of the following snippet of code?

for(int i = 0; i < shapes.length; i++) {

shapes[i].calculateArea();

}

Page 6: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer
Page 7: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

4. Prolog puzzle: A special island is inhabited only by knights and knaves. Knights alwaystell the truth, and knaves always lie.

The following facts encode the fact that a knight is a knight and a knave is knave. Add facts or rules such that the following query is supported:“truth(says(knight,is(knight,knave)),true).”, which succeeds if a knight is telling the truth when he says he is a knave.

Write a query using the truth predicate with variables to test if anyone on the island can say that he is a knave.

truth(is(knight,knight),true).

truth(is(knave,knave),true).

truth(is(knight,knave),false).

truth(is(knave,knight),false).

Page 8: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

5. Create a tail recursive equivalence of this iterative pseudocode. Feel free to use any language or pseudocode. You may change or add parameters. Include a sample call to your function.

sum = sumArray(A); … integer function sumArray(A) {

s := 0 for (i := 1 to |A|) {

s := s + A[i]; } return s

}

Page 9: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

6. Implement a pipeline using Go’s channels and goroutines. The first goroutine should send the natural numbers (0, 1, 2, …) to a channel called naturals every 1 second (you can use the time.Sleep function), and the second goroutine should double all values received from the naturals channel to a channel called doubles. The main goroutine simply prints out all the values received from the doubles channel.

Here is the base program that you are to complete. You just need to write the two goroutines..

package main

import "fmt"

import "time"

func main() {

naturals := make(chan int)

doubles := make(chan int)

// first goroutine

go func() {

}()

// second goroutine

go func() {

}()

for {

fmt.Println(<-doubles)

}

}

Page 10: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

7. Suppose you are given the following definitions of classes.

class Person {

string Name;

void work() { ... }

void relax() { System.out.println(“Generic Relax”); }

}

class Student extends Person {

void prepFinals() { System.out.println(“Prep for Finals”);

}

void work() { ...; prepFinals(); ... }

}

class Instructor extends Person {

void gradeFinals() { System.out.println(“Grading Finals”);

}

void makeFinals() { System.out.println(“Making the Finals);

}

void work() { ...; makeFinals(); ... ; gradeFinals(); ... }

void relax() { System.out.println(“Hit Franklin Street”); }

}

Given two instance of Person, p0 and p1, declared as the following.

Person p0 = new Instructor(“Jane”);

Person p1 = new Student(“John”);

What is the output of the following snippet of code?

p1.work();

p0.work();

p1.relax();

p0.relax();

Page 11: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

8. Create two Haskell functions. The first is to raise a value to a given power and the second is to curry the function to raise a value to the cube. Specifically, the first function is to be called raisePower. The first parameter is to be the power and the second the value. You are to name the second function raiseCube and it is to only take the value.

Page 12: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

9. There exists a programming language that supports dynamic type checking, user-defined types, disjoint union variants and has a level of aspect-oriented capability that will catch type errors. The system will throw a type_error (system defined error) if you try to do an inappropriate function on a variant. You can assume that the data types date and time have been appropriately defined. The language only supports functions and automatically adds errors to the return value (making it a variant).

Here is the snippet of code for this language that defines the type and two variables and the handler that catches the type_error: // define the variant type cal_entry type cal_entry variant of { variant allday { label char(*), startDate date, endDate date

} variant hourly { label char(*), startDate date, startTime time, endDate date, endTime time

} }; // declare two variables of type cal_entry var event_to_add cal_entry; var current_event cal_entry; // if a cal_entry type error occurs, abandon the current process and propagate

the error on (type_error(cal_entry)) { return(type_error) } The intent of this code snippet is to check whether the event_to_add overlaps with the current_event (e.g., in the middle of a loop to see if it can safely be added to the calendar). The return value of the function is a list, where the first element is a 0 (failure) or 1 (success) and the rest of the list items are messages to be returned to the user – there may be more than one. The writer knows that event_to_add is of the hourly variant but mistakenly did not check which variant the current_event is. The on function will return out of the current function with the type_error value raised

if (event_to_add.startDate == current_event.startDate & event_to_add.startTime >= current_event.startTime & event_to_add.startTime < current_event.endTime) then {

return ([0,”New event starts during existing event”]); }

You are to rewrite this conditional statement assuming that there is no dynamic type checking, no aspect oriented capability to the language but that you can check the type of the variant by referencing variableName.variantType where variantType is a language construct. Your answer should handle the case where the current_event is an allday event. Do not worry about duration or end time, but assume that if the error is not found, then there is more code to be executed. You CANNOT assume that the language supports short-cut evaluation.

Page 13: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer
Page 14: COMP 524 Spring 2018 Final Tuesday, May 1 · Name _____ PID _____ COMP 524 Spring 2018 Final Tuesday, May 1. This exam is open note, open book and open computer

10. Given the following facts, write the prolog rules to determine if one person i an ancestor of another.

person(april). person(june). person(jim). person(jane). person(carol). person(sam). person(harry). person(lucy). mother(april,june). mother(jane,carol). mother(carol,lucy). father(april,jim). father(jane,sam). father(carol,harry).

The family tree looks like this: Lucy Harry \ / Carol Sam \ /

June Jim \ / April