Define recursion.
a method that calls itself.
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[];
How do you get the length of an ArrayList?
ar.Size()
Name 3 types of loops and explain them
While loops
For loops
For each (enhanced for loops)
Do while loops etc...
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
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++) {
How do you add, delete, and replace an item in an arraylist?
al.add("Added item");
al.remove(4);
al.set(3, "replaced item");
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);
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
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 + " ");
}
}
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();
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
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
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] + ", ");
}
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;
}
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 + " ");
}
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
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) {
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
What does marks look like after running this code?
int marks[] = { 10, 50, 60 };
for (int num : marks)
{
num = num*2;
}