Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote