Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
If you’re a scientist or an engineer interested in learning scientific computing, this is the place to start.
In this course, you’ll learn to write your own useful code to perform impactful scientific computations. Along the way, your understanding will be tested with periodic quizzes and exercises.
Topics covered in this course include arrays, plotting, linear equations, symbolic computation, scientific algorithms, and random variables. You’ll also be exposed to popular Python packages like NumPy, Matplotlib, SciPy, and others. In the last part of the course, the application section will test your ability to recall and apply the tools you have studied into newly learned scientific concepts.
At the end of this course, you’ll be equipped with the tools necessary for everyday scientific computation.
Q1. A comparison operation always returns a value of the ______ data type.
Q2. String indices can be floats.
Q3. The conditional statement always returns a ______.
Q4. What is the least number of parameters a function can have?
Q5. Which of the following keyword is used to create a lambda?
def
lamb
lambda
lbd
Q6. A while
loop runs as long as its condition holds True
.
Q7. In a dictionary, key-value pairs are indexed by _____.
Q8. For a given list tempList
, what is the correct way of calculating its length?
tempList.length()
len(tempList)
tempList.len()
Answer:
def check_sum(num_list, num):
for first_num in range(len(num_list)):
for second_num in range(first_num + 1, len(num_list)):
if num_list[first_num] + num_list[second_num] == num:
return True
return False
Q1. What is the output of the the code below:
print(np.arange(1, 50, 6))
Q2. What is the output of the following code:
np.linspace(1, 20, 5)
Q3. What is the output of the following code:
x = np.array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]])
print(np.reshape(x, (2, 6)))
Q4. What are the shapes of X
and Y
in the following code:
x = np.arange(0, 3)
y = np.arange(4, 8)
[X, Y] = np.meshgrid(x, y)
Q1. What is the output of the code?
arr = np.array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
print(arr[1][0][1])
Q2. Given an array arr
:
arr = np.arange(1, 100)
Which of the following commands will print multiples of 3?
Note: There can be multiple answers to this question.
print(arr[3: 100: 3])
print(arr[2: 100: 3])
print(arr[1: 100: 3])
print(arr[2: 99: 3])
Q3. What is the output of the code?
M = np.array([[ 0, 1, 2], [ 5, 6, 7], [10, 11, 12]])
N = np.array([[ 0, 5, 10],[ 0, 5, 10], [ 0, 5, 10]])
print(N - M * 2)
Q4. What condition must be satisfied for an element-wise multiplication of a 1-D array and a 2-D array?
Q5. What is the output of the code below:
arr = np.array([5, 10, 12, 4, 3, 2, 1, 6, 12, 9, 8])
print(np.max(arr))
Q6. What is the return type of any()
and all()
method?
Q7. What is the output of the code below?
x = np.arange(10)
y = x
y[4] = 24
x[5] = 25
print(x)
Q1. Will the following code compile?
x = np.arange(0, 10)
y = np.linspace(0, 10, 8)
plt.plot(x, y)
Q2. What is the correct code to add legends to a plot?
plot(x1, y1, label = "curve 1")
plot(x2, y2, label = "curve 2")
legend()
legend()
plot(x1, y1, label = "curve 1")
plot(x2, y2, label = "curve 2")
plot(x1, y1, legend = "curve 1")
plot(x2, y2, legend = "curve 2")
plot(x1, y1, legend = "curve 1")
plot(x2, y2, legend = "curve 2")
show_legend()
Q3. The following command will split the figure into a grid with ____ rows and ____ columns.
plt.subplots(2, 4)
Q4. What does the tight_layout()
command do?
Q5. Which of the following command can be used to to create a scatter plot?
Note: This question can have multiple answers.
plot(x, y, 'scatter')
plot(x, y, 's')
scatter(x, y)
scatter(x, y, 'plot')
Q1. Which of the codes solves the following set of equation?
x+y-3z=-6x+y−3z=−6
2x+3y+z=102x+3y+z=10
x+4y-4z=-6x+4y−4z=−6
A = np.array([[2, 3, 1], [1, 1, -3], [1, 4, -4]])
b = np.array([-6, 10, -6])
print(np.linalg.solve(A, b))
A = np.array([[1, 1, -3], [2, 3, 1], [1, 4, -4]])
b = np.array([10, -6, -6])
print(np.linalg.solve(A, b))
A = np.array([[1, 1, -3], [2, 3, 1], [1, 4, -4]])
b = np.array([-6, 10, -6])
print(np.linalg.solve(A, b))
A = np.array([[1, 1, -3], [1, 4, -4], [2, 3, 1]])
b = np.array([-6, 10, -6])
print(np.linalg.solve(A, b))
Q2. To compute both, eigenvectors and eigenvalues, we use the eigvals
methods.
Q3. Suppose there is a 3×3 matrix, M
. Which of the following can not be the eigenvectors returned from eig(M)
?
Q4. What is the output of the code below?
A = np.array([[2, 7, 3], [7, 9, 4], [3, 4, 7]])
print(np.transpose(A))
Q5. In a sparse matrix, only the value and location of non-zero values in a matrix are stored.
Q6. What is the output of the following code?
import numpy as np
import scipy.sparse as sp
M = np.array([[0, 1, 1, 1], [1, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1]])
print(sp.csc_matrix(M))
Q1. What is the correct code to assign mutiple symbols to variable?
Note: This question can have multiple answers.
x, y, z = symbols('x y z')
x, y, z = Symbols('x y z')
x = Symbol('x')
y = Symbol('y')
z = Symbol('z')
x = symbols('x')
y = symbols('y')
z = symbols('z')
Q2. The imaginary unit is denoted by I
in Sympy.
Q3. What is the correct way to represent \frac{2}{5}52 in SymPy?
Rational(2, 5)
Rational(5, 2)
Rational(2/5)
2/5
Q4. What is the output of the following code?
x = Symbol('x')
a = 1/x + 1/x**2
print(together(a))
Q1. Which of the following will compute second derivative of the function f
with respect to x
?
Note: This question can have mutiple answers.
diff(f(x), x, x)
diff(f(x), xx)
diff(f(x), x, 2)
diff(f(x), x*2)
Q2. The function f(x, y)f(x,y) is given by:
f(x, y) =y^3x^3 + x^4y^2f(x,y)=y3x3+x4y2
\frac{\partial^3 f}{\partial x^2 \partial y}∂x2∂y∂3f can be computed using:
Note: This question can have mutiple answers.
diff(f(x), x, 2, y, 1)
diff(f(x), x, x, y)
diff(f(x), x, y, x)
diff(f(x), y, x, x)
Q3. For improper integrals in SymPy, we use the oo
symbol for infinity.
Q4. For definite integrals, we use ____ or _____ for providing limits of integration.
Q5. The direction of limit can be specified using which of the optional argument?
direction
dir
Q1. For taylor expansion of a function f(x)
, which of the following commands are used?
Note: This question can have mutiple answers
series(f(x), x)
expand(f(x), x)
f(x).series(x)
f(x).expand(x)
Q2. By default, the series()
function expands the expression around 0.
Q3.
x = Symbol('x')
fps_exp = fps(sin(x), x, 0)
fps_exp[4]
What does fps_exp[5]
return?
Q4. Which of the following could be the output type of solve()
?
Note: This question can have mutiple answers.
Q5. solve()
can only solve single variable equations equations.
Q1. quad()
returns a tuple with the first value being the numerical result and the second is the estimation of the numerical error in the result.
Q2. In SciPy namespace, we use oo
to refer to inifinity.
Q3. Which of the following is the correct SciPy implementation of the integral given below?
\int_0^3\int_0^2\int_0^1x y^2 z^3\space dxdydz∫03∫02∫01xy2z3 dxdydz
def f(x, y, z):
return(x * y**2 * z**3 )
tplquad(f, 0, 1, lambda y : 0, lambda y : 2, lambda y, z : 0,
lambda y, z : 3)
def f(z, x, y):
return(x * y**2 * z**3 )
x = tplquad(f, 0, 1, lambda y : 0, lambda y : 2, lambda z : 0,
lambda z : 3)
def f(z, y, x):
return(x * y**2 * z**3 )
tplquad(f, 0, 1, lambda y : 0, lambda y : 2, lambda y, z : 0,
lambda y, z : 3)
def f(x, y, z):
return(x * y**2 * z**3 )
x = tplquad(f, (x, 0, 1),(y, 0, 2), (z, 0, 3))
Q4. The third argument of the interp1d()
function determines the type of interpolation.
Q1. Which of the following can be the value of z
if z
is given by:
z = np.polyfit(x, y, 3)
Note: This question can have more than one answer.
Q2. What does poly1d
do?
Q3. The curve_fit
function needs to be called with two neccessary arguments.
Q4. There are two minimum input arguments for fmin
.
Q5. What does fminbound
do?
Q6. Fourier transform computes the peaks only in the postive frequency domain
Q1. Which of the following could be the output of the code below:
print(rnd.randint(0, 1, 10))
Note: This question can have more than one answers
Q2. The code below
rnd.seed(10)
print(rnd.randint(0, 2, 10))
produces
[1 1 0 1 0 1 1 0 1 1]
Does this output change when we run the code again?
Q3. Which of the following can not be the output of the code below:
arr = np.arange(1, 11)
pick = rnd.choice(arr, 3, replace=False)
Note: This question can have more than one asnwers
Q4. comb()
is part of which package?
Q5. Which of the following command produces an array of size = 100, mean = 3, and standard deviation = 2.4?
rnd.normal(loc = 2.4, scale = 3, size = 100)
rnd.normal(loc = 3, scale = 2.4, size = 100)
rnd.normal(mean = 3, sd = 2.4, size = 100)
Q6. The hist()
function creates a histogram graph and returns a tuple of three items
Q1. What will be the output of the following code:
arr = (0, 1, 2, 3, 4)
for i in range(0, len(arr)):
arr[i] = arr[i] ** 2
print(arr)
Q2. What will be the output of the following code:
num = 12
while (num < 13):
if (num == 12):
num = 10
if (num == 10):
num = 8
if (num == 8)
num = 6
else:
num = 14
print(num)
Q3. What will be the output of the following code?
name = "Python"
def func(n):
n = "Educative"
print(n)
func(name)
print(name)
Q4. See the code below:
import numpy as np
a = np.arange(10)
Which one of the commands will print the output below?
[6 7 8]
Q5. What will be the output of the following code?
import numpy as np
a = np.arange(5)
b = a
b[3] = 55
print(a)
Q6. What will be the output of the following code:
import numpy as np
x = np.array([1, 3, 5, 7, 9])
y = np.ones(5)
z = np.zeros(5) + 2
a = x + y * z
print(a)
Q7. For the add_axes(x, y, w, h)
command, what are the correct value ranges for x
, y
, w
, and h
?
Q8. For which of the commands below, the following code will color the area between y=3x^3y=3x3 and the y=x^3y=x3 curves?
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
fig, axes = plt.subplots(figsize=(12, 8))
Q9. How many curves will the following code produce?
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi , 100)
y1 = np.sin(x)
y2 = np.cos(x + np.pi/2)
plt.plot(x, y1)
plt.plot(x, y2)
Q10. What is the output of the following code?
import numpy as np
A = np.array([[10, 0, 0], [0, 20, 0], [0, 0, 30]])
print(np.linalg.eigvals(A))
Q11. What is the output of the following code?
import numpy as np
A = np.array([[1, 0, 3], [2, 2, 0], [0, 2, 2]])
B = np.transpose(A)
C = A + B
print(np.trace(C))
Q12. Which of the following Python commands will solve the below systems of linear equations?
\begin{bmatrix} 1 & 3& 1\\ 4 & 4 & 2\\ 2 & 1 & -1 \end{bmatrix} \begin{bmatrix} x\\ y\\ z \end{bmatrix} =\begin{bmatrix} 17\\ 25\\ 6 \end{bmatrix}⎣⎡14234112−1⎦⎤⎣⎡xyz⎦⎤=⎣⎡17256⎦⎤
Here is a part of the code:
import numpy as np
A = np.array([[1, 3, 1], [4, 4, 2], [2, 1, -1]])
b = np.array([17, 25, 6])
Q13. What is the output of the following code?
from sympy import *
def f(x):
return (x**2 + 2*x + 4)
def g(x):
return (sin(x) + cos(x))
x = Symbol('x')
print(diff(f(x), x) + diff(g(x), x))
Q14. Taylor series of sin(x)sin(x) around 00 is given below:
x-\frac{1}{3!}x^3+\frac{1}{5!}x^5-\frac{1}{7!}x^7+\frac{1}{9!}x^9+\ldots \:x−3!1x3+5!1x5−7!1x7+9!1x9+…
What is the output of the following code?
from sympy import *
x = Symbol('x')
fps_sin = fps(sin(x), x, 0).truncate(3).removeO()
print(fps_sin)
Q15. What is the output of the following code?
from sympy import *
def f(x):
return sin(x) / x
x = Symbol('x')
print(limit(f(x), x, 0))
Q16. What is the output of the following code?
from sympy import *
a, b, c = symbols('x y z')
print(a + b + c)
Q17. Which command will yield the correct result for the following integral?
\int _0 ^3 \int _0 ^2 x^2y^3 \space dxdy∫03∫02x2y3 dxdy
Here is the function defined in the code:
def f(y, x):
return x**2 * y**3
Q18. What is the output of the following code?
import numpy as np
x = np.linspace(-10, 11, 20)
y = (0.2 * x**2 + x + 2)
z = np.polyfit(x, y, 3)
print(len(z))
Q19. See a graph for the data collected for an experiment:
To interpolate using the interp1d
function, what is the best possible value for kind
for the data in the graph above?
y0 = interp1d(x, y, kind='')
Q20. What is the output of the following code?
import scipy as sc
x = sc.arange(0, 25, 3)
print(x)
Q21. What could not be a possible output of the following code?
import numpy.random as rnd
print(rnd.randint(0, 4 + 1, 10))
Q22. What are the possible values of pick
in the following code?
import numpy as np
import numpy.random as rnd
arr = np.arange(1, 5)
pick = rnd.choice(arr, 4, replace = True)
Q23. Which of the following is least likely to be a possible outcome of the code below:
import numpy as np
import numpy.random as rnd
data = rnd.normal(loc=6, scale=2, size=1000) # Array with 100 values
print(np.mean(data))
print(np.std(data))
Q24. Running the code below produces the following output the first time:
import numpy.random as rnd
rnd.seed(40)
flips = rnd.randint(0, 1+1, 10)
print(flips)
[0 1 1 1 0 0 0 1 0 1]
What will be the output when the code is run for the third time?
Q25. In Python, an arithmetic expression containing different operators will be computed on the basis of operator precedence.
Q26. If there is an operation between two matrices, they should have exactly the same shape.
Q27. To plot a three-dimensional curve, set the keyword projection to ‘3d’ when creating the axis.
Q28. The SymPy polyfit() function returns a dictionary containing the coefficients of the polynomial.
Q29. SymPy solve() is just limited to solving a single variable equation, it cannot also solve a system of multivariable equations.
Q30. Match the blanks with the correct data type.
Q31. Implement a function has_duplicates(arr)
which takes a list of integers and returns True
if the list has duplicates and returns False
if the list has no duplicates.
Below are the sample inputs and outputs forhas_duplicates(arr)
:
has_duplicates([1, 2, 3, 1])
has_duplicates([1, 2, 3, 4])
has_duplicates([1, 1, 1, 1])
True
False
True
Answer:
Q32. Write a function maximum
which takes in the complex NumPy array and returns a tuple containing the maximum value
arr = numpy.array([2+5j, 3+4j, 8-2j], dtype="complex")
a = maximum(arr)
print(a)
(8.0, -3.0)
Answer:
Q33. Solve the following system of linear equations:
3a +2b-c+d+e=163a+2b−c+d+e=16
a+ b + c+ 2d-e=9a+b+c+2d−e=9
a+2b+2c+2d-4e=1a+2b+2c+2d−4e=1
3a+2b-2c+4d+e=263a+2b−2c+4d+e=26
a+b+c+d-e=5a+b+c+d−e=5
Store the exact values of the unknowns in their corresponding variables.
For example, the exact value of aa should be stored in
a
, the exact value of bb should be stored in the variableb
and so on.
Answer:
Q34. Integrating complex single fractions can be a hassle. To accommodate for this, we convert these fractions into a sum of partial fractions and then integrate each partial fraction.
Convert the following function to partial fractions, store it in the variable partial_fraction
and then integrate it indefinitely:
\int_2^4 \frac{4x^3+10x+4}{x(2x+1)} \space dx∫24x(2x+1)4x3+10x+4 dx
Store the indefinite integral in the variable indefinite_integral
and final answer of the integration in the variable result
. The final result, result
, should be a float of 44 significant figures.
Answer:
Q35. Write a Python function throw
which takes in the two input arguments: seed
and the number of throws. The function returns a dictionary containing probabilities of each number on the dice. The 0
index corresponds to the probability of 11 on the dice
print(throw(30, 50))
{'1': 0.12, '2': 0.22, '3': 0.1, '4': 0.16, '5': 0.18, '6': 0.22}
Answer:
Q36. Using the SymPy quad
, write a function func_area(n)
to compute the area under the following function from x=0x=0 to x=nx=n:
f(x)=x^2f(x)=x2
where n \geq 0n≥0.
Below are the sample inputs and sample outputs for func_are(n)
.
func_n(0)
func_n(3)
func_n(6)
0.0
9.000000000000002
72.00000000000001
Answer:
I hope this Python for Scientists and Engineers Educative Quiz Answers would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>