1. What are the indexes for the first and last positions of any array?
x[0] x[x.leght-1]
2. Immediately after instantiating a new array of primitives (ints, doubles, etc.), what fills the array? What about
an array of objects?
Zero, null reference
3. What happens when you try to access an array element past the end of an array?
Throws ArrayIndexOutOfBoundsException
4. Instantiate three arrays called x, y and z of types int, String and BankAccount, respectively, all of size 10:
int[] x = new int[10];
String[] y = new String[10];
BankAccount[] accounts = new BankAccount[10];
5. What is method overloading?
Overloading, methods with the same names, same class, different signatures
6. Use the following full array, x, to answer the parts of question 6:
4
8
5
1
6
3
2
a. What value is given by x[1]? 8
b. What value is given by x[6]? 2
c. What value is given by x[7]? Exception thrown
d. What value is given by x.length? 7
7. Write a for-loop to double each element in the array x given in question 6 above.
for (int i=0; i<x.length; i++)
x[i] *=2;
8. What is a static variable? What is a static method?
static variable: shared among all instances of a class
static method – invoked using the class name
Answers to CSE 110 Exam 3 Review
Use the following code to answer the parts of question ten:
public class AmazingClass
{
private static int number;
public AmazingClass(int a)
{
number = a;
}
public int twice()
{
number*=2;
return number;
}
}
9. What is the value of number after the following statements? (For each part, assume the preceding parts it have
already been executed.)
a. AmazingClass ac1 = new AmazingClass(3); 3
b. AmazingClass ac2 = new AmazingClass(7); 7
c. ac1.twice();14
d. ac2.twice(); 28
10. Using the code from question 9, how many copies of the variable number exist after I instantiate 374 different
AmazingClass objects? 1
11. What is the meaning of each of “public”, “static”, “void”, “main”, and “String[] args”? public: visible to
all classes static: static method invoked by class name
void: no return value
main: method name called by the interpreter
string[] args: array of string passed to the method main
12. Given the array from question 6, write the code fragments requested. You don’t need to write them in methods,
just assume you’re working within an established main method.
a. Write code to store the largest number in the array into a variable called max.
int max = x[0];
for (int i=1; i<x.length; i++)
if (x[i] > max)
max = num[i];
b. Write code to count how many numbers in the array are strictly larger than four, and store that total in a variable
called total.
int total =0;
for (int i=0; i<x.length; i++)
if (x[i] > 4)
total++;
c. Write code to print out every other element in the array separated by tabs.
for (int i=0; i<x.length; i+=2)
System.out.println(x[i] + “\t“);
d. Write code to shift each number one place to the right. There will be two copies of the first element when
you’re done with this.
for(int i=x.length-2; i>=0; i--)
x[i+1] = x[i];
e. Write code to print the contents of the array in reverse order, one element for each line.
for (int i=x.length-1; i>=0; i--)
System.out.println(x[i]);
12. Circle the valid method headings assuming they are written inside a class named SomeClass.
public void Void( ) public String string( int n )
public double void f2( ) public BankAccount bankAccount( )
public double sum( int left, right )
13. Use the following array to answer parts of the question:
Mike
Betsy
Aaron
Steven
Doug
Pat
Elise
(1) Write the contents after each step of selection sort (alphabetical).
M B A S D P E
Pass1: A B M S D P E
Pass2: A B M S D P E
Pass3: A B D S M P E
PASS4: A B D E M P S
PASS5: A B D E M P S
PASS6: A B D E M P S
b. Write the contents after each step of insertion sort (alphabetical).
M B A S D P E
PASS1; B M A S D P E
PASS2: A B M S D P E
PASS3; A B M S D P E
PASS4; A B D M S P E
PASS5; A B D M P S E
PASS6: A B D E M P S
15. Use the sorted list and use a binary search to look for Mike in the list. Show all the names that are going to be
compared with Pat before it finds it.
Aaron
Betsy
Doug
Elise
Mike
Pat
Steven
First= 0 First=4 Elise, Pat, Mike
Last = 6 last = 5
Middle = 3 Middle= 5 Middle = 4
Repeat the same process looking for Cathy, which is not in the list.
First = 0 First =2 Elise, Betsy, Doug
Last =6 Last =2 Last = 1
Middle = 3 middle = 1 middle = 2
15. Write class LittleStatistician to maintain two descriptive statistics: count and average. Write method add to add
numbers to the collection. Write methods count and average to return the correct values. Also, write method toString
to return all elements in the collection as a string (see output below). You must use an array instance variable to
store the elements. Let the capacity be 5 (kept small here for demonstration purposes). When an attempt is made to
add to a LittleStatistician object when the array is full (trying to add a 6th or 7th number), print a message stating the
number could not be added. The following code on the left must generate the output shown to the right. Use the back
of this page if necessary.
LittleStatistician tests = new LittleStatistician( );
tests.add( 80.0 );
tests.add( 70.5 );
tests.add( 75.0 );
tests.add( 82.5 );
tests.add( 99.5 );
tests.add( 65.0 );
tests.add( 52.0 );
System.out.println( "Average: " + tests.average( ) );
System.out.println( "Count: " + tests.count( ) );
System.out.println( tests.toString( ) );
public class LittleStatistician2
{
//instance variables
private int my_count;
private double[] my_data;
//constructor
public LittleStatistician2( )
{
my_count = 0;
my_data = new double[5];
}
public void add( double newNumber )
{
if( my_data.length > my_count )
{
my_data[my_count] = newNumber;
my_count++;
}
else
Output:
Could not add 65.0
Could not add 52.0
Average: 81.5
Count: 5
System.out.println( "Could not add " + newNumber );
}
public int count( )
{
return my_count;
}
public double average()
{
double sum = 0.0;
for(int j = 0; j < my_count; j++ )
sum += my_data[j];
return sum / my_count;
}
public String toString( )
{
String result = "[";
for( int j = 0; j < my_count - 1; j++ )
result += my_data[j] + ", ";
return result + my_data[my_count-1] + "]";
}