Self Quiz Week 2

What are two parts to recursion?

Select one:

a. (1) If the problem is easy, solve it immediately, and (2) If the problem can't be solved immediately, divide it into smaller problems. 
b. (1) Divide the problem into smaller problems, and (2) give immediate solutions for the hard problems.
c. (1) Discard the hard cases , and (2) solve the easy easy cases.
d. (1) Solve the problem by asking it to solve itself, (2) Solve the easy cases in one step.

The correct answer is: (1) If the problem is easy, solve it immediately, and (2) If the problem can't be solved immediately, divide it into smaller problems.


Question 2

How can you drink an entire keg of root beer?

Select one:

a. (1) take one swallow, then (2) take another swallow.
b. (1) If the keg is empty do nothing, otherwise (2) take one swallow, then drink the rest of the keg. 
c. (1) take one enormous gulp, and (2) wish you hadn't.
d. (1) drink one keg, and (2) drink another keg.

The correct answer is: (1) If the keg is empty do nothing, otherwise (2) take one swallow, then drink the rest of the keg.


Question 3

 How do you study a text book?

Select one:
a. (1) Read the book on day 1, and (2) read it again each day of the semester.
b. (1) If you have reached the end of the book you are done, else (2) study one page, then study the rest of the book. 
c. (1) Divide the book in two, and (2) study each half.
d. (1) Cram all the pages in one horrible session, and (2) forget everything the next night.

The correct answer is: (1) If you have reached the end of the book you are done, else (2) study one page, then study the rest of the book.



Question 4

Which answer is a correct skeleton for a recursive Java method?

A.
int solution( int N )
{
  if ( base case )
  {
    return something easily computed
  }
  else
  {
    divide problem into pieces
    return something calculated from the solution to each piece 
  }
}

B.
int solution( int N )
{
  if ( base case )
  {
    return something easily computed
  }
  else
  {
    return solution(N) 
  }
}

C.
int solution( int N )
{
  divide problem into pieces
  return something calculated from the solution to each piece 
}

D.
int solution( int N )
{
  divide problem into pieces

  if ( base case )
  {
    return something easily computed
  }
  else
  {
    return something calculated from the solution to each piece 
  }
}

Select one:
a. Correct
b.
c.
d.

The correct answer is: a.


Question 5

Which of the following statements are true?

Select one:

a. The Fibonacci series begins with 0 and 1, and each subsequent number is the sum of the preceding two numbers in the series. 
b. The Fibonacci series begins with 1 and 1, and each subsequent number is the sum of the preceding two numbers in the series.
c. The Fibonacci series begins with 1 and 2, and each subsequent number is the sum of the preceding two numbers in the series.
d. The Fibonacci series begins with 2 and 3, and each subsequent number is the sum of the preceding two numbers in the series.

The correct answer is: The Fibonacci series begins with 0 and 1, and each subsequent number is the sum of the preceding two numbers in the series.



Question 6

In the following method, what is the base case?

static int xMethod(int n) {
    if (n == 1)
       return 1;
    else
       return n + xMethod(n - 1);
}

Select one:
a. n is 1 
b. n is greater than 1.
c. n is less than 1.
d. no base case.

The correct answer is: n is 1



Question 7

Consider the following two programs:

A.
public class Test {
     public static void main(String[] args) {
          xMethod(5);
     }

     public static void xMethod(int length) {
          if (length > 1) {
               System.out.print((length - 1) + " ");
               xMethod(length - 1);
          }
     }
}

B.
public class Test {
     public static void main(String[] args) {
          xMethod(5);
     }

     public static void xMethod(int length) {
          while (length > 1) {
               System.out.print((length - 1) + " ");
               xMethod(length - 1);
          }
     }
}

Select one:
a. The two programs produce the same output 5 4 3 2 1.
b. The two programs produce the same output 1 2 3 4 5.
c. The two programs produce the same output 4 3 2 1.
d. The two programs produce the same output 1 2 3 4.
e. Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 .... 1 infinitely 

The correct answer is: Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 .... 1 infinitely



Question 8
What code is missing to complete the following method for sorting a list?

public static void sort(double[] list) {
___________________________;
}

public static void sort(double[] list, int high) {
   if (high > 1) {
      // Find the largest number and its index
      int indexOfMax = 0;
      double max = list[0];
      for (int i = 1; i <= high; i++) {
          if (list[i] > max) {
              max = list[i];
              indexOfMax = i;
         }
      }

// Swap the largest with the last number in the list
list[indexOfMax] = list[high];
list[high] = max;

// Sort the remaining list
sort(list, high - 1);
}
}

Select one:
a. sort(list)
b. sort(list, list.length)
c. sort(list, list.length - 1) 
d. sort(list, list.length - 2)

The correct answer is: sort(list, list.length - 1)


Question 9

For a linked list to be used in a program, that program needs:

i. A variable that refers to the first node in the list.
ii. A pointer to the first node.
iii. A null pointer in the last node.

Select one:

a. i and ii 
b. i
c. ii and iii
d. i, ii and iii

The correct answer is: i and ii



Question 10

Suppose cursor refers to a node in a linked list (using the IntNode class with instance variables called data and link). What statement changes cursor so that it refers to the next node?

Select one:

a. cursor++;
b. cursor = link;
c. cursor += link;
d. cursor = cursor.link; 


The correct answer is: cursor = cursor.link;


No comments:

Post a Comment