Asiman Data School

Module 1: Foundations of Python Programming. After topic 12.

Python final exam

Everything from Topics 1 to 12 in one exam: the language itself, small programs, and a real table of data to clean and analyse.

questions in three parts
34 questions in three parts
points
100 points
the topics it covers
1 to 12 the topics it covers

How the exam works

The three parts of the exam
PartQuestionsPointsWhat you do
A. Multiple choice1 to 1010 x 2 = 20Choose one answer
B. Coding11 to 188 x 4 = 32Write a small program
C. Hands-on with data19 to 3416 x 3 = 48Work on a table with pandas
  • Your answers are saved in this browser while you write. You can close the page and continue later on the same device.
  • You can run your code as often as you like. While you work, nothing is marked right or wrong.
  • At the end you press Submit the exam. Every answer is checked, you see your points, and the answer key opens with a sample solution for every question.
  • After submitting you cannot change your answers.
  • There is no timer on this page. Your instructor tells you how much time you have.

AMultiple choice

Ten questions, 2 points each. Choose one answer for every question. You can change your choice until you submit.

Question 1

1.What does this print?

2 points
print(7 // 2, 7 % 2, 2 ** 3)
Question 2

2.Which of these values can be changed after it is created?

2 points
Question 3

3.What does this print?

2 points
x = 15
if x > 10:
    print("A")
elif x > 5:
    print("B")
else:
    print("C")
Question 4

4.What does this print?

2 points
for i in range(2, 10, 3):
    print(i, end=" ")
Question 5

5.What does this print?

2 points
def add(a, b=10):
    return a + b

print(add(5), add(5, 1))
Question 6

6.What does this print?

2 points
prices = {"tea": 2, "coffee": 4}
prices["juice"] = 3
prices["tea"] = 5
print(len(prices), prices["tea"])
Question 7

7.What does this print, line by line?

2 points
try:
    number = int("abc")
    print("ok")
except ValueError:
    print("bad")
finally:
    print("end")
Question 8

8.What does this print?

2 points
import numpy as np

a = np.array([1, 2, 3, 4])
print(a[a > 2] * 2)
Question 9

9.Inside a method of a class, what is self?

2 points
Question 10

10.You want to see how the values of one number column are spread: where most of them are, and whether there is a long tail. Which chart fits?

2 points

BCoding

Eight small programs, 4 points each. Every program reads its input with input() and prints one line. The lines that read the input are already written: keep them. The examples show the exact form of the output.

Run tries your code with the input in the box under the editor; change the input to try other cases. When you submit, each program is run with several test inputs that you do not see, and you get the points in proportion to the tests it passes.

11.Ticket price

4 points

A museum sells tickets by age: children under 7 enter free (print 0), ages 7 to 17 pay 5, ages 18 to 64 pay 10, and visitors of 65 and older pay 6. Read an age and print the price.

Examples: the input 30 gives 10the input 6 gives 0

Opening the editor...
Shift + Enter runs

12.Hours and minutes

4 points

Read a number of minutes. Print it as hours and minutes in exactly this form: 2 h 15 min.

Examples: the input 135 gives 2 h 15 minthe input 60 gives 1 h 0 min

Opening the editor...
Shift + Enter runs

13.Sum of the digits

4 points

Read a whole number that is not negative. Print the sum of its digits. Use a loop.

Examples: the input 1234 gives 10the input 7 gives 7

Opening the editor...
Shift + Enter runs

14.Count the vowels

4 points

Read a line of text. Print how many of its letters are vowels (a, e, i, o, u). Capital letters count too.

Examples: the input Data Science gives 5the input Python gives 1

Opening the editor...
Shift + Enter runs

15.Numbers divisible by 3

4 points

Read a whole number n. Print the sum of all numbers from 1 to n that are divisible by 3.

Examples: the input 10 gives 18the input 1 gives 0

Opening the editor...
Shift + Enter runs

16.Above the average

4 points

Read whole numbers written on one line with spaces between them. Print how many of them are greater than their average.

Examples: the input 4 8 15 16 23 42 gives 2the input 5 5 5 5 gives 0

Opening the editor...
Shift + Enter runs

17.The most frequent word

4 points

Read words written on one line with spaces between them. Print the word that appears most often, then how many times it appears, on one line. If several words share the highest count, print the one that appears first in the line. Use a dictionary.

Examples: the input red blue red green blue red gives red 3the input one gives one 1

Opening the editor...
Shift + Enter runs

18.Prime or not

4 points

Write a function is_prime(n) that returns True when n is a prime number and False when it is not. A prime number is greater than 1 and can be divided only by 1 and by itself. The last two lines read a number and print the result: keep them.

Examples: the input 17 gives Truethe input 2 gives True

Opening the editor...
Shift + Enter runs

CHands-on with data

Sixteen questions about one data file, 3 points each. The file employees.csv has one row for every employee of a company. It was typed in by hand, so some values are missing and a few rows were entered twice.

The data file of the exam

256 rows and 8 columns. On this page it is already there for your code; download it to work in Jupyter or Colab.

employees.csv, 12 KB

The columns of employees.csv
ColumnWhat it holds
namethe first name of the employee
departmentIT, Sales, Operations, Finance, Marketing or HR
citythe city of the office
agein years
yearshow many years the employee has worked at the company
salarythe salary of one month
scorethe result of the last review, from 1 to 5
hiredthe date the employee started

The data file: employees.csv

256 rows, 8 columns. A cell marked "empty" has no value in the file: pandas shows it as NaN.

Download

Loading the table ...

This part works like one notebook. Question 19 reads the file into the table df. Every later question continues from question 19: your answer there is run first, so df is ready and you do not read the file again. A few questions also continue from another question, and they say so. Print exactly what a question asks for, in the order it asks.

19.Import the data

3 points

Import pandas as pd, read the file employees.csv into a table named df, and print its first 5 rows.

Opening the editor...
Shift + Enter runs

20.Size and types

3 points

Print the shape of the table. On the next lines print the data type of every column.

Opening the editor...
Shift + Enter runs

21.The numerical features

3 points

Print the names of the numerical columns as a list.

Opening the editor...
Shift + Enter runs

22.Summary statistics

3 points

Print the summary statistics of the numerical columns (describe), rounded to 2 decimals.

Opening the editor...
Shift + Enter runs

23.Missing values

3 points

Print the number of missing values of every column.

Opening the editor...
Shift + Enter runs

24.Duplicates

3 points

Print the number of duplicated rows. On the next line print the shape the table would have without them. Do not change df.

Opening the editor...
Shift + Enter runs

25.Fill the missing salaries

3 points

The column salary has missing values, and a few salaries are very high. Fill the missing salaries with the mean or with the median: choose the one that the very high salaries do not pull up. Print the value you filled with. On the next line print the number of missing salaries after filling.

Opening the editor...
Shift + Enter runs

26.Fill the missing cities

3 points

The column city is text, so no mean can fill it. Fill its missing values with the most frequent city. Then print the number of employees of every city (value_counts).

Opening the editor...
Shift + Enter runs

27.Fill the missing scores

3 points

Fill the missing values of score with the mean score, rounded to 1 decimal. Print the value you filled with. On the next line print the mean of the column after filling, rounded to 2 decimals.

Opening the editor...
Shift + Enter runs

28.A filter with two conditions

3 points

Print how many employees of the department IT are older than 40.

Opening the editor...
Shift + Enter runs

29.Hired in each year

3 points

Add a column year: the year of the date in hired. Print the number of employees hired in each year, the years in order from the earliest to the latest (value_counts, then sort_index).

Opening the editor...
Shift + Enter runs

30.Mean salary by department

3 points

Calculate the mean salary of every department, rounded to 2 decimals, the highest first. Store the result in by_department and print it.

This question also continues from Question 25: your answer there is run first.

Opening the editor...
Shift + Enter runs

31.The cities

3 points

For every city print the number of employees and their mean age, rounded to 1 decimal: group by city, take the column age, and use agg with count and mean.

This question also continues from Question 26: your answer there is run first.

Opening the editor...
Shift + Enter runs

32.The five best paid

3 points

Print the name, department and salary of the 5 employees with the highest salary, the highest first.

Opening the editor...
Shift + Enter runs

33.Ages as a NumPy array

3 points

Turn the column age into a NumPy array named ages (to_numpy). Print how many employees are older than the mean age. On the next line print the mean age of these employees, rounded to 1 decimal.

Opening the editor...
Shift + Enter runs

34.A chart

3 points

Import matplotlib.pyplot as plt and draw the mean salary of every department (by_department) as a bar chart, with a title and a label for each axis. Nothing has to be printed.

This question also continues from Question 25 and Question 30: your answer there is run first.

Opening the editor...
Shift + Enter runs

Submit the exam

When you submit, every answer is checked, you see your points, and the answer key opens: the right answers of Part A and a sample solution for every question of Parts B and C. After submitting you cannot change your answers.

Part A: Multiple choice
0 of 10 answered
Part B: Coding
0 of 8 answered
Part C: Hands-on with data
0 of 16 answered

Optional. It is written on your result, for example when you save it as a PDF for your instructor. It stays in this browser.