Chapter 19 Recursion and Method Redefinition

Similar documents
Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Recursion. Chapter 5

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Programming with Recursion. What Is Recursion?

1.7 Recursion. Department of CSE

CSE 143 Lecture 10. Recursion

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

CSE 143. Lecture 9: introduction to recursion reading: 12.1

UNIT 5A Recursion: Basics. Recursion

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Recursive Problem Solving

Lecture 8 Recursion. The Mirrors

APCS-AB: Java. Recursion in Java December 12, week14 1

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Recursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Chapter 10: Recursive Problem Solving

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Decision-Making and Repetition

Recursion. notes Chapter 8

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

CS200: Recursion and induction (recap from cs161)

Bachelor Level/ First Year/ Second Semester/ Science Full Marks: 60 Computer Science and Information Technology (CSc. 154) Pass Marks: 24

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

Chapter 13 Recursion. Chapter Objectives

EE 368. Weeks 4 (Notes)

Recursive Definitions

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Tribhuvan University Institute of Science and Technology Computer Science and Information Technology (CSC. 154) Section A Attempt any Two questions:

Chapter 6 Single-dimensional Arrays

Recursion. Chapter 7

Data Structures And Algorithms

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

Lecture 24 Tao Wang 1

CS/CE 2336 Computer Science II

q To develop recursive methods for recursive mathematical functions ( ).

q To develop recursive methods for recursive mathematical functions ( ).

Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems. COMP Week 8 & 9 1

Chapter 15: Recursion

CSE 142 Sample Midterm Exam #3

Lecture 19: Recursion

Chapter 12 Supplement: Recursion with Java 1.5. Mr. Dave Clausen La Cañada High School

Recursive Thinking. Chapter 8: Recursion. Recursive Definitions. Recursion. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 6 Recursion. The Concept of Recursion

Solving problems by recursion

Sorting & Searching (and a Tower)

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

CSE 214 Computer Science II Recursion

CSE 143 SAMPLE MIDTERM

COS 126 General Computer Science Fall Written Exam 1

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

CmpSci 187: Programming with Data Structures Spring 2015

CS302 - Data Structures using C++

Recursive Algorithms. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Chapter Objectives. List Processing. Chapter 10: Applications of Arrays and Strings. Java Programming: Program Design Including Data Structures

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Lecture P6: Recursion

Recursion. Chapter 17 CMPE13. Cyrus Bazeghi

We cover recursion in 150. Why do it again in 151?

CSE 373: Data Structures and Algorithms

Practical Questions CSCA48 Week 6

CSC 273 Data Structures

C Sc 227 Practice Test 2 Section Leader Your Name 100pts. a. 1D array b. PriorityList<E> c. ArrayPriorityList<E>

Chapter 17 Recursion

BBM 201 DATA STRUCTURES

CSE413 Midterm. Question Max Points Total 100

What is Recursion? Chapter 17 Recursion. Executing RunningSum. High-Level Example: Binary Search

COS 126 Midterm 1 Written Exam, Fall 2009

What is Recursion? ! Each problem is a smaller instance of itself. ! Implemented via functions. ! Very powerful solving technique.

UNIT 5A Recursion: Basics. Recursion

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

Fundamentals of Recursion. Solving Problems Recursively. Recursive example. Recursive example

CS171 Final Practice Exam

Recursion. Garfield AP Computer Science. As usual, significant borrowings from Stuart Reges and Marty Stepp at UW -- thanks!!

Analyzing Complexity of Lists

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

PROGRAMMING FUNDAMENTALS

Pace University. Fundamental Concepts of CS121 1

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

CSE 142 Sample Midterm Exam #3

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

Reduction & Recursion Overview

Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

Recursion. Readings: RS Chapter 12. Recursion. Recursion: The definition of an operation in terms of itself.

SOFTWARE DEVELOPMENT 1. Recursion 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Java Basics Programming Fundamentals

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Recursion. Thinking Recursively. Tracing the Recursive Definition of List. CMPT 126: Lecture 10. Recursion

Recursion. Based on Chapter 7 of Koffmann and Wolfgang. Chapter 7: Recursion 1


Linear Data Structure

DATA STRUCTURES AND ALGORITHMS. Sorting algorithms (insertion sort, bubble sort, selection sort)

Transcription:

Exposure Java Multiple Choice Test Chapter 19 Recursion and Method Redefinition DO NOT WRITE ON THIS TEST This test includes program segments, which are not complete programs. Answer such questions with the assumption that the program segment is part of a correct program. Objective 1 - Recursion Fundamentals 01. The process of a method calling itself is recursion. iteration. compiling. error checking. 02. You must have an exit or to stop the recursive process. compiler stack index pointer base case 03. Which of the following is a data structure that accesses information using the LIFO principle? compiler stack index pointer base case 04. The program execution sequence is handled by the compiler. stack. index pointer. base case. Exposure Java 2013, APCS Edition Chapter 19 Test Page 1 05-20-13

05. Consider the count method below. public static void count(int a, int b) if (a <= b) System.out.print(a + " "); count(a+1, b); What will be displayed by the method call count(10,20)? 10 11 12 13 14 15 16 17 18 19 20 10 11 12 13 14 15 16 17 18 19 20 19 18 17 16 15 14 13 12 11 10 19 18 17 16 15 14 13 12 11 10 (E) 20 19 18 17 16 15 14 13 12 11 10 10 11 12 13 14 15 16 17 18 19 20 06. Consider the count method below. public static void count(int a, int b) if (a <= b) count(a+1, b); System.out.print(a + " "); What will be displayed by the method call count(10,20)? 10 11 12 13 14 15 16 17 18 19 20 10 11 12 13 14 15 16 17 18 19 20 19 18 17 16 15 14 13 12 11 10 19 18 17 16 15 14 13 12 11 10 (E) 20 19 18 17 16 15 14 13 12 11 10 10 11 12 13 14 15 16 17 18 19 20 Exposure Java 2013, APCS Edition Chapter 19 Test Page 2 05-20-13

07. Consider the count method below. public static void count(int a, int b) if (a <= b) System.out.print(a + " "); count(a+1, b); System.out.print(a + " "); What will be displayed by the method call count(10,20)? 10 11 12 13 14 15 16 17 18 19 20 10 11 12 13 14 15 16 17 18 19 20 19 18 17 16 15 14 13 12 11 10 10 11 12 13 14 15 16 17 18 19 20 20 19 18 17 16 15 14 13 12 11 10 (E) 20 19 18 17 16 15 14 13 12 11 10 10 11 12 13 14 15 16 17 18 19 20 08. Consider the count method below. public static void count(int a, int b) if (a < b) System.out.print(a + " "); count(a+1, b); What will be displayed by the method call count(10,20)? 10 11 12 13 14 15 16 17 18 19 20 10 11 12 13 14 15 16 17 18 19 20 19 18 17 16 15 14 13 12 11 10 19 18 17 16 15 14 13 12 11 10 (E) 20 19 18 17 16 15 14 13 12 11 10 10 11 12 13 14 15 16 17 18 19 20 Exposure Java 2013, APCS Edition Chapter 19 Test Page 3 05-20-13

Objective 2 Evaluating Recursive Return Methods 09. What value does mystery1(5) return? 0 1 4 5 (E) 6 public int mystery1(int a) if (a==1) return 1; return mystery1(a-1); 10. What value does mystery2(7) return? 0 1 4 8 (E) 13 public int mystery2(int n) if (n <= 1) return 1; if (n % 2 == 1) return n + mystery2(n-1); return n - mystery2(n-1); 11. What value does mystery3(5,1) return? 1 4 5 7 (E) 8 public int mystery3(int a, int b) if (a < b) return a; return b + mystery3(a-1,b+1); 12. What value does mystery4(123,82) return? 1 41 82 121 (E) 242 public int mystery4(int A, int B) if (A % B == 0) return B; return mystery4(b, A % B); Exposure Java 2013, APCS Edition Chapter 19 Test Page 4 05-20-13

13. What value does mystery1(13) return? 0 1 12 13 (E) 14 public int mystery1(int a) if (a == 1) return 1; return mystery1(a-1); 14. What value does mystery2(8) return? 0 1 7 8 (E) 9 public int mystery2(int n) if (n <= 1) return 1; if (n % 2 == 1) return n + mystery2(n-1); return n - mystery2(n-1); 15. What value does mystery3(1,5) return? 1 4 5 7 (E) 8 public int mystery3(int a, int b) if (a < b) return a; return b + mystery3(a-1,b+1); 16. What value does mystery4(29,13) return? 1 13 26 29 (E) 58 public int mystery4(int a, int b) if (a % b == 0) return b; return mystery4(b, a % b); Exposure Java 2013, APCS Edition Chapter 19 Test Page 5 05-20-13

17. What is the action of the method mystery1? a + b a * b (E) a b b a a! (a factorial) public int mystery1(int a, int b) if (b == 1) return a; return a + mystery1(a,b-1); 18. What is the action of the method mystery2? a + b a * b (E) a b b a a! (a factorial) public int mystery2(int a, int b) if (b == 1) return a; return a * mystery2(a,b-1); 19. What is the action of the method mystery3? a + b a * b (E) a b b a a! (a factorial) public int mystery3(int a, int b) if (a == 1) return b; return b * mystery3(a-1,b); 20. What is the action of the method mystery4? a + b a * b (E) a b b a a! (a factorial) public int mystery4(int a, int b) if (a == 0) return 1; return a * mystery4(a-1,b); Exposure Java 2013, APCS Edition Chapter 19 Test Page 6 05-20-13

Objective 3 Manipulating Parameters of Recursive Methods Use the "iterative" liinear1 method and the three "recursive linearx methods below. public static int linear1(int list[], int key) boolean found = false; int k = 0; while (k < list.length &&!found) if (list[k] == key) found = true; k++; if (found) return k; return -1; public static int linear3(int list[], int key, int k) if (k == list.length) return -1; if (list[k] == key) return k; return linear3(list,key,k+1); public static int linear2(int list[], int key) int k = 0; if (k == list.length) return -1; if (list[k] == key) return k; k++; return linear2(list,key); public static int linear4(int list[], int key, int k) if (k == list.length) return -1; if (list[k] == key) return k; k++; return linear2(list,key,k); 21. Which of the "recursive" linearx methods performs the same searching actions as the "iterative" linear1 method? (E) linear2 only linear3 only linear4 only linear2 and linear3 only linear3 and linear4 only Exposure Java 2013, APCS Edition Chapter 19 Test Page 7 05-20-13

22. Method binary1 is a correct, iterative Binary Search method. Which of the recursive method(s) perform(s) the same action as the binary1method? public static int binary1(int list[], int key) int lo = 0; int hi = list.length-1; int mid = 0; boolean found = false; while (lo <= hi &&!found) mid = (lo + hi) /2; if (list[mid] == key) found = true; if (key > list[mid]) lo = mid + 1; hi = mid - 1; if (found) return mid; return -1; public static int binary3(int list[], int key) int lo = 0; int hi = list.length-1; int mid = 0; if (lo > hi) return -1; mid = (lo + hi) /2; if (list[mid] == key) return mid; if (key > list[mid]) lo = mid + 1; return binary3(list,key); hi = mid - 1; return binary3(list,key); public static int binary2(int list[], int key, int lo, int hi) int mid = 0; if (lo > hi) return -1; mid = (lo + hi) /2; if (list[mid] == key) return mid; if (key > list[mid]) return binary2(list,key,mid+1,hi); return binary2(list,key,lo,mid-1); public static int binary4(int list[], int key, int lo, int hi) int mid = 0; if (lo > hi) return -1; mid = (lo + hi) /2; if (list[mid] == key) return mid; if (key > list[mid]) lo = mid + 1; return binary4(list,key,lo,hi); hi = mid - 1; return binary4(list,key,lo,hi); binary2 only binary2 and binary3 only binary3 only binary2 and binary4 only (E) All three binaryx methods work Exposure Java 2013, APCS Edition Chapter 19 Test Page 8 05-20-13

23. Is the parameter list likely to be different between iterative and recursive methods of the same algorithm? No, the parameter list will always be the same. No, in most instances the parameter list will be the same, with some occasional exceptions. Yes, the parameter list will always be different. It depends, many methods will have the same parameter lists. However, anytime that an iterative method initializes a variable in the method body, the parameter list will be different. 24. Recursive solutions of the Linear Search and the Binary Search have shorter code than the iterative solutions. Is this a typical occurrence between recursive and iterative solutions? Yes, it is typical. Most recursive solutions are shorter than iterative solutions. No, it is not typical. Most recursive solutions are inter than iterative solutions. No, it is not typical. Most recursive solutions are the same length as iterative solutions. No, it is not typical. It depends strictly on the algorithm. Objective 4 - The Tower of Hanoi 25. Given that n is the number of disk to be moved in a Tower of Hanoi puzzle. Which one of the formulas below computes the number of moves to solve the puzzle? n 2 n 2 1 2 n 2 n 1 Exposure Java 2013, APCS Edition Chapter 19 Test Page 9 05-20-13

26. Which steps describe the general solution to the Tower of Hanoi puzzle? Move nth disk from source to destination. Move n-1 disks from source to temp. Move n-1 disks from temp to destination. Move n-1 disks from source to temp. Move nth disk from source to destination. Move n-1 disks from temp to destination. Move n-1 disks from source to destination. Move nth disk from source to temp. Move n-1 disks from temp to destination. Move n-1 disks from source to temp. Move n-1 disks from temp to destination. Move nth disk from source to destination. 27. Assume that disks in a Tower of Hanoi puzzle are numbered from top to bottom, starting with 1. Furthermore, assume that Peg-A is the source location, Peg-B is the temporary location and Peg-C is the destination location. What is the solution for a three-disk problem? Move Disk1 from Peg A to Peg B Move Disk2 from Peg A to Peg C Move Disk1 from Peg B to Peg C Move Disk3 from Peg A to Peg B Move Disk1 from Peg C to Peg A Move Disk2 from Peg B to Peg C Move Disk1 from Peg A to Peg C Move Disk1 from Peg A to Peg C Move Disk2 from Peg A to Peg B Move Disk1 from Peg C to Peg B Move Disk3 from Peg A to Peg C Move Disk1 from Peg B to Peg A Move Disk2 from Peg B to Peg C Move Disk1 from Peg A to Peg C Move Disk3 from Peg A to Peg C Move Disk2 from Peg A to Peg B Move Disk1 from Peg A to Peg B Move Disk2 from Peg B to Peg C Move Disk3 from Peg C to Peg B Move Disk2 from Peg B to Peg C Move Disk1 from Peg B to Peg C Move Disk2 from Peg B to Peg C Move Disk1 from Peg A to Peg C Move Disk1 from Peg A to Peg B Move Disk2 from Peg A to Peg C Move Disk1 from Peg B to Peg C Move Disk3 from Peg A to Peg B Move Disk1 from Peg C to Peg A Exposure Java 2013, APCS Edition Chapter 19 Test Page 10 05-20-13

28. Consider the incomplete solvehanoi method below. public static void solvehanoi(char s; char t; char d; int n) if (n!= 0) // code segment Which of the following code segments complete the solvehanoi method correctly? solvehanoi(s, d, t, n); System.out.println("Move Disk " + n-1 + " from Peg " + s + " to Peg " + d); solvehanoi(t, s, d, n); solvehanoi(t, s, d, n-l); System.out.println("Move Disk " + n + " from Peg " + s + " to Peg " + d); solvehanoi(s, d, t, n-1); System.out.println("Move Disk " + n + " from Peg " + s + " to Peg " + d); solvehanoi(s, d, t, n-l); solvehanoi(t, s, d, n-1); solvehanoi(s, d, t, n-l); System.out.println("Move Disk " + n + " from Peg " + s + " to Peg " + d); solvehanoi(t, s, d, n-1); Exposure Java 2013, APCS Edition Chapter 19 Test Page 11 05-20-13

Objective 5 Why Recursion? 29. Does a recursive solution execute faster than an iterative solution? Yes, recursion is always faster than iteration. Most of the time recursion is faster than iteration. No, recursion and iteration execute at the same speed. No, recursion is anywhere from slightly slower to a lot slower than iteration. 30. Does a recursive solution use less memory than an iterative solution? Yes, recursion uses less memory than iteration. Most of the time recursion uses more memory than iteration. No, recursion and iteration use the same amount of memory. No, recursion uses more memory than iteration. 31. Is a recursive solution more readable than an iterative solution for the novice programmer? Yes, recursion is more readable than iteration. Most of the time recursion is more readable than iteration. No, recursion and iteration are equally readable. No, recursion is less readable than iteration. 32. Why do you learn and use recursion? Recursion is faster than iteration. Recursion uses less memory than iteration. Recursion is easier to code than iteration for certain problems like the Tower of Hanoi. Recursion is more readable than iteration. Exposure Java 2013, APCS Edition Chapter 19 Test Page 12 05-20-13

Objective 6 - The Fibonacci Sequence 33. If the first four numbers of the Fibonacci Sequence are: 1 1 2 3 Which of the following is the 13th number in the sequence? 12 89 144 233 34. What is true about comparing the execution efficiency of the iterative and recursive solution of the Fibonacci Sequence for numbers greater than the 20th number? The recursive solution is always faster than the iterative solution. The iterative solution is always faster than the recursive solution. The recursive solution is the same speed as the iterative solution. The iterative solution is initially faster, but the recursive solution is faster for later numbers in the sequence. 35. What does guess(6) return? 1 6 16 32 (E) 64 public int guess(int n) if (n == 1) return 1; return guess(n-1) + guess(n-1); Exposure Java 2013, APCS Edition Chapter 19 Test Page 13 05-20-13

36. Which one of the methods below correctly returns the n th number in the Fibonacci Sequence? NOTE: By correctly we are saying it works. We are not necessarily saying it works efficiently. I. III. II. public static int fibo(int n) int temp1 = 1; int temp2 = 1; int temp3 = 1; int k; for (k=3; k<=n; k++) temp3 = temp1 + temp2; temp1 = temp2; temp2 = temp3; return temp3; public static int fibo(int N) int temp1 = 1; int temp2 = 1; int temp3; int K; for (K=1; K<=N; K++) temp3 = temp1 + temp2; temp1 = temp2; temp2 = temp3; return temp3; IV. V. public static int fibo(int n) if ((N == 1) (N == 2)) return 1; return Fibo(n-1) + Fibo(n-2); pubic static int fibo(int n) if ((N == 1) (N == 2)) return 1; return fibo(n) + Fibo(N-1); public static int fibo(int n) int list[] = new int[n+1]; for (int j=0; j<=n; j++) if (j < 2) list[j] = j; list[j] = list[j-1] + list[j-2]; return list[n]; (E) I only I & II only I & III only II & IV only I, III & V only Exposure Java 2013, APCS Edition Chapter 19 Test Page 14 05-20-13

Objective 7 - Challenging Recursive Method Refer to this method for questions 37-40 public static int mystery(int a, int b) if (a = = b) return a * b; if (a % b == 0) return mystery(b,b); if (a < b) return mystery(b,a); return mystery(1,mystery(a-1,b+1)); 37. What values does mystery(5,5) return? 0 1 5 10 (E) 25 38. What values does mystery(65,13) return? 5 13 65 169 (E) 4175 39. What values does mystery(7,21) return? 3 7 21 49 (E) 441 40. What values does mystery(29,13) return? 1 13 29 169 (E) 196 Exposure Java 2013, APCS Edition Chapter 19 Test Page 15 05-20-13

Exposure Java 2013, APCS Edition Chapter 19 Test Page 16 05-20-13