CSCI Flashcards
test (40 cards)
What is a base case in recursion?
The condition under which a method stops calling itself.
What does this method do?
public static int sum(int n) {
if(n == 1) return 1;
return n + sum(n - 1);
}
Returns the sum of all integers from 1 to n.
What is the output of:
public static void recurse(int n) {
if(n == 0) return;
System.out.print(n + “ “);
recurse(n - 1);
}
recurse(3);
3 2 1
What is the output of:
public static void printReverse(int n) {
if(n == 0) return;
printReverse(n - 1);
System.out.print(n + “ “);
}
printReverse(3);
1 2 3
What does this return?
public static int func(int n) {
if(n == 0) return 0;
return n + func(n - 2);
}
System.out.println(func(6));
12
What does the following method return?
public static int productPositives(int[] a, int i) {
if (i == a.length) return 1;
return (a[i] > 0 ? a[i] : 1) * productPositives(a, i + 1);
}
productPositives(new int[]{1, -4, -3, 3, 5, 4}, 0);
60
What does this recursive palindrome method do?
public static boolean isPalindrome(int[] a, int low, int high) {
if (high <= low) return true;
if (a[low] != a[high]) return false;
return isPalindrome(a, low + 1, high - 1);
}
Returns true if the array is a palindrome by comparing elements from both ends inward.
What does this recursive method return?
public static ArrayList<Integer> eliminateConsecutiveDuplicates(int[] a, int I,
ArrayList<Integer> result) {
if (i >= a.length) return result;
if (result.isEmpty() || result.get(result.size() - 1) != a[i])
result.add(a[i]);
return eliminateConsecutiveDuplicates(a, i + 1, result);
}</Integer></Integer>
Call: eliminateConsecutiveDuplicates(new int[]{1, 1, 2, 3, 3, 1, 5}, 0, new ArrayList<>());
[1, 2, 3, 1, 5]
What does the extends keyword do?
It indicates a class is inheriting from another class.
What is dynamic method dispatch?
When a superclass reference calls a method that is overridden by the subclass at runtime.
What is printed?
GeometricObject[] objs = new GeometricObject[5];
objs[0] = new Square(2);
if (objs[0] instanceof Colorable)
((Colorable)objs[0]).howToColor();
Color all four sides
What does instanceof check?
Whether an object is an instance of a given class or interface.
What is an abstract class in Java?
A class that cannot be instantiated and may contain abstract methods.
What happens if a subclass doesn’t implement all abstract methods?
The subclass must be declared abstract.
Can an abstract class have a constructor?
Yes, and it can be called by subclass constructors.
Can an abstract class implement an interface?
Yes, and it may choose to implement some or none of the interface’s methods.
Which methods must a class implement if it declares implements Comparable<GeometricObject>?</GeometricObject>
The compareTo method.
What’s the purpose of clone() in GeometricObject?
To create a deep copy of the object.
What is the output of the following code?
int y = (x > 0) ? 15 : 10;
do {
System.out.print(“Drive”);
} while (age < 16);
Prints ‘Drive’ at least once, then repeats as long as age < 16.
What is the result of the expression (6 + 3 * 2) > 4 * (5 + 1) - 3 || (8 - 15 <= 2 * 4)?
True
What does (a + b) == c return if a = “Welcome”, b = “ to Java”, c = “Welcome to Java”?
false
What does (a + b).equals(c) return if a = “Welcome”, b = “ to Java”, c = “Welcome to Java”?
true
What is the output of:
int i = 2;
do { i += 2; } while (i < 20);
System.out.print(i);
20
What is wrong with int i = 1, j = 2, k = 3; i = 1 - j = (1 == (k = 1));?
It’s an invalid assignment — cannot assign a value to an expression.