Memory Management and Recursion
If you Google "recursion", it will reply "Did you mean: recursion", and the word "recursion" is a clickable hyperlink that leads back to the current page (try it). This is a splendid example of which of the following?
A programmer's sorry attempt at humor. and Recursion
In the context of recursion, "short-circuiting" means to:
Check for the base case before making a recursive call.
The fact that bad programs and/or bad data will result in bad results can be summarized as:
GIGO
A "stack overflow" will occur when:
Recursion is excessively deep.
When a method calls itself to solve a problem, the algorithm is referred to as?
Recursive
Depending on the programming language, what java calls a "method" might be referred to as a function.
True
Problems that feature multi-way branching, where the method would recursively call itself in each branch are viable candidates for recursion.
True
Some programmers have more trouble writing, understanding and debugging recursive algorithms than do others
True
Which of the graphs depicts "quasi-linear" time complexity?
this but starting from 0 and curving up
Which of the following is true regarding n! and nn, if n>1?
n^n is larger than n!
If a program recursively calculates "n!", what would its time complexity be?
quasi-linear
The area of memory where information about the active methods of a computer program are stored is referred to as the?
Call stack
What will be the output from the following program? public class Quiz1 { public static void main(String[ ] args) { method1(10); } static void method1(int x) { if (x >= 1) { System.out.println(x); method2(x / 2); } } static void method2(int x) { if (x >= 1) { System.out.println(x); method1(x - 1); } } }
10 5 4 2 1
What will be the output of the following Java program? public class Test1 { public static void main(String[] args) { System.out.println("The answer is: " + method1(10)); } static int method1(int x) { if (x == 1) return 1; else return x + method1(x / 2); } }
18
A recursive algorithm is causing a stack overflow. Which of the following might solve this problem?
Add more RAM to the system. Split the input data set into subsets, run the program on each subset, and then combine the results. Rewrite the program to use an iterative algorithm.
A recursive algorithm without a base case is most like:
An infinite loop in an iterative program.
What is the value of 6!
Correct answer: 720
Which of the following will result in "infinite recursion"?
Failure to specify or satisfy the base case.
Because recursive algorithms do not contain loops, they don't have characteristic time complexities like iterative programs do.
False
From a CPU and RAM perspective, using a recursive algorithm is always the most efficient way to solve a problem.
False
Only extra-super-special-secret programming languages support recursion, most programming languages do not.
False
Short-circuiting is used with recursion in order to :
Increase efficiency.
For which of the following conditions will using a recursive algorithm result in a call stack overflow?
Input data set too large. Base case never satisfied. Base case not specified.
Which of the following are true regarding recursive tail-call optimization?
It could reduce the algorithm's call stack memory requirements from O(n) to O(1). and It reduces the number of CPU cycles needed to execute the algorithm.
Which of the following programming techniques are supported by Java?
Iteration. Recursion incorporating base case short-circuiting. Recursion without optimizations. In-line coding.
Using a loop to solve a problem non-recursively is referred to as what type of solution?
Iterative
One purpose of the call stack is to:
Keep track of the point to which each method instance should return control when it finishes executing.
If a program calculates "n!" using iteration, what would its time complexity be?
Linear
Which of the following is an example of simple recursion?
Method "test2" calls method "test2".
If a particular recursive algorithm requires one recursive method call for each item in the input set, it has a time complexity of?
O(n)
f a particular recursive algorithm requires one recursive method call for each item in the input set, it has a time complexity of?
O(n)
When a method is called
Program control is handed to that method
The technique of checking for the base case before making a recursive call is referred to as:
Short-circuiting
Based upon what you know about recursion, evaluate the following statement as either true or false: "When measuring the students' knowledge of recursion, it might be far too easy to make the quiz far too difficult."
True
It's called "recursion" because:
The method "recurs".
What will be the output of the following program? public class Test1 { public static void main(String[] args) { System.out.println("The answer is: " + method1(5)); } static int method1(int x) { if (x == 1) return 1; else return x + method1(x * 2); } }
The program will crash on a stack overflow.
A triangular number counts objects arranged in an equilateral triangle. The nth triangular number is the number of dots in the triangular arrangement with n dots on a side, and is equal to the sum of the n natural numbers from 1 to n. The sequence of triangular numbers starts {0, 1, 3, 6, 10, 15 ...}. The following program is intended to generate the twentieth triangular number (for which n = 19), but it crashes and displays a "StackOverflowError" message. Diagnose the problem. public class Triangular { public static void main(String[] args) { System.out.println("The answer is: " + method1(19)); } static int method1(int x) { if (x == 20) return 0; else return x + method1(x-1); } }
The specified base case is incorrect.
Any program that can be written recursively can also be written iteratively, and vice-versa.
True
Recursion is:
Used in some of the most efficient sorting algorithms.
A method that supports and calls another method is referred to as what type of method?
Wrapper