Skip to main content\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 9.21 Arrays-WE8-P1
Subsection 9.21.1 Subgoals for Evaluating Arrays
Set up array from 0 to size-1
Evaluate data type of statements against array
-
Trace statements, updating slots as you go
Remember assignment subgoals
Subsection 9.21.2 Arrays-WE8-P1
Exercises Exercises
1.
Q45: What are the contents of array alpha after this code has been executed?
int [] alpha = {10, 20, 30, 40, 50, 60, 70};
int start = alpha[alpha.length-1];
for (int i = 1; i < alpha.length; i++) {
alpha[i] = alpha[i-1];
}
alpha[0] = start;
{20, 30, 40, 50, 60, 70, 70}
{20, 30, 40, 50, 60, 70, 10}
{10, 20, 30, 40, 50, 60, 70}
{10, 10, 20, 30, 40, 50, 60}
{70, 10, 10, 10, 10, 10, 10}
2.
Q46: What are the contents of array beta after this code has been executed?
int [] beta = {10, 20, 30, 40, 50, 60, 70};
for (int i = 1; i < beta.length; i++) {
beta[i] = beta[i-1])
}
{20, 30, 40, 50, 60, 70, 70}
{20, 30, 40, 50, 60, 70, 10}
{10, 20, 30, 40, 50, 60, 70}
{10, 10, 20, 30, 40, 50, 60}
{70, 10, 20, 30, 40, 50, 60}
3.
Q47: What are the contents of array gamma after this code has been executed?
int [] gamma = {10, 20, 30, 40, 50, 60, 70};
for (int i = 0; i < gamma.length; i++) {
gamma[i] = gamma[i+1])
}
{20, 30, 40, 50, 60, 70, 70}
{20, 30, 40, 50, 60, 70, 10}
{10, 20, 30, 40, 50, 60, 70}
{10, 10, 20, 30, 40, 50, 60}
IndexOutOfBounds Exception
You have attempted
of
activities on this page.