\documentclass[twoside]{article}
\title{Examination 2}
\author{CSC140 Foundations of Computer Science}
\date{19 February 2016}
\usepackage{amsmath}
\usepackage{listings}
\lstset{language=java}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt}
\newenvironment{answer}
{\vspace*{0.2cm} \rule{12cm}{0.04cm} \vspace*{0.2cm}}
{\vspace*{0.2cm}}
\begin{document}
\maketitle
\begin{enumerate}
\item In your lifetime, technological innovation has made it
possible for a young person to reach an audience of
millions overnight.
Sometimes the popularity of an idea surprises
its author.
Sometimes the publication of creative work on the Web
opens the doors to new careers.
Which very popular Web sites did Matt Harding,
Rob Malda, and Eric Weisstein create?
\begin{answer}
Write your answer here.
\end{answer}
\item In what sense are all programming languages equivalent?
\begin{answer}
Write your answer here.
\end{answer}
\item What is one distinguishing feature of the Java programming language?
\begin{answer}
Write your answer here.
\end{answer}
\item Let us suppose that the speed of the Starship Enterprise depends
upon the rate at which matter and antimatter are fed into the
warp engines and on the age of the reactor's dilithium crystals.
Write a stub method that computes the ship's speed (expressed as a
warp factor---a value
between 1.0 and 10.0, such as 5.2), given the
rate of consumption of matter and antimatter (a fraction
of the maximum rate---its value will be a number between 0.0 and 1.0) and the age of
the crystals (measured as a whole number of years).
\begin{answer}
\begin{lstlisting}{}
// Write your answer here.
\end{lstlisting}
\end{answer}
\item The sequential search algorithm is $O(n)$. The binary
search algorithm is $O(\log n)$. What is the meaning of this difference?
\begin{answer}
Write your answer here.
\end{answer}
\item Avogadro's constant is the number of atoms or molecules in a mass
of specified size.
\begin{align*}
N_A & \approx 602 200 000 000 000 000 000 000
\end{align*}
Although it is a whole number, its value should be
stored in a \lstinline+double+ variable rather than an \lstinline+int+
variable. Why?
\begin{answer}
Write your answer here.
\end{answer}
\item Rewrite this method with a \lstinline+for+ loop
in place of the \lstinline+while+ loop.
\begin{lstlisting}{}
int fun( int n ) {
int i = 0;
int sum = 0;
while( i < n ) {
sum += 2 * i + 1;
i++;
} // while
return sum;
} // fun( int )
\end{lstlisting}
\begin{answer}
\begin{lstlisting}{}
// Write your answer here.
\end{lstlisting}
\end{answer}
\item Write a method that computes an approximation of $\sin \theta$ using
this formula: $\sin( \theta ) \approx \theta - (\theta^3/6)$
\begin{answer}
\begin{lstlisting}{}
// Write your answer here.
\end{lstlisting}
\end{answer}
\item What does this method do?
\begin{lstlisting}{}
void f( int i, int j, int [] data ) {
int temp = data[i];
data[i] = data[j];
data[j] = temp;
} // f( int, int, int [] )
\end{lstlisting}
\begin{answer}
Write your answer here.
\end{answer}
\item What does this method do?
\begin{lstlisting}{}
int g( int i, int [] data ) {
int result = i;
for( int j = i + 1; j < data.length; j++ ) {
if( data[j] < data[result] ) {
result = j;
} // if
} // for
return result;
} // g( int, int [] )
\end{lstlisting}
\begin{answer}
Write your answer here.
\end{answer}
\item What does this method do?
\begin{lstlisting}{}
void h( int [] data ) {
for( int i = 0; i < data.length; i++ ) {
int j = g( i, data );
f( i, j, data );
} // for
} // h( int [] )
\end{lstlisting}
\begin{answer}
Write your answer here.
\end{answer}
\item Write a method that computes the sum of the trace of
a matrix. The trace of a matrix is the sum of the elements
found on the main diagonal of the matrix.
\begin{align*}
M & =
\left[ \begin{array}{rrr}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array} \right] \\
\mathrm{trace}(M) & = 1 + 5 + 9 \\
& = 15
\end{align*}
\begin{answer}
\begin{lstlisting}{}
// Write your answer here.
\end{lstlisting}
\end{answer}
\end{enumerate}
\end{document}