0% found this document useful (0 votes)
23 views8 pages

Numerical Prev Quiz and Answers

Uploaded by

Asif Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

Numerical Prev Quiz and Answers

Uploaded by

Asif Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. (1 point) Which is true with the following code fragment?

B = np.random.rand(3, 6)
c = B.T
print(c)

a) B has random dimensions


b) C has 3 columns
c) C.dot(B) results in a real number
d) The above code would produce error
e) B has random dimension

Answer : The correct answer is b) C has 3 columns. because B.T would


transpose B from 3 rows and 6 columns to 6 rows and 3 columns.

2. (1 point) What does the function numpy.linalg.solve(A, b) do?


a) Calculates the determinant of matrix A
b) Finds the eigenvalues of matrix A
c) Calculates the dot product of matrix A and vector b
d) None of the others
Answer : The correct answer is d) None of the others, because it solves
the linear system Ax = b.
3. (1 point) Which is not true about the following code snippet?

x = np.linspace(0, 10, 100)


y = np.sin(x)

a) x is an array of evenly spaced values between 0 and 10


b) x has sin value of a number
c) y is an array of evenly spaced values between 0 and 10
d) y has a hundred elements in it
e) None of the others

Answer : The correct answer is b) x has sin value of a number, as x


is an array of evenly spaced values.

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)

5. (1 point) What is the output of the code: np.arange(5)?


Answer : The function creates a NumPy array containing a range of inte-
gers starting from 0 up to (but not including) the specified value, which
is 5 in this case. The output is [0, 1, 2, 3, 4].
6. (1 point) What does the following code snippet do?

import numpy as np
A = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])
x = np.linalg.solve(A, b)

a) Performs element-wise multiplication of matrix A and vector b


b) Calculates the determinant of matrix A
c) Solves the linear equation Ax = b
d) None of the others
e) Finds the eigenvalues of matrix A

Answer : The correct answer is c) Solves the linear equation Ax = b.


7. (4 points) Produce a (tiny) algorithm to swap/exchange the values of two
variables (of the same type) without using a third variable. Please write
only essential few lines 0f C/Python code.
Here is the Python code:

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

Answer : The correct answer is e) x2 − 18x + 16 = 0.


Explanation :
- The arithmetic mean (AM) of two numbers a and b is given by:

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)

Forming the Quadratic Equation : A quadratic equation whose roots are


a and b can be expressed as:

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.

Using floats is important in many numerical computations, especially


when dealing with operations that require precision, such as division or
in cases where the output needs to be in a specific format. In this case,
np.linspace returns floating-point values to ensure that the spacing be-
tween the values is correctly represented, even if they happen to be whole
numbers in this specific example.
If we want to convert the array to integers, we can use .astype(int):

import numpy as np
result = np.linspace(1, 4, 4).astype(int)

This would give us:

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:

a = np.array([[1, 7, 3],[4, 5, 6]])


a[2,1]

Answer : The output is an error, because there is no row with index 2.


The array a has a shape of (2,3), meaning it has 2 rows and 3 columns.

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

Answer : Here is the Python code:

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.

You might also like