You are on page 1of 9

CS 341 / Fall 2016

Practice Final Exam


Working with C++

1) Consider the program shown on the right.

a) What is the type of x in function F?

b) What does the program output?

c) Function F uses a range for loop. Rewrite using an


iterator-based loop.

2) Suppose V is a vector of ints. Sort V into ascending order by calling the built-in C++ sort algorithm. Recall
that sort is defined as sort(first, lastp1, compare) where first is an iterator denoting the first
element, lastp1 is an iterator denoting one beyond the last element, and compare is a lambda expression
returning a boolean denoting whether one element should come before another (true if so, false if not).

Prof. Joe Hummel Page 1 of 9


C++ memory model

3) Suppose the built-in vector class defines a handle containing 3


members: (1) a pointer to a dynamically-allocated array, (2) the
vectors size (number of elements in array), and (3) the vectors
capacity (total number of locations in array). Suppose the initial
capacity of a vector is 8, and it doubles in size when full.

Based on this information, execute the program shown to the right


(which is identical to the program on the previous page). Stop
when execution reaches the range for loop, and draw the state of
the stack and heap. Be as accurate as possible; when a value is
unknown, use ?.

Prof. Joe Hummel Page 2 of 9


Working with F#

4) Consider the program shown on the right.

a) Is function F tail recursive? Yes or No.

b) Is function F a curried function? Yes or No.

c) What type does F# infer for function F?

d) What does this program output?

5) Consider the program above. It has no type declarations, yet it is strongly-typed type errors are caught
& reported (vs. the all-too-common silent crashes you get from incorrect C programs). Discuss how type
errors are detected in F#. In particular, discuss whether they are caught at compile-time or run-time, and
how.

Prof. Joe Hummel Page 3 of 9


F# memory model

6) F# never makes a copy of a list unless it has to in order to


preserve immutability.

Based on this information, execute the program shown to the


right (which is identical to the program on the previous page).
Stop when execution reaches Fs base case, and draw the state of
the stack and heap. Be as accurate as possible; when a value is
unknown, use ?.

Prof. Joe Hummel Page 4 of 9


Higher-order Programming

map is a powerful, higher-order function, and an important tool for functional programming. In F# map
is available as List.map F L. In C++ its available as std::transform(first, lastp1, firstout, F), where F is applied
to each element in the iterator range [first, lastp1) starting with first and up to but not including the
element denoted by lastp1. The results are stored in memory starting at the location denoted by the iterator
firstout; it is assumed that firstout can be advanced without error to store the results.

7) Youre programming in C++, and have an integer vector V along with an integer C. You need to multiply
every element of V by C. You could write a loop to do that, but today youre feeling functional. Show below
how you would call std::transform to perform this computation, using a lambda expression for F. [ Hint: store
the results back into V. ]

vector<int> V = ;
int C = ;

std::transform(

8) The operation you performed above is scalar multiplication. Write this function in F# using List.map.

let ScalarMultiply C L =
List.map

9) Given the usefulness of map, its only natural that there are versions that operate on 2 lists, 3 lists, and
more. For example, in F#, map2 is available as List.map2 F L1 L2. The function F takes 2 arguments one
element from each list and returns a single value; map2 applies F across the corresponding elements of L1
and L2, and collects the return values into a list that is ultimately returned.

An example is vector multiplication: [1; 2; 3] * [4; 5; 6] = [4; 10; 18]. Write the function VectorMultiply in
F# using List.map2. Assume L1 and L2 are the same length.

let VectorMultiply L1 L2 =
List.map2

Prof. Joe Hummel Page 5 of 9


Higher-order programming, part 2

10) Now implement the F# function map2. You must write this function in a tail-recursive manner, using only
the most basic F# features: if, match, .Head, .Tail, :: and @. No loops, no mutable variables, no other
functions from F# or .NET. Assume the two lists are the same length, though possibly empty.

[ Note: define any helper functions you may need below map2; well assume they were defined above map2 as
required by the F# compiler. ]

let map2 F L1 L2 =

Prof. Joe Hummel Page 6 of 9


Declarative programming with SQL

11) In the Northwestern Hospital database, there is a table named Doctors. Every doctor has a unique
DoctorID, a PagerNumber, a FirstName, a LastName, and a Specialty (Heart, OB/GYN, Brain, etc.).
Answer the following questions using an SQL query:

a) How many doctors are there in each specialty? Retrieve the specialty and the # of doctors; the output
should be in descending order by the # of doctors. If 2 specialties have the same # of doctors, output
in ascending order by specialty.

b) Theres another table named NightCall that lists the doctors able to work at night. The NightCall table
contains a NightCallID and a DoctorID; the DoctorID is a foreign key. Retrieve the first and last name of
every doctor that is able to work at night. Output should be in ascending order by last name; if 2
doctors have the same last name, then output in ascending order by first name.

Prof. Joe Hummel Page 7 of 9


Declarative programming and N-tier design

12) [ This question spans 2 pages ] Suppose the Netflix database contains an additional table, Users:
Users
UserID LastName FirstName Email
822109 Parker Robert parker@gmail.com
837488 Bashi Pooja pbashi@hotmail.com
. . . .
. . . .

Add a GetUser(int userid) method to the Business Tier to lookup a user by their user ID, returning the users
info in the form of a User object. The first step is to define the User class do that below. Wait to define the
GetUser( ) method on the next page.

class User
{

}
// question continued on next page:

Prof. Joe Hummel Page 8 of 9


Assume a typical Data Access Tier that can execute well-formed SQL Select queries via ExecuteScalar(sql) and
ExecuteNonScalar(sql) methods. The ExecuteScalar function returns an object, while ExecuteNonScalar
returns a DataSet.

class BusinessTier
{
private DataAccessTier datatier; // data tier to execute SQL:
.
.
.
public User GetUser(int userid)
{

Prof. Joe Hummel Page 9 of 9

You might also like