Question
The following is a recursive program. Assume that it only takes the positive integers as the input: int compute( int x, int n) { if(n==
The following is a recursive program. Assume that it only takes the positive integers as the input:
int compute(int x, int n) {
if(n==0) return 1;
else
if(n is even) return (compute(x, n/2) * compute(x, n/2));
else return (x * compute(x, n-1);
}
Can you tell what the above program really does?
- It always returns 1.
- It returns the value x.
- It returns the value xn.
- It returns the value x2.
Which one of the following statements gives us a correct inductive definition of a palindrome?
- A palindrome is a string that reads the same left to right as right to left.
- A palindrome is a string whose left half is a mirror image of its right half.
- A single character is a palindrome and if a string s is a palindrome, then xsx is also a palindrome.
- Both single character and an empty string are a palindrome. If a string s is a palindrome, then xsx is also a palindrome.
Which of the following pseudo code returns the length of a given list?
a) length(L) {
if(L has a single number x) return x;
else return (head(L) + length(tail(L)));
}
b) length(L) {
if(L has a single number x) return 1;
else return (1 + length(tail(L)));
}
c) length(L) {
if(L is an empty list) return 0;
else return (length(tail(L)) + 1);
}
d) length(L) {
if(L is an empty list) return 0;
else return (head(L) + length(tail(L)));
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started