Question 45 Consider the following C function.
Palo Alto Networks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Consider the following C function.
float f(float x, int y)
{
float p, s; int i;
for (s=1, p=1, i=1; i < y; i ++)
{
p*= x/i;
s+=p;
}
return s;
}
For large values of y, the return value of the function f best approximates
Select any one of the following options
Show answer & explanation
Answer: B. B) e^x
The function computes s = 1 + x + x^2/2! + x^3/3! + ... + x^(y-1)/(y-1)!, which is the Maclaurin/Taylor series expansion of e^x.
Step-by-step Derivation:
Step 1: Initialize s = 1, p = 1.
Step 2: For i = 1 to y-1, p *= x/i => p = x^i / i!.
Step 3: s accumulates each term: s = 1 + sum_{i=1}^{y-1} x^i / i!.
Step 4: As y -> infinity, this series converges to e^x.