Java

TOPIC 48 – TEST REVIEW

 

DETAILS & REVIEW


 

DETAILS


Your test will be fully written.  You will not have a computer.

 

Minor programming errors will not count against you.

 

The test covers most of the unit.  Every year, expectations vary a little bit depending on how much time we have.  Your teacher will give you details.

 

 

REVIEW

 

QUESTION 1 – DEFINITIONS

You should be comfortable with the following terms:

 

          Array

          ArrayList

          Index

          Element

          OutOfBounds error

          Array length

          Array size

          Array type

          Passing by value

          Passing by reference

          Sorting arrays

          Array trace
          Swapping elements

          Algorithm complexity

 

QUESTION 2 – ARRAY VS ARRAYLIST

a)    What is the difference between an array and an ArrayList?

b)    Write the statement to create an array that will hold 5 integers.  Write the statement to create an ArrayList that will hold integers.

c)     How do we get the number of elements in an array?  How about in an ArrayList?

d)    In an array named arr, how do you set element 2 to the value 7.  In an ArrayList named al, how do you set element 2 to the value 7.

e)    In an array, how do you output the value of element 5 to screen?  In an ArrayList, how do you output the value of element 5 to screen?

 

QUESTION 3 – SHORT ANSWER QUESTIONS

Consider the following array and answer the related questions:

 

 

a)    What is the array’s length?

 

b)    What is the index of the element with value 74?

 

c)     What is the value of element 3?

 

d)    What type of array is this?

 

e)    Which element contains the smallest value in the array?

f)      What is the maximum value in the array?

g)    What is the index of the minimum value in the array?


QUESTION 4 – ARRAY TRACING

 

Trace the array after each group of statements.  You can assume that all of these statements would be placed inside one single program.  So any element unchanged in E would still have their value from D.

 

#

Code

Array Trace

A)

int[] arr = new int[6];

 

 

B)

arr[0] = 2;

arr[1] = 3;

arr[2] = 4;

arr[3] = 5;

arr[4] = 6;

arr[5] = 1;

 

C)

for (int i=0; i<arr.length; i++)

{

   arr[i] = i * 3;

}

 

D)

arr[5] = -11;

arr[4] = 2;

arr[arr[4]] = 9;

 

E)

swap(arr,0,3);

 

F)

int min = minIndex(arr);

int max = maxIndex(arr);

swap(arr, min, max);

 

G)

ArrayList<Integer> al;

al = new ArrayList<Integer>();

al.add(4);

al.add(2);

System.out.println(al);

 

H)

al.add(1);
al.add(0,3);

System.out.println(al);

 

I)

al.set(1, 11);

System.out.println(al)

 

 

QUESTION 5 – CODING QUESTIONS

 

PART 1

a) Write the line of code that will create a String[] array containing the words “blue”, “pink”, “green” and “red”.

b) Write the for loop needed to output the array to screen one element at a time.


PART 2

a) Write the line of code required to create an integer array called coco of size 12.

b) Write the for loop necessary to initialize each element x in coco to x+1.  So element 0 should get the value 1, element 1 should get the value 2, …

 

c) Output the array to screen by using the toString function from the Arrays class.

 

QUESTION 6 – ADVANCED CODING QUESTIONS

 

a)    Write a function called same that will get two arrays as parameters and returns true if the two arrays contain the exact same data.

b)    Write a function called maxValue that will get an array as parameter and return its maximum value.

 

c)     Write a function called minIndex that will get an array as parameter and return the index of the element containing the minimum value.

 

d)    Write a function called indexOf that will get an array and an integer v as parameters.  It returns the index of the element that contains the value v.  If no element contains the value v, then the function returns -1.


 

Good luck!  There is always hope – at least a ray of hope.

 

J