Module 4

¡Supera tus tareas y exámenes ahora con Quizwiz!

The _____ case defined in a function stops recursion. loop terminal base start

base

Which XXX will complete the code? public class RecursiveCalls { public static int convertToBinary(int num) { if (num == 0) { return 0; } else { return (num % 2 + 10 * convertToBinary(num / 2)); } } public static void main (String [] args) { System.out.print("The Binary number is: "); System.out.println(convertToBinary(7)); } } convertToBinary(num / 2); convertToBinary(num * 2); convertToBinary(num); convertToBinary();

convertToBinary(num / 2);

A stack overflow typically causes a program to _____. give an incorrect output crash return a value give multiple outcomes

crash

A stack overflow typically causes a program to _____. give multiple outcomes crash return a value give an incorrect output

crash

Identify the base case in the following code. public class FindMatch { public static int findMatch(char array[], int low, int high, char key) { if (high >= low) { int mid = low + (high - low) / 2; if (array[mid] == key) { return mid; } if (array[mid] > key) { return findMatch(array, low, mid, key); } else { return findMatch(array, mid + 1, high, key); } } return -1; } } if (high>=low) if (array[mid] == key) int mid = low + (high - low)/2; if (array[mid] > key)

if (array[mid] == key)

Which XXX will lead to stack overflow? public class RecursionExample { public static void reverseCount(int locNum) { XXX { locNum = locNum-1; System.out.println("hello"); reverseCount(locNum); } } public static void main(String[] args) { reverseCount(3); } } if(locNum == 3) if(locNum > 3) if(locNum <= 3) if(locNum > 0)

if(locNum <= 3)

Binary search begins at the ______ of the range. first element midpoint given index last element

midpoint

Which of the following recursive methods will result in a stack overflow when main() calls fact(10)? public static int fact(int n) { if (n != 100) return 1; else return n*fact(n-1); } public static int fact(int n){ if (n <= 100) return 1; else return n*fact(n-1); } public static int fact(int n) { if (n < 100) return 1; else return n*fact(n-1); } public static int fact(int n) { if (n == 100) return 1; else return n*fact(n-1); }

public static int fact(int n) { if (n == 100) return 1; else return n*fact(n-1); }

After the following code prints "yz", what is the next call to remix()? public class RemixChars { public static void remix(String rmn, String chsn) { if (rmn.isEmpty()) { System.out.println(chsn + rmn); } else { for (int i = 0; i < rmn.length(); ++i) { remix((rmn.substring(0, i) + rmn.substring(i + 1, rmn.length())), (chsn + rmn.charAt(i))); } } } public static void main(String args[]) { remix("yz", ""); } } remix("", "yz"); remix("yz", ""); remix("z", "y"); remix("y", "z");

remix("y", "z");

What is output? public class RecursionExample { public static void printer() { System.out.println("hello"); printer(); } public static void main(String[] args) { printer(); } } hello hello hello hello hello hello stack overflow

stack overflow

What is output? public class WordScramble { public static void wordScramble(String rem, String scr) { if (rem.isEmpty()) { System.out.println(scr + rem); } else { for (int i = 0; i < rem.length(); ++i) { wordScramble((rem.substring(0, i) + rem.substring(i + 1, rem.length())), (scr + rem.charAt(i))); } } } public static void main(String args[]) { wordScramble("123", ""); } } 123 132 123 132 213 231 312 321 123 321 312 231 213 132 123

123 132 213 231 312 321

How many times is numberOfStudents() called if main() calls numberOfStudents(9)? public static void numberOfStudents(int num) { if (num >= 10) { System.out.println(num); } else { System.out.print("Bye"); numberOfStudents(num+1); } } 9 1 10 2

2

What is output? public class MathRecursive { public static void myMathFunction(int a, int r, int counter) { int val; val = a*r; System.out.print(val+" "); if (counter > 4) { System.out.print("End"); } else { myMathFunction(val, r, counter + 1); } } public static void main (String [] args) { int mainNum1 = 1; int mainNum2 = 2; int ctr = 0; myMathFunction(mainNum1, mainNum2, ctr); } } 2 4 8 16 32 2 4 8 16 32 End 2 4 8 16 32 64 End 2 2 2 2 2

2 4 8 16 32 64 End

How many times is the function wordScramble() called in the given program? public class WordScramble { public static void wordScramble(String rem, String scr) { if (rem.isEmpty()) { System.out.println(scr + rem); } else { for (int i = 0; i < rem.length(); ++i) { wordScramble((rem.substring(0, i) + rem.substring(i + 1, rem.length())), (scr + rem.charAt(i))); } } } public static void main(String args[]) { wordScramble("ab", ""); } } 3 5 4 2

5

What is output for countNum(5);? public class CountNum { public static void countNum(int num) { if (num <= 0) { System.out.println("Done"); } else { System.out.print(num + " "); countNum(num - 2); } } } 5 3 1 -1 Done 5 3 1 Done 5 4 3 2 1 Done 5 3 1 0 Done

5 3 1 Done

What is output? public class RecursiveFactorial { public static int factorial(int factValue) { if (factValue <= 0) { return 1; } else { return factValue*factorial(factValue-1); } } public static void main (String [] args) { int userVal = -5; System.out.println("Factorial = " + factorial(userVal)); } } Factorial = -120 Error Factorial = 1 Factorial = 120

Factorial = 1


Conjuntos de estudio relacionados

Turning point questions exam 2 H&I

View Set

Endocrine + Diabetes (Brunner 45, uWorld)

View Set

Match Given Mollusc to Correct Molluscan Class

View Set

TEST: ATOMIC AND NUCLEAR PHYSICS

View Set

Utah Life and Health Exam- Chapter 5: Annuities

View Set

Chapter 7 & 8 Principles of Accounting Homework

View Set

Module 9 Knowledge Check (FINAL)

View Set