Preview

15 - Developing Algorithms Using Arrays

 1. Regarding the creation of an integer array, is the following statement true or false?
To create an array type the name and 
an equals sign then use the new keyword,
followed by a space, then the type, and
then in curly brackets the length of the
array (the last index number).

Example: names = new String{5};

  TRUE

  FALSE

 2. What is the output of the following code?
public class Test
{
    public static void main(String[] args)
    {
        int[] arr1 = {1, 3, 7, 9, 15};
        for (int index = 0; index < arr1.length; index+=2)
        {
            System.out.print(arr1[index] + ", ");
        }
    }
}

  3,5,9,12,17

  1,7,15

  1,9

  1,3,7,9,15

 3. We want the following code to print 15,9,7,3,1. What needs to be changed or added to the following code?
public class Test
{
    public static void main(String[] args)
    {
        int[] a1 = {1, 3, 7, 9, 15};
        for (int i = a1.length - 1; i >= 0; i)
            System.out.print(a1[i] + ", ");
    }
}

  Line 6 changed to: for (int i = a1.length - 1; i - 1 >= 0; i)

  Line 6 changed to: for (int i - 1 = a1.length - 1; i >= 0; i--)

  Line 6 changed to: for (int i = a1.length - 1; i >= 0; i--)

  Line 6 changed to: for (int i = a1.length - 1; i >= 0; i++)

 4. In order for the following code to output 12,3,4, what should line 6 be?
public class Test1
{
    public static void main(String[] args)
    {
        int[] arr1 = {1, 2, 3, 4};
        ??????????????????????????
        {
            System.out.print(value + ", ");
        }
    }
}

  for (value:arr1[value]

  for ( value:arr1)

  for (int:arr1[i])

  for (int value:arr1)

 5. You wish to print out all the odd values in the array a1. What should line 8 be?
public class Test1
{
    public static void main(String[] args)
    {
        int[] a1 = {0, 3, 6, 7, 9, 10};
        for (int value : a1)
        {
            ????????????????????????
            {
                System.out.print(value + " ");
            }
        }
    }
}

  if (value ==0,3,5)

  if (value + 2 )

  if (value % 2 ==0)

  if (value % 2 == 1)

 6. What is the output of the following code?
public class Test
{

    public static int getThis(int[] arr)
    {
       int this = 0;
       for (int value : arr)
       {
           this = this + value;
       }
       return this;
    }

    public static void main(String[] args)
    {
        int[] a1 = {1, 2, 5, 3};
        System.out.println("Your answer is: " + getThis(a1));
    }
}

  Error

  Your answer is: 1,2,5,3

  Your answer is: 11

  Your answer is: 1

 7. What is the output of the following code?
public class Test1
{
    public static void main(String[] args)
    {
            String[] stArr1 = {"Adam", "Eve", "Cain", "Abel", "Seth"};
        for (int i = 1; i < stArr1.length; i+=2)
        {
            System.out.println(stArr1[i]);
        }
    }
}

  Adam,Eve,

  Eve,Seth

  Eve,Abel

  Adam.Cain,Seth

 8. What is the output of the following code?
public class Test
{
    public static int getThis(String[] strArr)
    {
        int sum = 0;
        for (String str : strArr)
        {
            sum = sum + str.length();
        }
        return sum;
    }

    public static void main(String[] args)
    {
        String[] strArr = {"Test", "and", "Track"};
        System.out.println(getThis(strArr));
    }
}

 9. What should go in line 10 so that the method finds and returns the minimum value in the array?
public class Test
{

    public static int findMin(int[] arr)
    {
         int min = arr[0];
         int value = 0;
         for (int i = 1; i < arr.length; i++)
         {
             ??????????????????????
             if (value < min)
             {
                 min = value;
             }
          }
          return min;
    }

    public static void main(String[] args)
    {
        int[] arr = {20, -3, 18, 55, 4};
        System.out.println(findMin(arr));
    }
}

  value = arr[i];

  if value < arr[i];

  if value > min;

  value = arr[0];

 10. What should go on line 11 to calculate and return the average of all of the values in the array. (20.0, in this case)?
public class Test
{

    public static double getAverage(int[] arr)
    {
        double total = 0;
        for (int value : arr)
        {
            total = total + value;
        }
        ??????????????????????????
    }

    public static void main(String[] args)
    {
        int[] arr = {20, 3, 18, 55, 4};
        System.out.println(getAverage(arr));;
    }
}

  return total / arr.length;

  return arr.length/total;

  return value /arr.length

  return total / value