Assessment 2 Flashcards

1
Q

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

A

countDown(1,”GAMEOVER”), countDown(2,”GAMEOVER”), countDown(3,”GAMEOVER”), countDown(4,”GAMEOVER”), countDown(5,”GAMEOVER”)

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

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

A

125

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

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

A

In reverse order

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

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

A

In order

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

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

A

Neither

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

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

A

14

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

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

A

136

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

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

A

8

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

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

A

fib(a) for a < 2

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

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

A

The code does not terminate

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