Which algorithm does *NOT* correctly implement the power function? Assume that t
ID: 3779127 • Letter: W
Question
Which algorithm does *NOT* correctly implement the power function? Assume that the exponent is always zero or greater. Select all that apply.
A) public static int pow(int base, int exp) {
if(exp == 0)
return 1;
return base * pow(base, exp - 1);
}
B) public static int pow(int base, int exp) {
int result = 1;
while(exp != 0) {
result *= base;
exp--;
}
return result;
}
C) None of the other options.
D) public static int pow(int base, int exp) {
while(exp != 0)
return base * pow(base, exp - 1);
}
E) public static int pow(int base, int exp) {
if(exp == 1)
return base;
if(exp == 0)
return 1;
return base * pow(base, exp - 1);
}
Choose all answers that are correct. There are multiple correct answers.
Explanation / Answer
The answer is D. When exp=0, the while loop is not executed, so nothing is returned. The expected return value is 1.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.