Recursion
Arrays
ArrayLists
Loops
100

Define recursion. 

a method that calls itself.

100

Which is not a valid way to initialise a 1D array?

(A) int[] list = new list[];
(B) int list = new list[];
(C) int[] list = new list[5];
(D) int[] list = {1, 2, 3, 4, 5}; 

(B) int list = new list[]; 

100

How do you get the length of an ArrayList?

ar.Size()


100

Name 3 types of loops and explain them

While loops

For loops

For each (enhanced for loops)

Do while loops etc...

200

Identify the base case and the base case's result of the following recursive method.

public int mystery(int n) {
     if (n == 1) {
          return 1;
     } else {
          return n * (mystery(n)) + 3;
     }
}

(A) 1, 4
(B) n == 1,  1
(C) n == 0, 4
(D) n, 1

(B) n == 1, 1 

200

Replace the following line to iterate through the 2D array. 

int[][] list = /* correctly initialised 2D array */

// missing code
     for (int r = 0; r < list.length; r++) {
          System.out.println(list[r][c]); 

     }
}

(A) for (int r = 0; r< list.length-1; r++) {
(B) for (int c = 0; c < list.length; c++) {
(C) for (int c = 0; c < list[0].length; c++) {
(D) for (int r = 0; r< list.length; r++) {

(C) for (int c = 0; c < list[0].length; c++) {

200

How do you add, delete, and replace an item in an arraylist?

al.add("Added item");

al.remove(4);

al.set(3, "replaced item");

200

Write a program to calculate the sum of 1-10

int sum = 0;       

 for(int i=1; i<=10; i++)        {            

     sum += i;        

}       

 System.out.println("Sum: " + sum);

300

What is returned when mystery(7, 6) is called?

public int mystery(int x, int y) {
     if (x <= y) {
          return x + y;
     } else {
          return mystery(x, x + y) + 2;
     }
}

22

300

Finish the following code so that it prints out all of the odd values in the array a1

 int[] a1 = {0, 3, 6, 7, 9, 10};

        for (int value : a1)

        {

        }

 

int[] a1 = {0, 3, 6, 7, 9, 10};

        for (int value : a1)

        {

            if (value % 2 == 1)

            {

                System.out.print(value + " ");

            }

        }

300

Which of the following is the correct way to declare an ArrayList?

(A) ArrayList<Integer> = new ArrayList();
(B) ArrayList list;
(C) ArrayList<integer> = new ArrayList();
(D) ArrayList<Int> = new ArrayList();

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

300

What is the difference between a while and a do-while loop?

While loop checks the condition before the execution of the statement(s) whereas the do-while loop ensures that the statement(s) is executed at least once before evaluating the condition

400

Which of the following situations would be best suited for a recursive method?

(A) sorting a list
(B) assigning a random student ID
(C) multiplication 
(D) finding the sum of a list

(C) multiplication

400

Fix the following code so that it prints every other value in the array arr1
int arr1 = (1, 3, 7, 9, 15, 17);

for (int index = 0; index <= arr1.length; index+=2){

       System.out.print(index + ", ");

   }


int[] arr1 = {1, 3, 7, 9, 15};

for (int index = 0; index < arr1.length; index+=2){

            System.out.print(arr1[index] + ", ");

}

400

Write a method that takes an ArrayList of Integers and returns a reversed version of the ArrayList.

public ArrayList<Integer>reverse(ArrayList<Integer> list) {
        ArrayList<Integer> reversedList = new ArrayList<>();
        for (int i = list.size() - 1; i >= 0; i--) {
            reversedList.add(list.get(i));
        }
        return reversedList;
    }  

400

Create an enhanced (for-each) loop that iterates through an array

int ar[] = { 10, 50, 60, 80, 90 };

        for (int element : ar){

        System.out.print(element + " ");

}

500

How many stars will the following print?

public void printStars(int n) {
     if (n == 1) {
          System.out.println("*");
     } else {
          System.out.println("*");
          printStars(n-1);
     }
}

printStars(20); 

(A) 20
(B) 15
(C) 19
(D) none

(A) 20

500

Fill in the missing line to correctly finish the method sum that is designed to return the sum of a given 2D array. 

public static int sum(int[][] nums) {
     int count = 0;  
     // missing code
          for (int c : row) {
               count += c;
          }

     }
     return count;
}

(A) for (int i = 0; i < nums.length; i++) {
(B) for (row : nums) {
(C) for (int[] row : nums) {
(D) for (int row : nums) {

(C) for (int[] row : nums) {

500

Which of the following would work to remove all the 3s from a given ArrayList?

I.
public static ArrayList<Integer> removeThree(ArrayList<Integer> arr) {
     for (Integer num : arr) {
          if (num == 3) {
               arr.remove(num);
          }
     }
     return arr;
}

II.
public static ArrayList<Integer> removeThree(ArrayList<Integer> arr) {
     for (int i = 0; i < arr.size()-1; i++) {
         if (arr.get(i) == 3) {
              arr.remove(i);
         }
     }
     return arr;
}

III.
public static ArrayList<Integer> removeThree (ArrayList<Integer> arr) {
     for (int i = 0; i < arr.size()-1; i++) {
          if (arr[i] == 3) {
               arr.remove(i);
          }
     }
     return arr;
}

(A) I only
(B) II only
(C) III only
(D) I, II, and II

(B) II only

500

What does marks look like after running this code?

int marks[] = { 10, 50, 60 };

for (int num : marks)
{
    num = num*2;
}