Integral of x² dx from 0 to 2 is (x³/3) evaluated from 0 to 2 = (8/3) - 0 = 2.6667 approximately.
Step 3: Check the printed output
The code rounds the result to 2 decimal places: 2.67.
Final Answer:
2.67 -> Option C
Quick Check:
Integral of x² from 0 to 2 = 2.67 [OK]
Hint: Integrate x² from 0 to 2 = 8/3 ≈ 2.67 [OK]
Common Mistakes:
Confusing integral result with function value at 2
Rounding incorrectly
Mixing error estimate with result
4. Identify the error in this code snippet:
from scipy.integrate import quad
result = quad(x**3, 1, 3)
print(result)
medium
A. quad does not return any value
B. The integration limits should be reversed
C. The print statement is missing parentheses
D. The function to integrate must be passed as a callable, not an expression
Solution
Step 1: Check the function argument to quad
The argument x**3 is an expression, not a function. quad expects a callable function like lambda x: x**3.
Step 2: Confirm other parts are correct
Integration limits and print statement are correct syntax. quad returns a tuple.
Final Answer:
The function to integrate must be passed as a callable, not an expression -> Option D
Quick Check:
Function argument must be callable [OK]
Hint: Pass a function or lambda, not a direct expression [OK]
Common Mistakes:
Passing expressions instead of functions
Swapping integration limits unnecessarily
Forgetting quad returns a tuple
5. You want to calculate the integral of sin(x) from 0 to π using scipy.integrate.quad. Which code snippet correctly computes this and prints the integral value rounded to 3 decimals?
hard
A. from scipy.integrate import quad
import math
result, error = quad(math.sin, 0, math.pi)
print(round(result, 3))
B. from scipy.integrate import quad
result, error = quad('sin(x)', 0, 3.14)
print(result)
C. import scipy
result = scipy.integrate.quad(lambda x: sin(x), 0, 3.14)
print(round(result, 3))
D. from scipy.integrate import quad
result = quad(lambda x: math.sin(x), 0, 3.14)
print(round(result, 3))
Solution
Step 1: Check function import and usage
from scipy.integrate import quad
import math
result, error = quad(math.sin, 0, math.pi)
print(round(result, 3)) imports math and passes math.sin as a callable, which is correct.
Step 2: Verify integration limits and print
Limits 0 to π are correct using math.pi. Result is rounded to 3 decimals before printing.
Step 3: Identify errors in other options
from scipy.integrate import quad
result, error = quad('sin(x)', 0, 3.14)
print(result) passes a string instead of a function. import scipy
result = scipy.integrate.quad(lambda x: sin(x), 0, 3.14)
print(round(result, 3)) misses math.sin import and uses undefined sin. from scipy.integrate import quad
result = quad(lambda x: math.sin(x), 0, 3.14)
print(round(result, 3)) does not unpack the tuple returned by quad.
Final Answer:
from scipy.integrate import quad
import math
result, error = quad(math.sin, 0, math.pi)
print(round(result, 3)) -> Option A
Quick Check:
Use callable math.sin and unpack quad result [OK]
Hint: Import math and pass math.sin as function, unpack result [OK]
Common Mistakes:
Passing string instead of function
Not unpacking quad's tuple output
Using undefined sin without import
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.