ArrayLists
2D-Arrays
Searching/Sorting
Recursion
100

What is advantage of using an ArrayList vs a 1-D array?

It is flexible and dynamic in size and there are built-in methods you can utilize with ArrayLists too such as .add, .set

100

Consider the following code.


int[][] grid = new int[4][3];

int c = 1;

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

    for (int j = 0; j < grid[i].length; j++) {    

        grid[i][j] = c;    

        c++;  

    }

}

What are the contents of the array?

1 2 3 

4 5 6 

7 8 9 

10 11 12 

100

Large or small datasets for binary Search?

Unsorted or sorted data for linear search?

Large, unsorted

100

What is printed when mysterm(3) is called? 

1, 2, 3

200

If an ArrayList called myList contains 10 integers and you do System.out.println(myList.size());  - what is printed ?

10

200

{3, 2 ,2}

{3,2,1}

{4,1,2}

If this 2D array was initialized as "myArray", how would you access the number 4? Assume there are no errors in this code above.

myArray[2][0]

200

Explain the difference between selection sort and insertion sort in detail

Selection sort finds the smallest number in an array and swaps the appropriate elements for each index, insertion sort compares adjacent elements and inserts the element where it needs to go.

200

What is returned when n=4? 


24

300
On a piece of paper, write the full code for creating an ArrayList of an object data type Dragon. Make sure all syntax is correct.


ArrayList<Dragon> myDragonList1 = new <Dragon>();

300

What are the contents of arr after the following code has been executed?

int[][] arr = { {3,2,1},{1,2,3}};
for (int row = 1; row < arr.length; row++)
{
   for (int col = 1; col < arr[0].length; col++)
   {
      if (arr[row][col] % 2 == 1)
      {
          arr[row][col] = arr[row][col] + 1;
      }
      if (arr[row][col] % 2 == 0)
      {
          arr[row][col] = arr[row][col] * 2;
      }
   }
}

{ {3, 2, 1}, {1, 4, 8}}

300

How many checks will occur with a linear search on the following array if the target value is 17:


{12,9,5,20,27,17,40,20}

6

300

What food manufacturing company headquartered in Battle Creek, Michigan, uses several animal mascots to sell its cereals, such as Newton the Owl, Tony the Tiger and a rooster named Cornelius?

Stack Overflow Error

400

ArrayList<Integer> list = new ArrayList<Integer>();

list.add(2);

list.add(3);

list.add(10);

list.add(2);

for(int i = 0; i < list.size() - 1; i++)

{

   if(list.get(i) > list.get(i + 1))

   {

       System.out.print(list.get(i) + " ");

   }

}

What is printed?

10

400

A two-dimensional array, imagePixels, holds the brightness values for the pixels in an image. The brightness can range from 0 to 255. What does the following method compute?

public int findMax(int[][] imagePixels)
{
   int r, c;
   int i, iMax = 0;

   for (r = 0; r < imagePixels.length; r++)
   {
      for (c = 0; c < imagePixels[0].length; c++)
      {
         i = imagePixels[r][c];
         if (i > iMax)
            iMax = i;
       }
    }
    return iMax;
 }

The maximum brightness value for all pixels in imagePixel

400

How many checks will a computer need to do on the following array using a binary search if the target value is 19:

{3,5,10,12,15,20,22,19}

4

400

How many times is mystery() called in mystery(4)? 


5 times

500

A class stores a list of integers representing daily temperatures. Write a method removeBelowAverage that removes all values that are strictly below the average of the list.

The method should:

  • First compute the average of all values
  • Remove all values less than the average
  • Modify the original list
  • Return the number of values removed

Starter code:

import java.util.ArrayList;

public class TemperatureUtils
{
   public int  removeBelowAverage(ArrayList<Integer> temps)
   {
       // to be implemented
   }
}

Syntax does not need to be perfect for this, but I would like to see the logic make sense and there needs to be a good attempt at syntax.

public static int removeBelowAverage(ArrayList<Integer> temps)
{
    int sum = 0;

    for (int num : temps)
    {
        sum += num;
    }

    double average = (double) sum / temps.size();
    int removed = 0;

    for (int i = temps.size() - 1; i >= 0; i--)
    {
        if (temps.get(i) < average)
        {
            temps.remove(i);
            removed++;
        }
    }

    return removed;
}

500

Given the following code segment, what is the value of sum after this code executes?

int[][] m = { {1,1,1,1},{1,2,3,4},{2,2,2,2},{2,4,6,8}};

int sum = 0;
for (int k = 0; k < m.length; k++)
{
    sum = sum + m[m.length-1-k][1];
}

9

500

Consider the following algorithm, which implements the selection sort algorithm.


int[] elements = {4, 10, 3, 1, 2, 8, 9, 6};

for (int j = 0; j < elements.length - 1; j++) {

      int minIndex = j;

      for (int k = j + 1; k < elements.length; k++) {

            if (elements[k] < elements[minIndex]){

              minIndex = k;

            }

      }

      int temp = elements[j];

      elements[j] = elements[minIndex];

      elements[minIndex] = temp;

 }

What are the contents of the array elements after 1 pass of the outer loop?

[1, 2, 3, 4, 10, 8, 9, 6]

500

Consider the following code:


public static int mysteryRecur(int n){

    if(n <= 0)

        return 1;

    else if(n <= 3)

        return 3;    

    else

        return mysteryRecur(n / 2) + n;

}

What is returned when the call mysteryRecur(20) is run?

38