Assessment 2 Flashcards
Given the following code:
public void countDown(int leadTime, String message)
{ System.out.println(leadTime + "..."); if (leadTime < 5) countDown(leadTime + 1, message); else System.out.println(message); }
If client code calls countDown(0,”GAMEOVER”), what is the order of the subsequent method calls?
a) countDown(4,”GAMEOVER”), countDown(3,”GAMEOVER”), countDown(2,”GAMEOVER”), countDown(1,”GAMEOVER”), countDown(0,”GAMEOVER”)
b) countDown(1,”GAMEOVER”), countDown(2,”GAMEOVER”), countDown(3,”GAMEOVER”), countDown(4,”GAMEOVER”), countDown(5,”GAMEOVER”)
c) countDown(1,”GAMEOVER”), countDown(2,”GAMEOVER”), countDown(3,”GAMEOVER”), countDown(4,”GAMEOVER”)
d) countDown(4,”GAMEOVER”), countDown(3,”GAMEOVER”), countDown(2,”GAMEOVER”), countDown(1,”GAMEOVER”)
countDown(1,”GAMEOVER”), countDown(2,”GAMEOVER”), countDown(3,”GAMEOVER”), countDown(4,”GAMEOVER”), countDown(5,”GAMEOVER”)
Given the following code:
public static int powerOf5(int exponent){
if (exponent == 0)
return 1;
else
return 5 * powerOf5(exponent -1);
}
what is powerOf5(3)
a) 25
b) 125
c) 15
d) 1
e) Infinite recursion
125
Given fields:
private Node firstNode; // Reference to first node
private int numberOfEntries;
And the following code:
private void displayChain(Node curr) {
if (curr != null) { displayChain(curr.getNext()); System.out.println(curr.getData()); } }
Indicate how each private helper member method would print a non-empty chain
a) In reverse order
b) Neither
c) In order
In reverse order
Given fields:
private Node firstNode; // Reference to first node
private int numberOfEntries;
And the following code:
private void displayChain(Node curr) {
if (curr != null) { System.out.println(curr.getData()); displayChain(curr.getNext()); } }
Indicate how each private helper member method would print a non-empty chain
a) In reverse order
b) Neither
c) In order
In order
Given fields:
private Node firstNode; // Reference to first node
private int numberOfEntries;
And the following code:
private void displayChain(Node curr) {
if (curr == null) System.out.println(curr.getData()); else displayChain(curr.getNext()); }
Indicate how the private helper member method would print a non-empty chain
a) Neither
b) In order
c) In reverse order
Neither
Given the following code:
public static void displayArray(int[] array, int first, int last)
{ if (first == last)
System.out.print(array[first] + “ “);
else
{
int mid = (first + last) /2; displayArray(array, first, mid); displayArray(array, mid + 1, last); }
}
With the call displayArray ({4,5,6,7,8,9,10,11},0,7), how many recursive calls would be made?
a) 22
b) 3
c) 7
d) 14
14
Consider the following recursive method:
public int examMethod(int n) {
if (n == 1) return 1;
else return (n + this.examMethod(n-1));
}
What value is returned by the call examMethod(16)?
a) None of these
b) 16
c) 1
d) 136
136
How many times will F be called if n = 4, ignoring F(4) itself?
public static long F(int n) {
if (n == 0)return 0;
if (n == 1) return 1;
return F(n-1) + F(n-2);
}
Correct answer:
a) 8
b) 9
c) 12
d) 14
8
public class RecursiveMath
…
public int fib (int a) {
if (a >= 2) return fib(a-1) + fib(a-2); return a;
}
…
}
What is the base case for fib in the above definition?
fib(a-1) + fib(a-2);
None of these
fib(a) for a >= 2
int a
fib(a) for a < 2
fib(a) for a < 2
public class RecursiveMath
…
public int fib (int a) {
if (a == 1) return 1; else return fib(a-1) + fib(a-2);
}
…
}
Given the above definition, what is the result of executing the following?
RecursiveMath bill = new RecursiveMath();
int x = bill.fib(-1);
a) x is set to undefined
b) None of these
c) The code cannot be executed, because it won’t compile
d) The code does not terminate
e) x is set to -1
The code does not terminate