Exam 2021-02-18

Here you find the questions on the exam 2021-02-18.

Information

Start by reading through all questions. Max score is 30 points.

  • For grade 3, 40% of max score (12 points) is required
  • For grade 4, 60% of max score (18 points) is required
  • For grade 5, 80% of max score (24 points) is required

During the test, you are only allowed to use:

  • The computer you sit at to only answer the questions on this exam (you may not run any other program).
  • A dictionary to translate to/from English from/to your native language.

Write your answers in either English or Swedish. If you write your answers in Swedish, make sure to not introduce any translation confusement. Write proper sentences (spelling, upper/lower case characters, punctuation, etc.). Answers that do not do this good enough/are vague/are ununderstandable cannot receive full score on the questions.

Good luck!

Question 1

Question

Place the following lines of code in such order they can be executed (read underscores as white-spaces/indentation (Inspera doesn't allow the text to begin with white-spaces)).

  • ___return entered_number + number
  • def add_number(number):
  • sum = add_number(5)
  • ___entered_number = int(entered_text)
  • print("The sum is "+str(sum)+".")
  • ___entered_text = input("Enter number to add: ")

Question 2

Question

How many statements and expressions does the following code contain?

times_to_add_three = 10
the_sum = 0
while 0 < times_to_add_three:
   the_sum = the_sum + 3
   times_to_add_three = times_to_add_three - 1

Question 3

Question

What type of error does the code below contain? Syntax error, Logical error, Runtime error or no error at all? The purpose of the function is to compute the average value of the two numbers it receives (e.g. average(4, 6)5).

def average(number1, number2):
    return (number1 + number 2) / 2

Question 4

Question

What values will be stored in the variables i and x after the following code has been executed?

students = ["Alice", "Bob", "Claire", "David"]
i = 0
x = 0
while i < len(students):
    x = x + len(students[i])
    i = i + 1

Question 5

Question

What is what in the code below?

def multiply(numbers):
    return numbers[0] * numbers[1]
product = multiply([6, 2])

Pair each code piece with its corresponding name.

Code pieces:

  • numbers[1]
  • numbers
  • [2, 6]
  • product

Names:

  • Argument
  • Expression
  • Variable
  • Parameter

Note: Some code pieces may match multiple names, but there is only one way to get all 4 pairs right.

Question 6

Question

Which one of the following statements is a bad suggestion on how to represent data?

  • To represent a room with a width and a height, one can use a list containing two numbers, e.g. room = [10, 15].
  • To represent a book with a title, one can use a dictionary with a string, e.g. book = {"title": "There and back again"}.
  • To represent a planet with a name, one can use a string, e.g. planet_name = "Tellus".
  • To represent the number of books in a library, one can use an integer, e.g. number_of_books = 120000.
  • To represent the books in a library, one can use a list containing dictionaries, e.g:
    books = [
        {"title": "The Killer", "number_of_pages": 123},
        {"title": "Not Again", "number_of_pages": 253}
    ]
    

Question 7

Question

The following expression:

range(3, 9, 3)

Creates a range containing some integers. What is the sum of the integers in that range?

Question 8

Question

Below is a program that should:

  1. Ask the user to enter her name.
  2. Ask the user to enter a positive integer.
  3. Print the user's name as many times as the number she entered.
name = input("Enter your name: ")
times_to_print = input("Enter a positive integer: ")
for i in range(times_to_print):
   print(name[i])

When Claire runs the program, it does not work as intended. Explain why the program doesn't work they way it should, and suggest a solution to make it work (no need to write any Python code, describing the changes using words is enough, but be detailed).

Question 9

Question

What integers should be assigned to the variables a and b to slice out the values "r", "s" and "t" in the code below?

my_list = ["r", "s", "t", "u", "v"]
a = ?
b = ?
my_new_list = my_list[a:b]
# my_new_list should now be ["r", "s", "t"]

Question 10

Question

Write the following code:

my_list = [c+c for c in "abcdef"]

Using a for loop (and the other things you need) instead of a list comprehension expression. Your own code should have the exact same meaning as the code above.

Question 11

Question

Write the following code:

list = [-x for x in get_numbers() if is_positive(x)]

Using a for loop (and the other things you need) instead of a list comprehension expression. Your own code should have the exact same meaning as the code above.

Question 12

Question

Here is a quite complex structure with information about the population in different areas:

populations = {
    "countries": [
        {"name": "Sweden", "population": 9903000},
        {"name": "Finland", "population": 5495000},
        {"name": "Norway", "population": 5233000}
    ],
    "cities": [
        {"name": "Stockholm", "population": 789024},
        {"name": "Jönköping", "population": 122952}
    ]
}

Given this structure, write an expression that evaluates to the population of the country with the name Stockholm, i.e. 789024.

Note: Do not write a statement, and simply writing 789024 is of course not OK; the value needs to be retrieved from the structure.

Question 13

Question

Suggest how the following data in Python:

team = {"name": "Arsenal", "city": "London"}

Can be written in XML code instead.

Question 14

Question

Suggest how the following data in Python:

countries = [
   {"name": "Arsenal", "city": "London"},
   {"name": "Tottenham", "city": "London"},
   {"name": "Elfsborg", "city": "Borås"}
]

Can be written in CSV format. Do not write any extra characters not needed, but use as few characters as possible.

Question 15

Question

The following code crashes when it's executed.

def read_name():
   name = input("Enter name: ")
def print_name():
   print("Your name is "+name)
read_name()
print_name()

Explain why it crashes, and suggest what changes that needs to be made to make it work. Your solution should contain one function for reading the name from the user and one to print the name the user entered.

Question 16

Question

What will be stored in the variable sum after the following code has been executed?

def a(number):
    number = number + 1
def b(list):
    for number in list:
        number = number + 3
def c(list):
    for i in range(len(list)):
        list[i] = list[i] + 5
my_list = [1, 1]
c(my_list)
b(my_list)
b(my_list)
c(my_list)
sum = my_list[0] + my_list[1]

Question 17

Question

Implement the function get_abbreviation, which receives a string and should return the abbreviation of that string. Your function should support the following abbreviations:

StringAbbreviation
et ceteraetc.
withw/
youu

If the string is none of the strings found in the table above, return the string itself.

Sample usage:

get_abbreviation("with")   →   "w/"
get_abbreviation("as soon as possible")   →   "as soon as possible"

Question 18

Question

Write a program that keeps asking the user to enter an integer until the user enters stop. The program should then print the sum of the lowest and highest integer the user entered. When running the program, it can look like this (bold text represents text entered by the user).

Enter an integer or stop: 2
Enter an integer or stop: 6
Enter an integer or stop: 4
Enter an integer or stop: stop
The sum of the lowest and highest integer is 8.

Note: The output should look precisely as in the example above (including white-spaces, but with the exception of the boldness from the input, of course).

Note: You can expect the user to actually enter a number or stop (no error handling needed).

Note: You can expect the user to enter at least two numbers.

Question 19

Question

Implement the function is_sum_at_least_100, which receives a list of numbers, and returns:

  • true if the sum of the numbers is equal to or greater than 100.
  • false otherwise.

Write two different implementations of the function: one using (at least) one while loop, and another one using (at least) one for loop.

Sample usage:

is_sum_at_least_100([50, 50, 20])   →  true
is_sum_at_least_100([50, 50, -20])  →  false
is_sum_at_least_100([])             →  false

Question 20

Question

Implement the function int_to_digits, which receives a positive integer, and should return a list containing the digits in that integer.

Sample usage:

int_to_digits(123)   →  [1, 2, 3]
int_to_digits(5398)  →  [5, 3, 9, 8]

Question 21

Question

Given the following data about some movies houses:

houses = [
   {"owner": "Alice", "name": "Alice's Palace", "size": 320, "number_of_rooms": 9},
   {"owner": "Alice", "name": "Alice's Home",   "size": 84,  "number_of_rooms": 3},
   {"owner": "Alice", "name": "Alice's Cabin",  "size": 30,  "number_of_rooms": 1},
   {"owner": "Bob",   "name": "Bob's Home",     "size": 94,  "number_of_rooms": 4},
   {"owner": "Bob",   "name": "Bob's Cabin",    "size": 30,  "number_of_rooms": 1}
]

Write code that computes and prints the name of the human that owns the most number of rooms in total.

Note: Your code should still work as expected if one adds/removes houses to/from the list. Alice and Bob are not the only ones who can own a house.

Question 22

Question

The class MovieCollection represents a collection of movies. It has the following constructor/methods:

Constructor/MethodDescription
MovieCollection()Creates a new empty MovieCollection.
add_movie(name, runtime)Adds the movie with the given name and runtime (length in minutes) to the collection.
get_number_of_movies()Returns the number of movies in the collection.
get_total_runtime()Returns the sum of the runtime of all movies in the collection.
get_average_runtime()Returns the average runtime of the movies in the collection.

Write a program making use of the class. In the program, you should continue to ask the user to enter the name and runtime of a movie to be added to the MovieCollection until the user don't want to add any more. Then you should display the average runtime of the movies in the collection on the screen.

Sample usage:

Enter one more movie? Yes
Enter movie name: Catch me if you can
Enter movie runtime: 115
Enter one more movie? Yes
Enter movie name: See you later
Enter movie runtime: 75
Enter one more movie? No
Average runtime is 95.

Question 23

Question

The class MovieCollection represents a collection of movies. It has the following constructor/methods:

Constructor/MethodDescription
MovieCollection()Creates a new empty MovieCollection.
add_movie(name, runtime)Adds the movie with the given name and runtime (length in minutes) to the collection.
get_number_of_movies()Returns the number of movies in the collection.
get_total_runtime()Returns the sum of the runtime of all movies in the collection.
get_average_runtime()Returns the average runtime of the movies in the collection.

Implement the MovieCollection class per the description above.