A recursive method and an iterative method both solve the same problem. Which of the following is a trade-off of choosing recursion over iteration?
Loading test…
Loading test…
15 questions
A recursive method and an iterative method both solve the same problem. Which of the following is a trade-off of choosing recursion over iteration?
What is the output of the following code?
ArrayList<Integer> a = new ArrayList<>();
a.add(1); a.add(2); a.add(3);
ArrayList<Integer> b = new ArrayList<>(a);
b.add(4);
System.out.println(a.size());
System.out.println(b.size());
What is the output of the following code?
int n = 100;
int count = 0;
while (n > 1) {
n /= 2;
count++;
}
System.out.println(count);
An abstract class Figure is defined as:
public abstract class Figure {
public abstract double area();
public void printArea() {
System.out.println("Area: " + area());
}
}
Which of the following statements is true?
What is the output of the following code?
public class Counter {
private int count;
public Counter(int start) {
count = start;
}
public Counter add(int n) {
count += n;
return this;
}
public int getCount() { return count; }
}
// In main:
Counter c = new Counter(0);
System.out.println(c.add(3).add(4).getCount());
A method receives a 2D array and should return the largest value in the array. Which implementation is correct?
public static int findMax(int[][] arr) {
int max = arr[0][0];
for (int[] row : arr) {
for (int val : row) {
if (val > max) max = val;
}
}
return max;
}
What is the value of n after the following code executes?
int n = (int)(Math.random() * 6) + 1;
What is the output of the following code?
int a = 2;
System.out.println(a++);
System.out.println(a);
Consider the following recursive method:
public static int sumArray(int[] arr, int index) {
if (index == arr.length) return 0;
return arr[index] + sumArray(arr, index + 1);
}
What is returned by sumArray(new int[]{3, 1, 4, 1, 5}, 0)?
What is the output of the following code?
int x = 0;
if (x = 5) {
System.out.println("hello");
}
What is the output of the following code?
int[][] g = new int[2][3];
for (int r = 0; r < g.length; r++) {
for (int c = 0; c < g[r].length; c++) {
g[r][c] = r + c;
}
}
System.out.println(g[1][2]);
What is the output of the following code?
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
System.out.println(arr[0] + " " + arr[2] + " " + arr[4]);
What is printed by the following code?
public class Temp {
private double celsius;
public Temp(double c) { celsius = c; }
public double toFahrenheit() {
return celsius * 9 / 5 + 32;
}
public String toString() {
return celsius + "C";
}
}
// In main:
Temp t = new Temp(100);
System.out.println(t);
System.out.println(t.toFahrenheit());
What is the output of the following code?
int[][] t = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int c = 0; c < t[0].length; c++) {
for (int r = 0; r < t.length; r++) {
System.out.print(t[r][c] + " ");
}
}
What is the output of the following code?
int x = 5;
while (true) {
if (x <= 0) break;
System.out.print(x + " ");
x -= 2;
}