Numerical Prev Quiz and Answers
Numerical Prev Quiz and Answers
B = np.random.rand(3, 6)
c = B.T
print(c)
1
4. (2 point) How can you calculate the mean of a numpy array x using numpy
and scipy?
x = np.array([1, 2, 3, 4, 5])
Answer :
np.mean(x)
import numpy as np
A = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])
x = np.linalg.solve(A, b)
x = x + y
y = x - y
x = x - y
OR :
x,y = y,x
2
8. (1 point) If the arithmetic mean and the geometric mean of two numbers
are 9 and 4, respectively, then these numbers are the roots of the quadratic
equation:
a) x2 + 18x + 16 = 0
b) x2 + 18x − 16 = 0
c) x2 − 16x + 18 = 0
d) x2 − 18x − 16 = 0
e) x2 − 18x + 16 = 0
a+b
AM = =9
2
This implies:
a + b = 18 (1)
- The geometric mean (GM) of the same two numbers is given by:
√
GM = ab = 4
This implies:
ab = 16 (2)
x2 − (a + b)x + ab = 0
Using equations (1) and (2): - The sum of the roots a + b = 18 - The
product of the roots ab = 16
Thus, we can substitute these values into the quadratic equation:
x2 − 18x + 16 = 0
3
9. (1 point) What is the output of the code np.linspace(1, 4, 4)?
a) array([1., 2., 3., 4.])
b) array([1., 2.33333, 3.66667, 5.])
c) None of the others
d) array([1., 1.6, 2.6, 3.4, 4.])
e) array([1., 4., 8., 12.])
Answer : The correct answer is a) array([1., 2., 3., 4.]).
Explanation :
The dot (.) in 1., 2., 3., and 4. indicates that these numbers are represented
as floating-point numbers (i.e., float64 in NumPy).
In Python, adding a dot after an integer makes it a float:
• 1 is an integer.
• 1. is a float.
import numpy as np
result = np.linspace(1, 4, 4).astype(int)
array([1, 2, 3, 4])
4
10. (1 point) If β1 , β2 and β3 are the roots of the equation x3 −x2 −8x+12 = 0,
then β1 β2 β3 =?
a) -8
b) 12
c) -12
d) 8
e) None of the others
Answer : The correct answer is b) 12.
Explanation :
To find the product of the roots (β1 β2 β3 ) of the polynomial equation
x3 − x2 − 8x + 12 = 0, we can use Vieta’s formulas.
For a cubic equation of the form ax3 + bx2 + cx + d = 0, the product of
the roots (β1 β2 β3 ) is given by:
d
β1 β2 β3 = −
a
In your equation, we have:
• a=1
• b = −1
• c = −8
• d = 12
Substituting these values into the formula:
12
β1 β2 β3 = − = −12
1
So, the product of the roots β1 β2 β3 is −12.
More formula with cubic equation : For a cubic equation of the form
ax3 + bx2 + cx + d = 0,
the roots β1 , β2 , and β3 can be analyzed using Vieta’s formulas and other
relationships. Here are some key formulas:
Vieta’s Formulas
1. Sum of the roots:
b
β1 + β2 + β3 = −
a
2. Sum of the products of the roots taken two at a time:
c
β1 β 2 + β 2 β3 + β3 β1 =
a
5
3. Product of the roots:
d
β1 β2 β3 = −
a
11. (2 points) Find the output of the below code:
12. (1 points) Which of the following calculates the standard deviation of the
numpy array x?
a) None of the others
b) numpy.std.dev(x)
c) numpy.median(x)
d) numpy.var(x)
e) numpy.sdeviation(x)
Answer : The correct answer is a) None of the others, the correct
function is numpy.std(x).
13. (1 points) Equipped with (xb , f (xb )) values of an unknown funtion f (xk ), k =
0, 1, 2, ...., n the process of determining valuates f (xj ) inside the interval
(x0 , xn ), where ∀kxk ̸= xf i is
a) root finding
b) approximation
c) regression
d) interpolation
e) extrapolation
Answer : The correct answer is d) interpolation.
6
14. (1 points) A root of the equation x3 = 100? lies between
a) (−1, −2)
b) (0, 1)
c) (3, 4)
d) (−2, 3)
e) (−2, 1)
Answer : The question was not clear if it was x3 or something else. (Hint
: Use Bisection method)
15. (1 point) How can you solve a system of linear equations Ax = b? Assume
that a and b are numpy arrays of right dimensions.
a) scipy.linalg.linsolve(A, b)
b) None of the above
c) numpy.linalg.solve(A,b)
d) scipy.solve(A, b)
e) numpy.solve(A, b)
Answer : The correct answer is c) numpy.linalg.solve(A, b).
16. (2 point) Write a numpy based Python statement to calculate dot product
xy of two numpy arrays x and y. Assume x is a row vector while y is a
column vector.
Answer :
np.dot(x, y)
17. (3 points) Write minimal Python codes that solve the following system of
linear equations:
8x + 3y − 2z = 9
−4x + 7y + 5z = 15
3x + 4y − 12z = 35
import numpy as np
A = np.array([[8, 3, -2], [-4, 7, 5], [3, 4, -12]])
b = np.array([9, 15, 35])
x = np.linalg.solve(A, b)
print(x)
7
18. (1 point) How can you calculate the inverse of a matrix A?
a) scipy.linalg.inv(A)
b) scipy.inv(A)
c) None of the others
d) numpy.linalg.inverse(A)
e) numpy.inv(A)
Answer : The correct answer is a) scipy.linalg.inv(A).
19. (1 point) Which of the following is not a valid scipy function used to find
the roots of a given equation/function?
a) scipy.optimize.bisect()
b) scipy.optimize.newton()
c) scipy.optimize.fsolve()
d) scipy.optimize.find.roots()
Answer : The correct answer is d) scipy.optimize.find.roots(), which
does not exist. The correct function names in the scipy.optimize module
for finding roots are:
• scipy.optimize.bisect()
• scipy.optimize.newton()
• scipy.optimize.fsolve()
These functions are used to find the roots of equations.