Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Quiz 1 coursera python

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

What will be the output of the following code?

import re

string = 'bat, lat, mat, bet, let, met, bit, lit, mit, bot, lot, mot'

result = re.findall('b[ao]t', string)

print(result)

a)

'bat, bot'

b)

['bat', 'bot']

c)

'bat, bet, bit, bot'

d)

['bat', 'bet', 'bit', 'bot']

2.

Assume a and b are two (20, 20) numpy arrays. The L2-distance (defined above) between two equal dimension arrays can be calculated in python as follows:

def l2_dist(a, b):

    result = ((a - b) * (a - b)).sum()

    result = result ** 0.5

    return result 

a)

l2_dist(a.T, b.T)

b)

l2_dist(np.reshape(a, (20 20)), np.reshape(b, (20 20, 1)))

c)

l2_dist(np.reshape(a, (20 20)), np.reshape(b, (20 20)))

d)

l2_dist(a, b)

3.

Consider the following variables in Python:

a1 = np.random.rand(4)

a2 = np.random.rand(4, 1)

a3 = np.array([[1, 2, 3, 4]])

a4 = np.arange(1, 4, 1)

a5 = np.linspace(1 ,4, 4)

Which of the following statements regarding these variables is correct?

a)

a5. shape == a1. shape

b)

a4. ndim() == 1

c)

a3. shape == a4. shape

d)

al. shape == a2. shape

4.

Which of the following is the correct output for the code given below?

import numpy as np

old = np. array([[1, 1, 1], [1, 1, 1]])

new = old

new[0, : 2] = 0

print (old)

a)

[[1 1 1] [1 1 1]]

b)

[[0 0 1] [1 1 1]]

c)

[[1 1 0][1 1 0]]

d)

[[0 1 1][0 1 1]]

5.

Given the 6x6 NumPy array r shown below, which of the following options would slice the shaded elements?

a)

r[[2, 3], [2, 3]]

b)

r [2: 4, 2: 4]

c)

r[[2, 4], [2, 4]]

d)

r[2: 3, 2: 3]

6.

import re

s = ACBСAC"

For the given string, which of the following regular expressions can be used to check if the string starts with 'AC'?

a)

re. findall(' [^A]C', s)

b)

re. findall(' ^AC', s)

c)

re. findall(' AC', s)

d)

re.findall('^[AC]', s)

7.

What will be the output of the variable L after the following code is executed?

import re

s = 'ACAABAACAAAB'

result = re.findall('A{1,2}', s)

L = len(result)

a)

8

b)

12

c)

5

d)

4

8.

Which of the following is the correct regular expression to extract all the phone numbers from the following chunk of

text:

'Office of Research Administration: (734) 647-6333 | 4325 North Quad

Office of Budget and Financial Administration: (734) 647-8044 | 309 Maynard, Suite 205

Health Informatics Program: (734) 763-2285 | 333 Maynard, Suite 500

Office of the Dean: (734) 647-3576 | 4322 North Quad

UMSI Engagement Center: (734) 763-1251 | 777 North University

Faculty Administrative Support Staff: (734) 764-9376 | 4322 North Quad'

a)

[(]\d{3}[)]\s\d{3}[-]\d{4}

b)

\d{3}[-]\d{3}[-]\d{4}

c)

\d{3}\s\d{3}[-]\d{4}

d)

[(]\d{3}[)]\d{3}[-]\d{4}

9.

Which of the following regular expressions can be used to get the domain names (e.g. google.com, www.baidu.com) from the following sentence?

'I refer to https://google.com and I never refer http://www.baidu.com if I have to search anything'

a)

(?<=https:\/\/)([.]*)

b)

(?<=https:\/\/)([A-Za-z0-9.]*)

c)

(?<=https:\/\/)([A-Za-z0-9]*)

d)

(?<=[https]:\/\/)([A-Za-z0-9.]*)

10.

The text from the Canadian Charter of Rights and Freedoms section 2 lists the fundamental freedoms afforded to everyone. Of the four choices provided to replace X in the code below, which would accurately count the number of fundamental freedoms that Canadians have?

text=r'''Everyone has the following fundamental freedoms:

    (a) freedom of conscience and religion;

    (b) freedom of thought, belief, opinion and expression, including freedom of the press and other media of communication;

    (c) freedom of peaceful assembly; and

    (d) freedom of association.'''

import re

pattern = X

print(len(re.findall(pattern,text)))

a)

'freedom'

b)

\(.\) 

c)

'[a-d]'

d)

(.)