CSCI Flashcards

test (40 cards)

1
Q

What is a base case in recursion?

A

The condition under which a method stops calling itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does this method do?
public static int sum(int n) {
if(n == 1) return 1;
return n + sum(n - 1);
}

A

Returns the sum of all integers from 1 to n.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the output of:
public static void recurse(int n) {
if(n == 0) return;
System.out.print(n + “ “);
recurse(n - 1);
}
recurse(3);

A

3 2 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the output of:
public static void printReverse(int n) {
if(n == 0) return;
printReverse(n - 1);
System.out.print(n + “ “);
}
printReverse(3);

A

1 2 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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));

A

12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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);

A

60

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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);
}

A

Returns true if the array is a palindrome by comparing elements from both ends inward.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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<>());

A

[1, 2, 3, 1, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the extends keyword do?

A

It indicates a class is inheriting from another class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is dynamic method dispatch?

A

When a superclass reference calls a method that is overridden by the subclass at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is printed?
GeometricObject[] objs = new GeometricObject[5];
objs[0] = new Square(2);
if (objs[0] instanceof Colorable)
((Colorable)objs[0]).howToColor();

A

Color all four sides

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does instanceof check?

A

Whether an object is an instance of a given class or interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an abstract class in Java?

A

A class that cannot be instantiated and may contain abstract methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens if a subclass doesn’t implement all abstract methods?

A

The subclass must be declared abstract.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can an abstract class have a constructor?

A

Yes, and it can be called by subclass constructors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can an abstract class implement an interface?

A

Yes, and it may choose to implement some or none of the interface’s methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Which methods must a class implement if it declares implements Comparable<GeometricObject>?</GeometricObject>

A

The compareTo method.

18
Q

What’s the purpose of clone() in GeometricObject?

A

To create a deep copy of the object.

19
Q

What is the output of the following code?
int y = (x > 0) ? 15 : 10;
do {
System.out.print(“Drive”);
} while (age < 16);

A

Prints ‘Drive’ at least once, then repeats as long as age < 16.

20
Q

What is the result of the expression (6 + 3 * 2) > 4 * (5 + 1) - 3 || (8 - 15 <= 2 * 4)?

21
Q

What does (a + b) == c return if a = “Welcome”, b = “ to Java”, c = “Welcome to Java”?

22
Q

What does (a + b).equals(c) return if a = “Welcome”, b = “ to Java”, c = “Welcome to Java”?

23
Q

What is the output of:
int i = 2;
do { i += 2; } while (i < 20);
System.out.print(i);

24
Q

What is wrong with int i = 1, j = 2, k = 3; i = 1 - j = (1 == (k = 1));?

A

It’s an invalid assignment — cannot assign a value to an expression.

25
What is 73 in binary?
1001001
26
What is 73 in hexadecimal?
49
27
What is binary 101011 in decimal?
43
28
What is binary 101011 in hexadecimal?
2B
29
What does 37 % 6 return?
1
30
What is octal 127 in hexadecimal?
7F
31
What does the conditional operator x > 0 ? 15 : 10 return if x = 0?
10
32
Fix this code: double d = input.nextLong();
Use either long d = input.nextLong(); or double d = input.nextDouble();
33
What keyword replaces else in a switch statement?
default
34
What is the output of: for (int i = 0; i < 5; i++) if (i % 2 == 1) System.out.print(i + " ");
36894
35
What is the output of: int x = 2; int y = (x++) + (--x);
4
36
What is the output of: int x = 2; int y = (++x) + (++x);
7
37
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.
38
What happens if a subclass does not implement all methods from an abstract class?
The subclass must be declared abstract.
39
Can an abstract class implement an interface?
Yes and it may choose to implement some or none of the interface methods
40
What is the base case in recursion?
The condition under which a method stops calling itself.