1. The current method executing is always the method whose activation record is
ID: 3698126 • Letter: 1
Question
1. The current method executing is always the method whose activation record is ________.
Select one: at the bottom of the stack.
second from the top of the stack,
just below the previous method call.
at the top of the stack.
never placed on the stack.
2.
An infinite loop and an infinite recursion
Select one:
both will be caught by the Java Virtual Machine during execution
both continue to repeat indefinitely
both will be caught by the compiler
are different because it is impossible to detect the latter, while it’s quite easy to detect the former
3.
Recursion is often less efficient than iteration because ________.
Select one:
recursive methods take longer to program.
it is not as intuitive.
recursive methods are harder to debug.
it can cause an explosion of method calls
4.
Recursion is sometimes preferable to iteration because ________.
Select one:
it is faster.
it requires less memory.
it models the problem more logically.
All of the above.
5.
It always is possible to replace a recursion by an iteration.
Select one:
True
False
6.
It always is possible to replace an iteration by a recursion.
Select one:
True
False
7.The following method should return true if the int parameter is even and either positive or 0, and false otherwise.
Which set of code should you use to replace … so that the method works appropriately?
public boolean evenPosZero(int x) {
if(x<0) return false;
… // ??? what goes here
}
Select one:
else if (x = = 0) return true;
else return evenPosZero(x – 2);
else if (x = = 0) return false;
else return evenPosZero(x – 2);
else if (x = = 0) return true;
else return evenPosZero(x – 1);
else if (x = = 0) return false;
else return evenPosZero(x – 1);
else return(x = = 0);
8.
What does the following recursive method determine if initially invoked with j=0?
public boolean whoKnows(int[ ]a, int[ ] b, int j) {
if (j = = a.length)
return false;
else if (j = = b.length)
return true;
else return
whoKnows(a, b, j+1);
}
Select one:
Returns true if a and b have no elements
Returns the length of array a + length of array b
Returns true if the size of a is greater than the size of b, false otherwise
Returns true if the size of b is greater than the size of a, false otherwise
Returns true if a and b are equal in size, false otherwise
9.
Why will the following method have infinite recursion?
public int infiniteRecursion(int n) {
if (n > 0)
return infiniteRecursion( n ) + 1;
else
return 0;
}
Select one:
Because there is no base case
Because the base case will never be true
Because the recursive call does not move the parameter closer to the base case
None of the above, there is no infinite recursion in this method
10.
What does the following method compute? Assume the method is called initially with i = 0
public int mysteryMethod(String a, char b, int i) {
if (i = = a.length( ))
return 0;
else if (b = = a.charAt(i))
return mysteryMethod(a, b, i+1) + 1;
else
return mysteryMethod(a, b, i+1);
}
Select one:
Returns 1 if char b appears in String a at least once, and 0 otherwise
The char which appears at location i in String a
The length of String a concatenated with char b
The number of times char b appears in String a
The length of String a
11.
The following method lacks a base case.
public int noBaseCase(int x) {
if (x > 0)
return noBaseCase(x – 1) + 1;
else
return noBaseCase(x – 2) + 2;
}
Select one:
True
False
12.
The following two methods will both compute the same thing when invoked with the same value of x. That is, method1(x) = = method2(x).
public int method1(int x) {
if (x > 0)
return method1(x – 1) + 1;
else
return 0;
}
public int method2(int x) {
if (x > 0)
return 1 + method2(x – 1);
else
return 0;
}
Select one:
True
False
Explanation / Answer
1. The current method executing is always the method whose activation record is ________.
at the top of the stack.
2.
An infinite loop and an infinite recursion
both continue to repeat indefinitely
Recursion is often less efficient than iteration because ________.
Select one:
3. recursive methods take longer to program.
it can cause an explosion of method calls
4. Recursion is sometimes preferable to iteration because ________.
it models the problem more logically.
5. It always is possible to replace a recursion by an iteration.
True
6. It always is possible to replace an iteration by a recursion.
False
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.