CSE205 - Exam 2 - Quiz 8 (Recursion)

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

Refer to the following recursive factorial method. public int factorial(int x) { if (x > 1) return x * factorial (x - 1); else return 1; }

6

Use the following recursive method. public int question(int x, int y) { if (x == y) return 0; else return question(x-1, y) + 1; } Calling this method will result in infinite recursion if which condition below is initially true?

(x<y)

Use the following recursive method. public int question(int x, int y) { if (x == y) return 0; else return question(x-1, y) + 1; } If the method is called as question(8, 3), what is returned?

5

Which of the following statements is true?

Recursion should be used in every program. Recursion should be avoided all the time. (Solutions that are easily expressed recursively should be programmed recursively.)*correct answer Solutions that are easily expressed recursively should be programmed iteratively. None of the above.

Which of the following will result from infinite recursion in Java?

The program will run out of memory.

In a recursive solution, _______________ is(are) always necessary.

a base case

__________________ recursion results from the lack of a base case.

infinite

What is wrong with the following recursive sum method? The method is supposed to sum up the values between 1 and x (for instance, sum(5) should be 5 + 4 + 3 + 2 + 1 = 15). public int sum(int x) { if (x == 0) return 0; else return sum(x - 1) + x; }

the base case condition should be (x <= 0) instead of (x = = 0)

The Towers of Hanoi puzzle cannot be solved iteratively.

False

What does the following method compute? Assume the method is called initially with i = 0 public int question(String a, char b, int i) { if (i == a.length( )) return 0; else if (b == a.charAt(i)) return question(a, b, i+1) + 1; else return question(a, b, i+1); }

The number of times char b appears in String a


Set pelajaran terkait

Identify five strategies a classroom teacher can use to promote communicative skills and enhance independence in the transition to adulthood.

View Set

Chapter 02: Expanded Tax Formula, Form 1040, and Basic Concepts - Smartbook

View Set

PrepU: Chapter 48: Nursing Care of the Child With an Alteration in Metabolism/Endocrine Disorder

View Set

NR 326 Week 1 EAQ Practice Questions

View Set

World Geography Chapter 12 Study Guide

View Set

Chapter 26: The Reproductive System

View Set