Exam

Last updated: 2022-06-25 01:13:42

General instructions

  • The exam takes place in a regular class, with pen and paper. The exam is not in front of a computer.

  • You need to bring a calculator to the exam!

  • Exam duration is 3 hours.

  • The exam is comprised of 20 multiple-choice questions. Each question gives a short code section. The code section is not identical to the ones that appeared in the course materials, but they combine functions and methods which were learned. You need to choose the right code output out of 4 options.

Material for the exam

The material for the exam is in chapters Python basics through Geometric operations, namely:

Topics from these chapters that will not appear in the exam are:

Note

Information appearing in notes (such as this one) will not be on the exam.

Sample exam questions

Question 1

What is the result of the following code section?

x = [3, 0, 2, 9]
x[x[1]:x[0]]

(A) [3]

(B) [3, 0]

(C) [3, 0, 2]

(D) [3, 0, 2, 9]

Question 2

What is the result of the following code section?

x = [3, 0, -1, 9]
result = 0
for i in x:
    result = result + 1
result 

(A) 1

(B) 12

(C) 11

(D) 4

Question 3

What is the result of the following code section?

x = [3, 0, -1, 9]
x.append(4)
x

(A) [3, 0, -1, 9, 4]

(B) []

(C) [3, 0, -1, 9]

(D) [[3, 0, -1, 9], 4]

Question 4

What is the result of the following code section?

counter = 0
for i in range(10):
    if i in [5, 10, 15]:
        counter = counter + 1
counter

(A) 0

(B) 1

(C) 2

(D) 3

Question 5

Suppose that the working directory contains a plain text file named tmp.csv, with the following contents:

group,count
a,2
a,3
a,2
b,7
b,5
b,9

What is the result of the following code section?

import csv
file = open('tmp.csv', 'r', encoding='utf-8')
reader = csv.DictReader(file)
x = 0
for row in reader:
    if row['group'] == 'a':
        x += int(row['count'])
file.close()
x

(A) 'aaa'

(B) '232'

(C) 2

(D) 7


For questions 6-10, we define a numpy array named a, as follows:

import numpy as np
a = np.array([
    [[5, 1, 9], [3, 6, 3], [0, 1, np.nan], [4, 9, np.nan]],
    [[4, 8, 1], [2, 0, 0], [1, 2, np.nan], [2, 1, 7]]
])
a
array([[[ 5.,  1.,  9.],
        [ 3.,  6.,  3.],
        [ 0.,  1., nan],
        [ 4.,  9., nan]],

       [[ 4.,  8.,  1.],
        [ 2.,  0.,  0.],
        [ 1.,  2., nan],
        [ 2.,  1.,  7.]]])

Question 6

What is the result of the following code section?

np.max(a, axis=0)[3, :]

(A) array([nan, nan])

(B) array([ 4.,  9., nan])

(C) array([4., 9., 7.])

(D) array([ 1.,  2., nan])

Question 7

What is the result of the following code section?

a[1, :, :].flatten().shape

(A) (12,)

(B) (1, 4, 3)

(C) (24,)

(D) (1,)

Question 8

What is the result of the following code section?

(~np.isnan(a[1, :, :])).sum()

(A) 0

(B) 1

(C) 10

(D) 11

Question 9

What is the result of the following code section?

np.isnan(a.astype(np.int64).astype(np.float64)).any()

(A) 3

(B) True

(C) False

(D) 0

Question 10

What is the result of the following code section?

(a[0, 1, :] > 4) & (a[0, 1, :] < 8)

(A) array([0, 0, 1])

(B) array([False,  True, False])

(C) array([False, False, False])

(D) array([False, False,  True])


For questions 11-15, we define four Series objects named name, district, population, and established, and a DataFrame named dat, as follows. The DataFrame contains the names, districts, population estimates (in 2019), and establishment years, for the five biggest cities in Israel:

import numpy as np
import pandas as pd
name = pd.Series(['Jerusalem', 'Tel Aviv-Yafo', 'Haifa', 'Rishon LeZion', 'Petah Tikva'])
district = pd.Series(['Jerusalem', 'Tel Aviv', 'Haifa', 'Center', 'Center'])
population = pd.Series([936425, 460613, 285316, 254384, 247956])
established = pd.Series([np.nan, np.nan, np.nan, 1882, 1878])
dat = pd.DataFrame({
    'name': name, 
    'district': district, 
    'population': population, 
    'established': established
})
dat
name district population established
0 Jerusalem Jerusalem 936425 NaN
1 Tel Aviv-Yafo Tel Aviv 460613 NaN
2 Haifa Haifa 285316 NaN
3 Rishon LeZion Center 254384 1882.0
4 Petah Tikva Center 247956 1878.0

Question 11

What is the result of the following code section?

(dat['population'] > 400000).sum()

(A) 1397038

(B) 2

(C) 5

(D) 0.4

Question 12

What is the result of the following code section?

dat.groupby('district').nunique().reset_index()['name'].to_numpy()

(A) array([2, 1, 1, 1])

(B) array([1, 1, 1, 1, 1])

(C) array(['Jerusalem', 'Tel Aviv', 'Haifa', 'Center'], dtype=object)

(D) array([5])

Question 13

What is the result of the following code section?

dat2 = pd.DataFrame({'district': ['Haifa', 'Jerusalem'], 'ports': [1, 0]})
dat3 = pd.merge(dat, dat2, on='district', how='left')
dat3['ports'].to_numpy()

(A) array([nan, nan, nan, nan, nan])

(B) array([1, 0])

(C) array([0., 0., 1., 0., 0.])

(D) array([ 0., nan,  1., nan, nan])

Question 14

What is the result of the following code section?

dat['established'].min()

(A) nan

(B) 1882.0

(C) 1878.0

(D) 0

Question 15

What is the result of the following code section?

dat[['name', 'population']].iloc[0:3, :].shape

(A) (3, 2)

(B) (4, 2)

(C) (2, 3)

(D) (2, 2)


For questions 16-20, we define:

  • three shapely geometry objects line1, line2, and line3, and

  • a GeoDataFrame object named layer,

as follows:

import shapely.geometry
import geopandas as gpd
line1 = shapely.geometry.LineString([(0, 0), [0, 1]])
line2 = shapely.geometry.LineString([(0, 1), [1, 1]])
line3 = shapely.geometry.LineString([(1, 1), [1, 0]])
layer = gpd.GeoDataFrame({
    'id': [1, 2, 3], 
    'count': [5, 2, 2], 
    'geometry': [line1, line2, line3]}
)
layer
/home/michael/.local/lib/python3.8/site-packages/geopandas/_compat.py:111: UserWarning: The Shapely GEOS version (3.10.2-CAPI-1.16.0) is incompatible with the GEOS version PyGEOS was compiled with (3.10.1-CAPI-1.16.0). Conversions between both will be slow.
  warnings.warn(
id count geometry
0 1 5 LINESTRING (0.00000 0.00000, 0.00000 1.00000)
1 2 2 LINESTRING (0.00000 1.00000, 1.00000 1.00000)
2 3 2 LINESTRING (1.00000 1.00000, 1.00000 0.00000)

Question 16

What is the result of the following code section?

x = layer['geometry'].iloc[0].buffer(1.1)
y = layer['geometry'].iloc[2]
x.intersects(y)

(A) 0

(B) True

(C) False

(D) 1

Question 17

What is the result of the following code section?

layer.dissolve(by='id')['count'].to_numpy()[0:]

(A) array([5])

(B) [5]

(C) array([5, 2, 2])

(D) [5, 2, 2]

Question 18

What is the result of the following code section?

shapely.geometry.box(*layer.total_bounds).area

(A) 1.0

(B) 2.0

(C) 3.0

(D) 4.0

Question 19

What is the result of the following code section?

layer.distance(shapely.geometry.Point([0.5, 1])).to_numpy()

(A) array([0.5, 0.5, 0.5])

(B) array([0, 0, 1])

(C) array([0.5, 0. , 0.5])

(D) array([1, 0, 0])

Question 20

What is the result of the following code section?

pnt = shapely.geometry.Point((0, 1))
layer2 = gpd.GeoDataFrame({'group': 'A', 'geometry': [pnt]})
gpd.sjoin(layer2, layer, how='left')['id'].to_numpy()

(A) array([1, 2])

(B) array([1, 2, 3])

(C) array([1])

(D) array([2])