%%% UNCOMMENT THIS TO PRINT AN ANSWER KEY (and comment out the other \documentclass)
%\documentclass[11pt,addpoints,answers]{exam}
%\newcommand{\SOLUTION}{ \textcolor{red}{-- SAMPLE SOLUTION --} } % TODO: uncomment this line to show SOLUTION LABEL
%\newcommand{\CORRECT}{ \textcolor{red}{CORRECT}} 

%%% UNCOMMENT THIS TO PRINT AN EXAM (and comment out the other \documentclass)
\documentclass[11pt,addpoints]{exam}
\newcommand{\SOLUTION }{Lecture Handout } % TODO: uncomment this line to so no solution label is included
\newcommand{\CORRECT}{} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%    TODO: Change HANDOUT VARIABLES for EACH HANDOUT
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\handoutname}{Shadowed Fields in Java }      % 1-primary          2-secondary          3-tertiary
\newcommand{\handoutcolor}{Yellow}   %  Yellow-primary  Green-secondary   Buff-tertiary
\newcommand{\weeknumber}{11}   % 1-primary          2-secondary          3-tertiary
\newcommand{\week}{Week \weeknumber }               %  P-primary         S-secondary          T-tertiary

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%    TODO: Change HANDOUT VARIABLES for EACH SEMESTER and COURSE OFFERING
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\semester}{Spring }
\newcommand{\yr}{2015 }   % \year was already defined elsewhere, but could not use
\newcommand{\lecA}{Lec 001\: Deppeler 800TR}
\newcommand{\lecB}{Lec 002\: Deppeler 100TR}
\newcommand{\instructorfull}{Deb Deppeler}
\newcommand{\instructor}{Deppeler} % fits better in header and footer
\newcommand{\coursenumber}{302}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setlength{\parindent}{0pt}
\usepackage[utf8]{inputenc}
\usepackage{changepage}
\usepackage{multicol}
\usepackage{graphicx}
\usepackage{latexsym}
\usepackage{textcomp}
\usepackage{dashrule}
\usepackage{multirow}
\usepackage{array}
\usepackage{enumitem}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%       CODE ENVIRONMENT FOR LONGER CODE FRAGMENTS
%       TO USE: \begin{lstlisting} [code] \end{lstlisting}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{listings}     % for preformatted code fragments
\usepackage{lmodern}   % NEEDED FOR BOLD FONTS WITHIN LISTINGS  
\lstset{
  language=Java,
  emph={EXPR,EXPR1,EXPR2,COND,COND1,COND2,STMT,STMT1,STMT2,DEC_STMT,CODE,CODE1,CODE2},  % LABELS that are bold and underline
  emphstyle=\bfseries\underbar,
  frame=none,   % default to none, can set =single, =tb, 
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=4
}

%%  TIP: to get list enumerated with lowercase roman numerals: i,ii,iii,iv,iv, ...
%\begin{enumerate}[label={\roman*}]
%\item double d = s;
%\item double d = c;
%\item double d = t;
%\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% package:exams POINTS STYLE AND PLACEMENT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pointpoints{pt}{pts}
\pointsinrightmargin

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HEADER & FOOTER STYLE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pagestyle{headandfoot}
\runningheadrule
%\firstpageheader{}{}{}
\firstpageheader{CS\coursenumber \space \handoutname \space }{\week: \SOLUTION}{\textcopyright \yr \instructor}
\runningheader{CS\coursenumber \space \handoutname \space }{\week: \SOLUTION}{\textcopyright \yr \instructor}
\firstpagefooter{\textcopyright  \yr  \instructor}{CS\coursenumber \space \handoutname \week \SOLUTION}{Page \thepage\ of \numpages}
\runningfooter{}{CS\coursenumber \space \handoutname \space \SOLUTION}{Page \thepage\ of \numpages}
\runningfootrule

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}


%%%%%%%%%%%%%%%%%%%%% HANDOUT TITLE %%%%%%%%%%%%%%%%%%%%
%\begin{center}  
%
%\vspace{1mm}
%
%Computer Sciences \coursenumber                   % Computer Sciences 302
%
%\vspace{2mm}
%
%{\Large\week \weeknumber: \handoutname \SOLUTION}                       % Shadowed (hidden) fields
%
%\vspace{2mm}
%
%Instructor: \instructorfull                                % Instructors: Deppeler
%
%\vspace{5mm}
%
%\end{center}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%% HANDOUT INSTRUCTIONS %%%%%%%%%%%%%%%%
%\textbf{Fill in these fields (left to right) on the red scantron form (use \#2 pencil):} 
%\begin{enumerate}
%\item \makebox[\textwidth]{\textbf{Full Name:}\enspace\hrulefill}
%
%\item \makebox[\textwidth]{\textbf{NetID and Email:}\enspace\hrulefill}
%
%\end{enumerate}
%\vspace{5mm}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%% WAIT TO GET STARTED (Quiz or Exam Only) %%%%%%%%%%%%%%%%%%
%
%\hfil\hdashrule{\textwidth}{0.5pt}{1pt}\hfil
%
%\vspace{3mm}
%
%\begin{enumerate}
%\item Be sure to review the reference pages as needed during the exam.
%\item \textbf{Turn off and put away} your cell phone, calculator, Inspector Gadget (watches, glasses, pencils, etc.) now and wait for the proctor to signal the start of the exam. 
%\end{enumerate}
%
%\vspace{3mm}
%
%\hfil\hdashrule{\textwidth}{0.5pt}{1pt}\hfil


%% GRADE TABLE UNUSED ON MULTIPLE CHOICE EXAMS %%
%\begin{center}
%\gradetable
%\end{center}
%\vspace*{\fill}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                     START OF EXAM REFERENCE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\newpage
%
%\begin{adjustwidth}{-0.5in}{-0.5in}
%\textbf{Disclaimer:} the following are provided for your reference only, and the inclusion of information here does not guarantee it will be used on the exam.
%\subsection*{Operator Precedence Table:}
%%%%%%%%%%%%%%%%%%%% CS302 TABLE
%\begin{center}
%\begin{tabular}{c || >{\ttfamily}c | c}
%\hline
%\textbf{level} & \textbf{operator} & \textbf{description} \\
%\hline
%\multirow{2}{*}{higher} & ( <expression> ) & grouping with parentheses \\
% & [ ] ( ) . & array index, method call, member access (dot operator) \\
%\multirow{8}{*}{\Huge{$\updownarrow$}} & ++ -- & post-increment, post-decrement\\
% & ++ -- + - !& pre-increment, unary plus/minus, logical negation\\
% & (type) new & casting and creating object \\
% & * / \% & multiplication, division, modulus \\
% & + - + & addition, subtraction, concatenation \\
% & < <= > >= & relational \\
% & == != & equality \\
% & \&\& & conditional AND (short-circuits) \\
% & || & conditional OR (short-circuits) \\
%\multirow{2}{*}{lower} & ? : & ternary conditional \\
% & = += -= *= /= \%= & assignment, arithmetic (compound) assignment\\
%\hline
%\end{tabular}
%\end{center}
%%%%%%%%%%%%%%%%%%%% CS302 TABLE
%
%%%%%%%%%%%%%%%%%%%%% COMPARABLE INTERFACE REFERENCE (Exam 3 only)
%%\subsection*{The java.lang.Comparable interface:}
%%\begin{tabular}{>{\ttfamily}l l}
%%int compareTo(Object ob) & Returns a negative value if \texttt{this} is less than \texttt{ob}, 0 if they are equal, \\
%%& and a positive value if \texttt{this} is greater than \texttt{ob} \\
%%\end{tabular}
%
%%%%%%%%%%%%%%%%%%%%% COMPARABLE INTERFACE REFERENCE (Exam 3 only)
%%\subsection*{The java.lang.Comparable\texttt{<T>} interface:}
%%\begin{tabular}{>{\ttfamily}l l}
%%int compareTo(T t) & Returns a negative value if \texttt{this} is less than \texttt{t},  0 if they are equal,\\
%%&  and a positive value if \texttt{this} is greater than \texttt{t} \\
%%\end{tabular}
%
%%%%%%%%%%%%%%%%%%%% OBJECT CLASS REFERENCE
%\subsection*{Methods from the java.lang.Object class: (all reference variables have these methods)}
%\begin{tabular}{>{\ttfamily}l l}
%String toString() & Returns a \texttt{String} representation of the object. \\ 
%\vspace{2mm} & This is the hashcode of the instance unless \texttt{toString()} has been overridden. \\
%boolean equals(Object o) & Returns \texttt{true} if the object referenced as \texttt{o} is the same as \texttt{this}. \\
%& It is often overridden (redefined) by the class. \\
%\end{tabular}
%
%%%%%%%%%%%%%%%%%%%%% INTEGER CLASS REFERENCE (Exam 3 only)
%%\begin{adjustwidth}{-0.5in}{-0.5in}
%%\subsection*{Methods from the java.lang.Integer class:}
%%\begin{tabular}{>{\ttfamily}l l}
%%static int parseInt(String s) & converts \texttt{String s} into its integer value. \\
%%&  throws a \texttt{NumberFormatException} if \texttt{s} cannot be \\
%%&  converted into an integer value
%%\end{tabular}
%%\end{adjustwidth}
%
%%%%%%%%%%%%%%%%%%%% MATH CLASS REFERENCE
%\subsection*{Constant and Methods from the java.lang.Math class:}
%\begin{tabular}{>{\ttfamily}l l}
%Math.PI & Field that represents the constant $\pi$ \\
%double random() & Returns a random value between 0 (inclusive) and 1 (exclusive) \\
%double pow(double x, double n) & Returns $x^{n}$ \\
%double sqrt(double n) & Returns $\sqrt{n}$ \\
%double abs(double n) & Returns the absolute value of n\\
%double ceil(double n) & Returns the value of n rounded up to the nearest whole number.\\
%double sin(double theta) & Returns the sine of the angle $\theta$ ($\theta$ is in radians)\\
%& Other trig methods also availabe.
%\end{tabular}
%
%%%%%%%%%%%%%%%%%%%%%% RANDOM CLASS REFERENCE
%\subsection*{Methods from the java.util.Random class:}
%\begin{tabular}{>{\ttfamily}l l}
%Random() & Creates a new random number generator. \\
%Random(int s) & Creates a new random number generator seeded with \texttt{s}. \\
%int nextInt() & Returns the next pseudo-random integer value. \\
%int nextInt(int n) & Returns the next pseudo-random integer value between 0 (inclusive) and n (exclusive). \\
%double nextDouble() & Returns the next pseudo-random double value between 0.0d (inclusive) and 1.0d (exclusive). \\
%\end{tabular}
%\end{adjustwidth}
%
%
%
%%%%%%%%%%%%%%%%%%%% STRING CLASS REFERENCE 
%\begin{adjustwidth}{-0.5in}{-0.5in}
%\subsection*{Methods from the java.lang.String class: (*REMEMBER 0-based indexing is used)}
%\begin{tabular}{>{\ttfamily}l l}
%int length() & Returns number of characters in the \texttt{String} \\
%char charAt(int index) & Returns the character at the specified \texttt{index} \\
%boolean contains(String s) & Returns \texttt{true} iff string s is in this string, otherwise \texttt{false} \\
%String toLowerCase() & Returns a\textit{new} string that is the lowercase week of this string. \\
%\vspace{2mm} String toUpperCase() & Returns a \textit{new} string that is the UPPERCASE week of this string. \\
%
%int indexOf(String s) & Returns the index within this string of the first character \\ 
%\vspace{2mm} & of the first occurrence of the specified string s. \\
%
%boolean equals(String s) & Returns \texttt{true} if the contents of this String \\
%\vspace{2mm} & is the same as the contents of String s. \\
%
%boolean equalsIgnoreCase(String s) & Returns \texttt{true} iff the contents of the \texttt{this} string is the same\\
%\vspace{2mm} & as that of the string \texttt{s}, ignoring differences in case. \\
%
%String substring(int begin) & Returns a \textit{new} string that is a substring of this string \\
%\vspace{2mm} & starting at begin to the end of the this string. \\
%
%String substring(int begin, int end) & Returns a \textit{new} string that is a substring of this string \\
%\vspace{2mm} & starting at index \texttt{begin} up to but not including \texttt{end}. \\
%
%boolean startsWith(String prefix) & Returns \texttt{true} if this string starts with the specified prefix \texttt{prefix}, \\
%\vspace{2mm} & \texttt{false} otherwise. \\
%
%boolean startsWith(String pre, int off) & Returns \texttt{true} if \texttt{this} string starts at the specified offset \texttt{off} \\
%& with the specified prefix \texttt{pre}, \texttt{false} otherwise. \\
%\end{tabular}
%\end{adjustwidth}
%
%%%%%%%%%%%%%%%%%%%%% COLOR CLASS REFERENCE ( Exam 3 only)
%%\begin{adjustwidth}{-0.5in}{-0.5in}
%%\subsection*{Constructors from the java.awt.Color class: r,g,b (0-255), a (0-10)):}
%%\begin{tabular}{>{\ttfamily}l l}
%%Color(float r, float g, float b) & Creates a Color instance \\
%%Color(float r, float g, float b, float a) & Creates a Color instance \\
%%Color(int rbg) & Creates a Color instance \\
%%Color(int r, int g, int b) & Creates a Color instance \\
%%Color(int r, int g, int b, int a) & Creates a Color instance \\
%%\end{tabular}
%%\end{adjustwidth}
%%
%%%%%%%%%%%%%%%%%%%%%% FILE CLASS REFERENCE Exam 3 only)
%%\begin{adjustwidth}{-0.5in}{-0.5in}
%%\subsection*{Methods from the java.io.File class:}
%%\begin{tabular}{>{\ttfamily}l l}
%%File(String filename) & Creates a \texttt{File} object for the given file name \\
%%boolean exists() & Returns \texttt{true} if the specified file already exists \\
%%boolean canRead() & Returns \texttt{true} if the specified file can be opened for reading \\
%%boolean canWrite() & Returns \texttt{true} if the specified file can modified (written to) \\
%%\end{tabular}
%%\end{adjustwidth}
%%
%%%%%%%%%%%%%%%%%%%%%% PRINTWRITER CLASS REFERENCE (Exam 3 only)
%%\begin{adjustwidth}{-0.5in}{-0.5in}
%%\subsection*{Methods from the java.io.PrintWriter class:}
%%\begin{tabular}{>{\ttfamily}l l}
%%PrintWriter(String filename) throws FileNotFoundException & Creates a \texttt{PrintWriter} for the given file name \\
%%PrintWriter(File out) throws FileNotFoundException & Creates a \texttt{PrintWriter} from out \\
%%void close() & Closes the stream and any associated file \\
%%void print(String s) & Prints given string \\
%%void println(String s) & Prints given string followed by a newline \\
%%\end{tabular}
%%\end{adjustwidth}
%
%\begin{adjustwidth}{-0.5in}{-0.5in}
%
%%%%%%%%%%%%%%%%%%%%%% SCANNER CLASS REFERENCE 
%\subsection*{Methods from the java.util.Scanner class:}
%\begin{tabular}{>{\ttfamily}l l}
%Scanner(String s) & Creates a \texttt{Scanner} to read the \texttt{String s} \\
%Scanner(System.in) & Creates a \texttt{Scanner} that reads from the keyboard. \\
%Scanner(File fn) throws FileNotFoundException & Creates a \texttt{Scanner} to read from file \\ % (EXAM 3 ONLY)
%void close() throws IOException & Closes the stream and any associated file \\
%boolean hasNext() & Returns \texttt{true} if there’s another token of input. \\
%boolean hasNextInt() & Returns \texttt{true} if the next input is an int value. \\
%boolean hasNextDouble() & Returns \texttt{true} if the next input is a double value. \\
%boolean hasNextLine() & Returns \texttt{true} if there’s another line of input. \\
%String next() & Returns the next word only, as a String. \\
%int nextInt() & Returns the next word only, as an integer. \\
%double nextDouble() & Returns the next word only, as a double. \\
%String nextLine() & Returns the next line as a String. \\
%\end{tabular}
%
%%%%%%%%%%%%%%%%%%%%%% ARRAYS CLASS REFERENCE (Some methods for Exam 2 & 3 only)
%\subsection*{Method from the java.util.Arrays class:}
%\begin{tabular}{>{\ttfamily}l l}
%\vspace{2mm} static String toString(T[] array) & Returns a \texttt{String} representation of any type (\texttt{T[]}) array. \\
%static void sort(T[] array) & sorts the specified array in memory \\
%\vspace{2mm} & type T must be Comparable or Comparable\texttt{<T>} \\
%static int[] copyOf(int[] orig, int newLength) & Copies the specified array, truncating or padding with \\
%\vspace{2mm} & zeros (if necessary) so the copy has the specified length. \\
%static <T> T[] copyOf(T[] orig, int newLength) & Copies the specified array, truncating or padding with \\
% &  \texttt{null}s (if necessary) so the copy has the specified length. \\
%\end{tabular}
%
%%%%%%%%%%%%%%%%%%%%%% ARRAYLIST CLASS REFERENCE (Exam 2 and 3 only)
%\subsection*{Methods from the java.util.ArrayList class (*REMEMBER 0-based indexing is used):\\
%Note the E’s below are replaced with the particular \texttt{ArrayList}’s element type.}
%\begin{tabular}{>{\ttfamily}l l}
%int size() & Returns the number of used elements in this list. \\
%E[] toArray() & Returns an array of the specified type of this ArrayList \\
%boolean contains(E item) & Returns true iff the specified item is in this list, otherwise false. \\
%\vspace{2mm} int indexOf(E item) & Returns the index of the specified item if it is in this list, otherwise \texttt{-1} \\
%E get(int index) & Returns the item at the specified index in this list. \\
%\vspace{2mm} & \texttt{throws IndexOutBoundsException} if invalid index. \\
%void add(E item) & Adds the specified item to the end of this list. \\
%\vspace{2mm} void add(int index, E item) & Adds the specified item by inserting it into this list at the specified index. \\
%E remove(int index) & Removes and returns the item from the specified index. \\
%boolean remove(E item) & Returns \texttt{true} iff the specified item was removed from \\
% & this list, otherwise \texttt{false}. \\
%\end{tabular}
%
%\end{adjustwidth}

%\vspace*{\fill}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                            START OF HANDOUT                                                                   
% ----------------------------------------------------------------------------------------------------------------------------
%  #-Y = Yellow = Primary Exam week      #- G = Green = Secondary Exam week  #-T = Buff = Tertiary week 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \subsection*{Elementary Programming}
% \subsection*{Selections}
% \subsection*{Loops}
% \subsection*{methods}
% \subsection*{Math functions}
% \subsection*{String and char}
% \subsection*{1D Arrays}
% \subsection*{Classes and objects}
% \subsection*{2D Arrays}
% \subsection*{1D Arrays}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection*{Introduction: Shadowed (hidden) Fields}

Fields become shadowed when a constructor or method contains a declaration of a parameter or local variable with the same name as the field.

Use the reserved word \textbf{\texttt{this}} to reference the field instead of the parameter or local variable.

\begin{questions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  1-SAMPLE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\question What is output by the code fragment given the class definition of \texttt{Shadow}?

\begin{lstlisting}[frame=none]
// Code Fragmet
Shadow shadow = new Shadow();
shadow.Shadow();
System.out.println( shadow ); 
\end{lstlisting}

\begin{adjustwidth}{}{3in} \begin{lstlisting}[frame=single]
public class Shadow { 

    private int shadow = 1;
    private final int N; 
    private int m;

    public Shadow() { 
        N = shadow; 
    }

    public void Shadow() { 
       int shadow = 2;
       shadow = shadow;
    }

    public String toString() {
        return N + ": shadow = " + shadow;
    }

}
\end{lstlisting} \end{adjustwidth}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  2-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\question Draw the Object (memory) diagram of the memory allocation during the program in the previous question's execution.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  3-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\vspace{2mm}

\question What is output by the code fragment given the class definition of \texttt{Shadow}?

\begin{lstlisting}[frame=none]
// Code Fragment
Shadow shadow = new Shadow(3);
System.out.println( shadow ); 
\end{lstlisting}

\begin{adjustwidth}{}{3in} \begin{lstlisting}[frame=single]
public class Shadow { 

    private int shadow = 1;
    
    public Shadow(int shadow) { 
       shadow = shadow;
    }

    public String toString() {
        return "shadow = " + shadow;
    }

}
\end{lstlisting} \end{adjustwidth}

\vspace{\fill}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  4-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\question What is output by the code fragment given the class definition of \texttt{Shadow}?

\begin{lstlisting}[frame=none]
// Code Fragment
Shadow shadow = new Shadow(3);
shadow.Shadow();
System.out.println( shadow ); 
\end{lstlisting}

\begin{adjustwidth}{}{3in} \begin{lstlisting}[frame=single]
public class Shadow { 
    private int shadow = 1;

    public Shadow(int shadow) { 
       this.shadow = shadow;
    }
    public void Shadow() { 
       int shadow = 2;
       this.shadow = shadow;
    }
    public String toString() {
        return "shadow = " + shadow;
    }
}
\end{lstlisting} \end{adjustwidth}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  5-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newpage

\question What is output by the code fragment given the class definition of \texttt{Shadow}?

\begin{lstlisting}[frame=none]
// Code Fragment
Shadow shadow = new Shadow();
System.out.println( shadow );
\end{lstlisting}

\begin{adjustwidth}{}{3in} \begin{lstlisting}[frame=single]
public class Shadow { 

    private static int shadow = 1;

    private final int N;

    public Shadow( int shadow ) {
        N = this.shadow; 
        this.shadow = shadow; 
    }

    public Shadow() { 
        this(5); 
    }

    public String toString() {
        return N + ": shadow = " + shadow;
    }
}
\end{lstlisting} \end{adjustwidth}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  6-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newpage

\question \textbf{FIXED 11-13-15} What is output by the code fragment given the class definition of \texttt{Shadow}?

\begin{lstlisting}[frame=none]
// Code Fragment
int n = 0;
Widget [][] wa; 
wa = new Widget[10][];

for ( int i=0; i < wa.length; i++ ) {
   wa[i] = new Widget[i];
   for ( int j=0; j < wa[i].length; j++ )
     wa[i][j]=new Widget("W_"+i+"_"+j,n);
}

System.out.println( wa[2][1].getName() );
System.out.println( wa.toString() );
System.out.println( wa[4].length );
System.out.println( wa[3][0].getPassCode().charAt(2) );
System.out.println( wa[5][1].getName() );
System.out.println( wa[5][1].getPassCodeArray()[2] );
\end{lstlisting}

\begin{adjustwidth}{}{6cm} \begin{lstlisting}[frame=single]
import java.util.Arrays;
public class Widget {
   public final String name;  
   public final long id;
   private char [] passcode;
   public Widget( String s, long id ) {
      name = s;  this.id = id; passcode = new char[4];
      for ( int i=0; i < passcode.length; i++ ) 
          passcode[i] = (char)('A'+(int)(Math.random()*26));
   }
   public String getName() { return name; }
   public char[] getPassCodeArray() {
      return passcode; 
   }
   public String getPassCode() {  
      return Arrays.toString(passcode); 
   }
}\end{lstlisting} \end{adjustwidth}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  7-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\question Draw the Object (memory) diagram of the memory allocation during the program in the previous question's execution.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  8-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\question What happens when you copy an array of references?

\begin{lstlisting}[frame=none]
// Code Fragment
Widget [] s = wa[3]; 
Widget w = wa[3][2];

System.out.println( s.toString() );
System.out.println( w.getPassCode() );
\end{lstlisting}

\begin{adjustwidth}{}{} \begin{lstlisting}[frame=single]
import java.util.Arrays;
public class Widget {

   public final String name;  
   public final long id;
   private char [] passcode;

   public Widget( String s, long id ) {
      name = s;  
      this.id = id; 
      passcode = new char[4];
      for ( int i=0; i < passcode.length; i++ ) 
          passcode[i] = (char)('A'+(int)(Math.random()*26));
   }

   public String getName() { 
      return name; 
   }

   public char[] getPassCode() {
      return passcode; 
   }

   public String getPassCode() {  
      return Arrays.toString(passcode); 
   }
}\end{lstlisting} \end{adjustwidth}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION 9-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\question How would you make copies of all instances?

\vspace{\fill}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  10-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  11-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  12-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION 13-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  14-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  15-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  16-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  17-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  18-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  19-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  20-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  21-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  22-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  23-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  24-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  25-Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% QUESTION  -Y   -G                                                QUESTION TEMPLATE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\question ?
%
%\begin{lstlisting}[frame=single]      % = none, =tb, =lr
%
%code fragment here
%
%\end{lstlisting}
%
%\begin{enumerate}[label={\roman*}]
%\item one
%\item two
%\item three
%\end{enumerate}
%	
%\begin{choices}
%\choice {\tt !}
%\choice @
%\choice \#
%\choice \$
%\choice \%
%\end{choices}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% QUESTION  -Y   -G                                                HAVE YOU ANSWERED ALL QUESTIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\newpage
%
%\hdashrule{\textwidth}{0.5pt}{1pt}
%
%Check that you have answered all questions and bring your UW ID and turn in your exam question paper and scantron sheet.
%
%\hdashrule{\textwidth}{0.5pt}{1pt}


\end{questions}

\end{document}

%%%%%%%%%%%%%%%%%%%%%% SAMPLE FORMAT EXAMPLES %%%%%%%%%%%%%
% ROMAN NUMERAL 
%\begin{enumerate}[label={\roman*}]
%\item \texttt{ double d = s.hasNextDouble(); }
%\item\texttt{  double d = c; }
%\item\texttt{  double d = t; }
%\end{enumerate}
%
%
%\question Which replacement for \textbf{\underline{CODE}} completes this method, which  must return true if and only if the value of \texttt{cents} is between 0 (inclusive) and 100 (inclusive).  Otherwise, the method must return false.
%
%\begin{lstlisting}[frame=none] 
%	public static boolean validCents (int cents) {
%  		CODE
%	}
%\end{lstlisting}
%
%\begin{choices}
%
%\choice \begin{lstlisting}[frame=none] 
%return true || false;
%\end{lstlisting}
% 
%\choice \begin{lstlisting}[frame=none] 
%if ( cents >= 0 ) { return true; }
%else if ( cents <= 100 ) { return true; }
%else { return false; }
%\end{lstlisting}
% 
%\choice \begin{lstlisting}[frame=none] 
%return !(cents < 0 || cents > 100);
%\end{lstlisting}
%
%\choice \begin{lstlisting}[frame=none] 
%if ( cents <= 100 || cents >= 0 ) { return true; }
%else { return false; }
%return 0 <= cents <= 100 ;
%\end{lstlisting}
%
%\end{choices}
%
%%%%%%%%%%%%%%%%%%%%%% SAMPLE FORMAT EXAMPLES %%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%% PYTHON EXAMPLES by HOBBES %%%%%%%%%%%%%
%
%  SAMPLE QUESTIONS FROM HOBBES CS301 -- kept for examples of fill-in the blank and multi-column
%
%%%%%%%%%%%%%%%%%%%%%% PYTHON EXAMPLES by HOBBES %%%%%%%%%%%%%
%\newpage
%
%\section{Question Type Examples}
%
%\subsection*{True or False: Basic Python information}
%

%%%%%%%%%%%%%%%%%%%%%%% QUESTION CHECKBOXES, TWO COLUMNS & TWO ROWS of choices
% QUESTION  -Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\question[4] What is the \textit{value} in {\tt x} after the following line of code is executed?
%\begin{adjustwidth}{1.3cm}{}
%{\tt x = "string"[1] + "berry"[-1]}
%\end{adjustwidth}
%
%%%%%%%%%%%%%%%%%%%%%%% QUESTION FILL IN THE BLANK EXAMPLES of choices
% QUESTION  -Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%
%\vspace{4mm}
%
%\begin{multicols}{2}
%\begin{checkboxes}
%	\CorrectChoice {\tt one
%	five}
%	
%	\vspace{5mm}
%	
%	\choice {\tt one
%	two}
%	
%	\choice {\tt one
%	two
%	five}
%	
%	\vspace{5mm}
%	
%	\choice {\tt five}
%\end{checkboxes}
%\end{multicols}

%\subsection*{Fill-in-the-blank: Writing code}
%
%For each of the following questions, fill in the value, operator, or statement needed to produce the indicated output (check the comments if you need a hint). Pay attention to data types!
%
%\vspace{5mm}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  -Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\question[6]
%\begin{adjustwidth}{.5cm}{}
%{\tt x = $\rule{5cm}{0.15mm}$ \# literal value
%
%print x * 5  \hspace{3.25cm}  \# output: 11111}
%\end{adjustwidth}
%
%\vspace{5mm}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  -Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\question[6]
%\begin{adjustwidth}{.5cm}{}
%{\tt x = $\rule{6cm}{0.15mm}$ \hspace{3mm} \# code to get number 1-10 from user
%
%print x + 5 \hspace{4.75cm} \# output is a number between 6-15}
%\end{adjustwidth}
%
%\vspace{5mm}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  -Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\question[6]
%\begin{adjustwidth}{.5cm}{}
%{\tt for x in range(10): \hspace{3.25cm} \# output: 1 2 3 4 5 6 7 8 9 10
%
%\vspace{3mm}
%
%\hspace{1cm} print $\rule{5cm}{0.15mm}$ \hspace{3mm} \# expression with x}
%\end{adjustwidth}
%
%\vspace{5mm}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  -Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\question[6] Fill in the blanks in the condition so that it evaluates to {\tt True} for the {\tt int} values of {\tt x} indicated on the number line below. Empty circles are not included in the range.
%
%\begin{center}
%\includegraphics[clip=true, trim=4cm 12.25cm 2cm 5cm, scale=0.6]{NumberlineE1}
%\end{center}
%
%\vspace{2mm}
%\begin{adjustwidth}{.5cm}{}
%\hspace{1cm}{\tt ( x < 5 and $\rule{3cm}{0.15mm}$ ) or $\rule{4cm}{0.15mm}$}
%\end{adjustwidth}
%
%\vspace{5mm}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  -Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\question[6]
%\begin{adjustwidth}{.5cm}{}
%{\tt x = 3 \hspace{3.4cm} \# output: 27
%
%print x $\rule{2cm}{0.15mm}$ 3 \hspace{3mm} \# operator only}
%\end{adjustwidth}
%
%\vspace{5mm}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% QUESTION  -Y   -G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\question[6]
%\begin{adjustwidth}{.5cm}{}
%{\tt num\_spaces = 0
%
%for x in word: \hspace{3.4cm} \# assume word is some long string
%
%\hspace{1cm}if $\rule{5cm}{0.15mm}$: \# condition
%
%\hspace{2cm}num\_spaces += 1 \hspace{1.7cm} \# count number of spaces
%}
%\end{adjustwidth}

