Exam 11: Recursion

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

Write a recursive method to compute the factorial of a number.

Free
(Essay)
4.9/5
(44)
Correct Answer:
Verified

public static int factorialint n)
{
ifn < 0)
{
System.out.println"Sorry negative numbers not allowed.");
System.exit0);
}
ifn == 1)//base case
return1);
else
return n * factorialn-1));
}

The underlying data structure used by the computer during recursion is a:

Free
(Multiple Choice)
4.7/5
(43)
Correct Answer:
Verified

D

Write a recursive method to compute the power of x< sup >n< /sup > for non-negative n.

Free
(Essay)
4.8/5
(36)
Correct Answer:
Verified

public static int powerint x,int n)
{
ifn < 0)
{
System.out.println"Illegal argument to power");
System.exit0);
}
ifn > 0)
return powerx,n-1)* x);
else
return1);//base case
}

The binary search algorithm is extremely slow compared to an algorithm that simply tries all array elements in order.

(True/False)
4.9/5
(33)

The following code for the method factorial)applies to the next two questions: public static double factorial double n) { If n == 0) { Return 1; } Else { Return n * factorialn-1); } } What is the value returned when the integer 3 is the argument to the factorial method?

(Multiple Choice)
5.0/5
(43)

How does the computer system handle a recursive method call?

(Essay)
4.8/5
(45)

A recursive solution can be preferable to an iterative solution because:

(Multiple Choice)
4.8/5
(39)

A recursively written method will usually run slower and use more storage than an equivalent iterative version.

(True/False)
4.8/5
(42)

What are the two base cases for a recursive binary search algorithm?

(Essay)
4.8/5
(45)

When defining recursive valued methods you should:

(Multiple Choice)
4.9/5
(39)

Write an iterative method to compute the power of x< sup >n< /sup > for non-negative n.

(Essay)
4.8/5
(31)

Activation records are used to implement recursion.

(True/False)
4.7/5
(45)

Binary search is a divide and conquer algorithm.

(True/False)
4.9/5
(38)

What is the value returned when the integer 5 is the argument to the factorial method?

(Multiple Choice)
4.9/5
(32)

What is an activation record?

(Essay)
4.9/5
(43)

The portion of memory in which a recursive computation is stored is called a/an:

(Multiple Choice)
4.9/5
(37)

What is a stack overflow?

(Essay)
4.8/5
(44)

Write an iterative method to compute the factorial of a number.

(Essay)
4.8/5
(33)

Write a recursive method to print a string backwards.

(Essay)
5.0/5
(37)

Explain how a sequential search works.

(Essay)
4.8/5
(42)
Showing 1 - 20 of 44
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)