CSAP recursion

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Consider the following method: public void mystery(int n) { int i; if (n <= 0) return; for (i = 0; i < n; i++) { System.out.print("-"); } for (i = 0; i < n; i++) { System.out.print("+"); } System.out.println(); mystery(n - 1); // Recursive call } What is the output when mystery(4) is called?

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

Consider the following recursive method: public static int mystery(int n) { if (n >= 5) return mystery(n - 2) - n / 2; else if (n <= 2) return (int) Math.pow(n, 2) - 1; else return n + mystery(n - 1); } What is the output when mystery(10)) is called?

-2

Consider the following method: public int mystery(int t) { if (t > 0) return 1; else return mystery(t + 2) - 2; } What is the output when mystery(-6) is called?

-7

Consider the following method: public void mystery(int a, int b) { System.out.print(a + " "); if (a <= b) mystery(a + 5, b - 1); } What is the output when mystery(0, 16)is called?

0 5 10 15

Consider the following recursive method. public int recur(int n) { if (n <= 10) return n * 2; else return recur(recur(n / 3)); } What value is returned as a result of the call recur(27)?

16

Consider the following recursive method. public static int mystery(int n) { if (n == 0) return 1; else return 3 * mystery(n - 1); } What value is returned as a result of the call mystery(5) ?

243

Consider the incomplete method powerOfgiven below. The call powerOf(n, x)should return the quantity nx. int powerOf(int base, int power) // precondition: power >= 1 // postcondition: returns basepower { int result; if (<expression1> == 1) { result = <expression2>; } else { result = <expression3> * powerOf(base, power - 1); } return result; } Which of the following could be used to replace <expression1>, <expression2>, and <expression3> so that powerOf will work as intended?

power base base

Consider the following method. /** Precondition: x > 0 */ public void mystery(int x) { System.out.print(x % 10); if ((x / 10) != 0) { mystery(x / 10); } System.out.print(x % 10); } Which of the following is printed as a result of the call mystery(1234) ?

43211234

Consider the following recursive method: public int mystery(int k) { if (k == 1) return 0; else return(1 + mystery(k / 2)); } Which of the following best characterizes the values of k for which the call mystery(k) leads to an infinate recursion?

All nonpositive values

Consider the following recursive method. public static void whatsItDo(String str) { int len = str.length(); if (len > 1) { String temp = str.substring(0, len - 1); whatsItDo(temp); System.out.println(temp); } } What is printed as a result of the call whatsItDo("WATCH")?

W WA WAT WATC


Set pelajaran terkait

Sadlier Vocab Level C Unit 3 Synonyms and Antonyms Sentences

View Set

Chapter 25: Complications of Pregnancy

View Set

1.2b & c: Cell Theory, Prokaryotes & Eukaryotes

View Set

Acute and chronic pyelonephritis

View Set

34 NURS 493 NGN Practice 1 SP 2024 (234)

View Set

molecular biology of the gene ch10

View Set