Fall 2001

CPSC 215
L01
FINAL EXAM

Name: _________________________________

Note: All questions and programs are about the Mathematica programming language.
Assume that the Mathematica kernel is restarted for each question.

Good Luck!

Department of Computer Science
The University of Calgary

CPCS 215 — Fall Session 2001

(Dr. Christian Jacob)
L01

FINAL EXAMINATION

December 11, 2001

Time: 120 Minutes

First Name, Last Name: ___________________________

Student ID:                   ______________

Instructions

({{({{\(1. \ Please\ write\ your\ name\ on\ the\ cover\ \(\(page\)\(.\)\)\)}, {\(2. \ Please\ write\ your\ name\ and\ ID\ on\ this\ page\)}, {3. Read the note on the first page!}, {4.  Read the questions carefully!}, {\(5. \ Write\ your\ answers\ in\ the\ provided\ \(\(spaces\)\(.\)\)\)}, {\(\(6.\)  \(THIS\)\(\ \)\(IS\)\(\ \)\(A\)\(\ \)\(CLOSED\)\(\ \)\(BOOK\)\(\ \)\(EXAM!\)\(\ \)\)}}), ({{          , ({{Q, Max P., Points}, {1, 10,  }, {2, 20,  }, {3, 10,  }, {4, 10,  }, {5, 10,  }, {6, 15,  }, {7, 15,  }, { , Sum,  }, { , Max,  }, { , Grade,  }})}})}})

(1) Inputs and Outputs: Multiple Choice

What output does Mathematica produce for the following inputs?

sin (((1 + (Sqrt[1 - x]))/(-(Sqrt[1 - x]) + 1)))

Output:
	□ 	Sin[((1 + (Sqrt[1 - x]))/(1 - (Sqrt[1 - x])))]
	□ 	No output is produced.
	□ 	((sin (1 + (Sqrt[1 - x])))/(1 - (Sqrt[1 - x])))

g[x] f (x)

Output:
	□ 	g[x] f (x)
	□ 	f x g[x]
	□ 	Syntax Error

[a, b, c] + [d, d, d]

Output:
	□ 	[a + d, b + d, c + d]
	□ 	{a, b, c} + d
	□ 	Syntax Error

{Range[5], {a, b, c, d, e}} // Transpose

Output:
	□ 	{{1, 2, 3, 4, 5}, {a, b, c, d, e}}
	□ 	{1  a, 2  b, 3  c, 4  d, 5  e}
	□ 	{{1, a}, {2, b}, {3, c}, {4, d}, {5, e}}

Reverse /@ {{a, b}, {c, d, d}, {e, f}}

Output:
	□ 	{f, e, d, d, c, b, a}
	□ 	{{e, f}, {c, d, d}, {a, b}}
	□ 	{{b, a}, {d, d, c}, {f, e}}

Map[h, 1 + 2  x + (x^2)]

Output:
	□ 	h[1,2  x,(x^2)]
	□ 	h[1]+h[2  x]+h[(x^2)]
	□ 	h[1+2  x+(x^2)]

MapAt[g, {{a, b}, {c, d}}, {{1, 1}, {2, 2}}]

Output:
	□ 	{g[{a, b}], g[{c, d}]}
	□ 	{{g[a], b}, {c, g[d]}}
	□ 	{g[a], g[b], g[c], g[d]}

Function[x, (x > 10) && (x < 50)][12]

Output:
	□ 	True
	□ 	False
	□ 	12

Select[Range[10], 2 # > 10 &]

Output:

	□ 	{1, 2, 3, 4, 5}
	□ 	{6, 7, 8, 9, 10}
	□ 	True

f[a + b - c, (d^2)] // FullForm

Output:

	□ 	f[a + b - c,(d^2)]
	□ 	f[Plus[a, b, Times[-1, c]], Power[d, 2]]
	□ 	f[Plus[a,b,-c,(d^2)]]

(2) Filling in the Blanks

For the following commands, fill in the missing spaces (□) so that the following respective outputs are produced:

expr = (x^2) + x + 1;
\[Placeholder][expr, \[Placeholder]]

SetDelayed[
      f[\[Placeholder][y, Blank[]]], \[Placeholder][y, 
        2]]

?f

\[Placeholder][f, Range[2, 5, \[Placeholder]]]

\[Placeholder][h, \[Placeholder][g, 1 + 2  x - 5  y]]

\[Placeholder][Join[{1, 2, 3}, {a, b, c}], \[Placeholder]]

s[k_] := 
      Module[{t = {0}, i = 1}, \[Placeholder][i ≤ k, 
          t = Append[t, 
              i]; i++]; \[Placeholder]]

{s[3], s[4], s[5]}

myPlus[x_List, y_List] := \[Placeholder][x, 
          y];
\[Placeholder][x_Integer, y_Integer] := 
        Plus[x, y];
myPlus[x_  \[Placeholder], y_  \[Placeholder]] := 
        StringJoin[x, y];

    
{myPlus[1, 2], myPlus[{a, b}, {c}], 
      myPlus["str", "ing"]}

primesAndSquares[from_Integer, 
        to_Integer] := 
Module[{s = \[Placeholder][
              Range[from, to], PrimeQ]}, 
{s, 
            (s^2)} // \[Placeholder]]

primesAndSquares[2, 10]

equation[2 x - b, 0, 
        x] /. {equation[a_ x_ + b_. , \[Placeholder], 
            x_] :> \[Placeholder][a x, right - b, x]}

(3) Patterns

Assume we enter the following definition of a symbolic expression:

expr = f[(a^2), f[1, g[(b^2)]], b, (d^4), 3];

What is the output for the following Mathematica commands?

\(Position[expr, _\^\(\(2\)\(\ \)\)]\)

\(Position[expr, _\^_Integer]\)

Cases[expr, x_ /; NumericQ[x], Infinity]

Extract[expr, {{1}, {2, 2, 1, 2}, {4, 2}}]

Position[expr, b]

(4) Computing with Rules

Assume we enter the following definition of a symbolic expression:

expr = f[(a^2), f[1, g[(b^2)]], b, (d^4), 3];

What is the output for the following Mathematica commands?

expr /. {x_ /; NumberQ[x] :> t[x]}

expr /. {x_f :> t}

expr /. {f[x___, f[y___], z___] :> f[x, t, z]}

Map[# /. {b -> 1} &, expr]

Map[If[Length[#] > 1, "big", "small", "other"] &, 
      expr]

(5) Evaluation of Expressions

Consider the following two assignments:

myFunction[x_, y_] := 
      (x^2) + (y^2)
a = 1;
someFunction = myFunction;

Give a step-by-step description of how Mathematica evaluates the following expression:

someFunction[2 + 1, a + 3]

(1) Evaluation of the head:

myFunction[2 + 1, a + 3]

(2) Evaluation of each argument, from left to right:

myFunction[3, a + 3]

myFunction[3, 4]

(3) Application of user-defined definitions:

myFunction[3, a + 3]⟶ (3^2) + (4^2)⟶9 + 
      16⟶25

(6) Recursive Function Definition

Here is the mathematical definition of a function g:

g(0) = 1

g(1) = 2

g(n) = g(n - 1)*g(n - 2), for n > 1.

Give a recursive definition of this function g in Mathematica.

Note: '*' denotes the multiplication operator.

(7) Iterative Function Definition

Here is the mathematical definition of a function g (same definition as in (6)):

g(0) = 1

g(1) = 2

g(n) = g(n - 1)*g(n - 2), for n > 1.

Give an iterative definition of this function g in Mathematica using a Module[...] construct and a Do[...] loop.