Chapter 13 Reading Quiz

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the output when mystery2(4) is called? a -+ --++ ---+++ ----++++ b ----++++ c ---- ++++ --- +++ -- ++ - + d ----++++ ---+++ --++ -+

-+ --++ ---+++ ----++++

What is output when mystery(4) is called? a -+ --++ ---+++ ----++++ b ----++++ ---+++ --++ -+ c ---- ++++ --- +++ -- ++ - + d ----++++

----++++ ---+++ --++ -+

The method below returns the String text in reverse. For example reverse("Hello") returns "olleH". Which statement should represent the base case? a len b text c "text" d ""

text

How many recursive calls are made from the original call powerOfTwo(63) (not including the original call)? Answer:

0

Consider the recursive method below. What is printed for the call myPrint(821)? Answer: public static void myPrint(int n) { if (n < 10) { System.out.print(n); } else { int m = n % 10; System.out.print(m); myPrint(n / 10); } }

128

Consider the following recursive code. What value is returned from a call to mystery(3, 6)? Answer: public static int mystery(int n, int m) { if (n <= 0) return 0; if (n == 1) return m; return m + mystery(n - 1, m); }

18

Consider the following recursive code. What value is returned from a call to mystery(1,5)? Answer: public static int mystery(int n, int m) { if (n <= 0) return 0; if (n == 1) return m; return m + mystery(n - 1, m); }

5

How many recursive calls are made from the original call of powerOfTwo(64)(not including the original call)? Answer:

6

Consider the recursive method below. What is printed for the call myPrint(8)? Answer: public static void myPrint(int n) { if (n < 10) { System.out.print(n); } else { int m = n % 10; System.out.print(m); myPrint(n / 10); } }

8

Not counting the original call, how many recursive calls to the fib method shown below would be made from an original call to fib(4)? Answer: public static int fib(int n) { if (n <= 1) return n; else return (fib(n - 1) + fib(n - 2)); }

8

With regards to recursion, what are base cases? a A condition that causes the recursion to terminate. b A condition that does not result in a recursive method call c A condition that handles the simplest computations directly d All of these

All of these

Consider the code for the recursive method. What is returned by the call mysteryPrint(-4) ? public static int mysteryPrint(int n) { if (n == 0) return 0; else return (n + mysteryPrint(n-1)); } a 0 b -10 c -22 d Nothing - infinite recursion will occur, causing a stack overflow error

Nothing - infinite recursion will occur, causing a stack overflow error

Consider the method powerOfTwo shown below. What is the best interpretation of line #1? a Any multiple of one is a power of two b Any multiple of two is a power of two c One is odd d One is a power of 2

One is a power of 2

Even though a recursive method might run a little slower than its equivalent iterative method, the recursive solution is often easier to understand and implement. True False

True

Consider the following code snippet for calculating Fibonacci numbers recursively: public static int fib(int n) { // assumes n >= 0 if (n <= 1) { return n; } else { return (fib(n - 1) + fib(n - 2)); } } Identify the terminating condition. a fib(n - 1) + fib(n - 2) b fib(n - 1) c n <= 1 d n > 1

n <= 1

Complete the code for the recursive method printSum shown in this code snippet, which is intended to return the sum of digits from 1 to n: public static int printSum(int n) { if (n == 0) return 0; else { ______________________________ } } a return (printSum(n - 1)); b return (n + printSum(n - 1)); c return (n + printSum(n + 1)); d return (n - printSum(n - 1));

return (n + printSum(n - 1));

The method below returns the String text in reverse. For example reverse("Hello") returns "olleH". Which statement below represents the nonbase case? a reverse(text.substring(1,len)) + text.substring(1,1) b reverse(text.substring(1,len-1)) + text.substring(0,1) c reverse(text.substring(1,len)) + text.substring(0,1) d reverse(text(1,len)) + text(0)

reverse(text.substring(1,len)) + text.substring(0,1)


Kaugnay na mga set ng pag-aaral

Chapter 9 - Entry Strategies and Organizational Structures

View Set

Med. surg. practice questions (LIVER)

View Set

N123- Chp 50 Biliary disorder- Prep-U

View Set

Chap 6: Elements of Planning and Decision-making

View Set

ANATOMY EXAM 3- Neural Tissue 2.0

View Set

Cardiac Arrest and Post-Cardiac Arrest Care - Pre-Assessment

View Set