0% found this document useful (0 votes)
23 views13 pages

MEK1300 Python Programming Exam

This document is the final exam for the MEK1300 Programming 1 course at OsloMet, consisting of multiple choice questions, fill-in-the-blank programming exercises, and complete programming tasks. The exam covers various Python programming concepts including syntax, data types, control structures, and functions. The total score for the exam is 100 points, divided among different parts.

Uploaded by

aamar07865
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views13 pages

MEK1300 Python Programming Exam

This document is the final exam for the MEK1300 Programming 1 course at OsloMet, consisting of multiple choice questions, fill-in-the-blank programming exercises, and complete programming tasks. The exam covers various Python programming concepts including syntax, data types, control structures, and functions. The total score for the exam is 100 points, divided among different parts.

Uploaded by

aamar07865
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Department of Mechanical, Electronics and Chemical Engineering

Faculty of Technology, Art and Design


OsloMet - Oslo Metropolitan University
MEK1300 – Programming 1
Final Exam

Date: 2 December 2020

Student Number: ______________________ Name & Surname: ____________________________

Part Points Score


One 20
Two 40
Three 40
Total 100

Page 1 of 13
Part One – Multiple Choice Questions (20 points)
1. What is the correct syntax to output “MEK1300” in Python?

a. print “MEK1300”
b. print ‘MEK1300’
c. print (“MEK1300”)
d. print (MEK1300)

2. Which one is a legal variable name in Python?

a. number 1
b. 1 number
c. 1number
d. number1

3. A variable defined outside a function is ---------------------.

a. a global variable
b. a static variable
c. a local variable
d. a dynamic variable

4. What is the correct syntax to output the number of items in an object “x”?

a. print (len (x))


b. print (lenof (x))
c. print (len of x)
d. print (LenOf (x))

5. What is the operator to test equality in Python?

a. =
b. !=
c. isEqual
d. ==

6. Which option is correct to create an empty set in Python?

a. {}
b. []
c. ()
d. set()

7. What is the correct way to create a function called my_func in Python?

a. create my_func:
b. def my_func ():
c. create my_func ():
d. def my_func:

Page 2 of 13
8. What is the correct syntax to print the first character in the following string?
course = “MEK1300”

a. print (course [0])


b. print (course [1])
c. print (course, 0)
d. print (course, 1)

9. Which method can be used to return a string in lower case letters?

a. lowercase ()
b. lowerCase ()
c. lower ()
d. islower ()

10. Which of these collections defines a Tuple?

a. (1, “a”, True, 2.3)


b. [1, “a”, True, 2.3]
c. {1, “a”, True, 2.3}
d. None of them

11. Which statement is True?

a. A list is a collection of items which is unordered and changeable.


b. A tuple is a collection of items which is ordered and changeable.
c. A set is a collection of items which is unordered and indexed.
d. A dictionary is a collection of items (keys and values) where each item can be
accessed by referring to its key name, inside square brackets.

12. Which statement is used to stop a loop in Python?

a. exit
b. break
c. continue
d. stop

13. Suppose my_list = [1, 2, 3, 4, 5], which of the following is the correct syntax to print the
elements of the list in reverse order?

a. print (my_list [ : : 1])


b. print (my_list [ : -1: ])
c. print (my_list [ : -1])
d. print (my_list [ : : -1])

14. Suppose my_list = [1, 2, 3, 4, 5], what is my_list [-1]?

a. 1
b. 5
c. None
d. Error

Page 3 of 13
15. What is the output of the following code?

string = “MEK1300”
print (string [1: 3])

a. EK
b. MEK
c. ME
d. KEM

16. Which operator has higher precedence in Python in the following list of options?

a. () Parentheses
b. * Multiplication
c. + Addition
d. – Subtraction

17. If we have two sets s1 and s2, and we want to check if all the elements of s1 are present
in s2 or not, we can use:

a. [Link] (s2)
b. [Link] (s1)
c. [Link] (s2)
d. [Link] (s1)

18. Suppose my_tuple = (1, 2, 3, 4, 5), which of the following is incorrect?

a. print (my_tuple[4])
b. my_tuple [4] = 10
c. print (len (my_tuple))
d. print (max (my_tuple))

19. What type of data is x = [(1, 2), (2, 1), (3, 5)]?

a. List of tuples
b. Tuple of lists
c. Set of tuples
d. Invalid type

20. If my_set = {“a”, “b”, “c”}, what happens when my_set.add(“b”) is executed?

a. Error as “b” already exists in my_set.


b. Error as there is no add method for set data type.
c. “b” is added to the set and my_set will be {“a”, “b”, “c”, “b”}
d. “b” is not added to the my_set as sets consist of only non-duplicate elements.

Page 4 of 13
Part Two – Fill in the Blanks (40 points)
Fill in the blanks of the following Python programs. Note that you are not allowed to define
new variables or objects. Please ONLY use the space provided for your answers.

a. (5 points) The following program gets unknown number of positive integers from the
user and counts the number of even numbers entered. The program stops when the
user enters 0. Assume that the user does not enter invalid inputs.

Sample Run

Enter a number (ZERO to stop): 10


Enter a number (ZERO to stop): 5
Enter a number (ZERO to stop): 27
Enter a number (ZERO to stop): 4
Enter a number (ZERO to stop): 0

Number of even integers: 2

count = --------------

number = int (input (“Enter a number (ZERO to stop): ”))

while ------------------------------:

if -----------------------------:

count = count + 1

number = -------------------------------------------------------------------

print (“Number of even integers:”, -------------------)

Page 5 of 13
b. (5 points) The following program inputs a student’s average between 0 and 100 and
displays 4 if the average is 90-100, 3 if the average is 80-89, 2 if the average is 70-79, 1
if the average is 60-69, and 0 if the average is lower than 60. Assume that the class has
5 students. The function called quality_points will get a student’s average and returns
the appropriate quality points. Assume that the user does not enter invalid inputs.

Sample Run

Enter an average: 70
Quality point: 2

Enter an average: 90
Quality point: 4

Enter an average: 20
Quality point: 0

Enter an average: 100


Quality point: 4

Enter an average: 85
Quality point: 3

def quality_points (average):

if average >= 90:


return 4
elif average >= 80:
return 3
elif average >= 70:
return 2
elif average >= 60:
return 1
else:
return 0

for i in range (-------------):

average = int (input ("---------------------------------------- "))

result = ------------------------------------------------- ### Call the function

print("Quality point:", -------------------------)

Page 6 of 13
c. (5 points) The following program gets a string from the user and counts the number of
digits in the string.

Sample Run

Enter a string: MEK1300

Number of Digit(s): 4

string = input ("Enter a string: ")

count_digit = -------------

for ---------------- in -------------------: ### Loop over each character in the string
if -----------------------------: ### Check if the character is a digit or not
count_digit = ------------------------------------

print ("Number of Digit(s):", count_digit)

d. (5 points) The following program prints the positive odd numbers till the user input.
Assume that the user does not enter invalid inputs.

Sample Run

Enter a positive integer: 10

The odd number(s) till number 10 ---> 1 3 5 7 9

n = int (input ("Enter a positive integer: "))

i = ------------- ### counter for the loop

if n < 1:
print ("Invalid input!!!")

else:
print ("The odd number(s) till number", n, "--->", end=" ")

while -------------------:

if --------------------:
print (--------------, end=" ")

i = ----------------------

Page 7 of 13
e. (3 points) Given a list of words, word_list, the following program prints one line for each
word, repeating that word twice. For example, if word_list is ['John', 'Sara', 'Emma'], then
the program prints

John John
Sara Sara
Emma Emma

word_list = ["John", "Sara", "Emma"]

for i in range (---------------------------):


print (-------------------------)

f. (7 points) The following program reads ten random integer numbers into a list, and
sends the list to a function to display the elements in the opposite order from which
they were entered.

Sample Run

List in original order: 8 85 14 4 69 91 37 33 39 48

List in reverse order: 48 39 33 37 91 69 4 14 85 8

from random import randint

def main ():


my_list = ------------ ### Initialize the list

for i in range (-----------------):


number = randint (1, 100)
------------------------------------ ### Add numbers to the list

print ("List in original order:", end=" ")


for element in my_list:
print (element, end=" ")
print ()

---------------------------------------------------- ### Call print_list_reverse funcrion

def print_list_reverse (my_list):


print ("List in reverse order: ", end=" ")
for number in ---------------------------:
print (number, end=" ")

main ()

Page 8 of 13
g. (5 points) The following program finds the largest element in a list of integers.

Sample Run

List: [1, 2, 15, -2, 45, 3, 1, 0, -89, 5]

Largest element: 45

my_list = [1, 2, 15, -2, 45, 3, 1, 0, -89, 5]

largest = -----------------------

i=1

while i < ----------------------:


if ------------------------------------------:
largest = -------------------------
----------------------

print ("List:", my_list)


print ("Largest element:", largest)

Page 9 of 13
h. (5 points) In the following program, function unique takes a dictionary and returns True
if every value in the dictionary has only one corresponding key. For example, if the
dictionary is {'key1': 4, 'key2': 5, 'key3': 3}, the function returns Ture but if the dictionary
is {'key1': 2, 'key2': 4, 'key3': 2}, the function returns False. Then a message is displayed
accordingly.

Sample Run

{'key1': 2, 'key2': 4, 'key3': 2}

The dictionary does not have unique values.

def unique (dictionary):


unique_value = set ()
for value in -------------------------------------------: ### Loop over values in the dictionary
if value in ------------------------------------------:
return False
--------------------------------------------------------- ### Add value to unique_value set
return True

my_dictionary = {'key1': 2, 'key2': 4, 'key3': 2}

result = ------------------------------------------------- ### call the function

print(my_dictionary)

if --------------------------:
print("The dictionary has unique values.")
else:
print("The dictionary does not have unique values.")

Page 10 of 13
Part Three – Complete Python Programs (40 points)
1. (10 points) Write a Python program that prints all positive integer numbers in reverse
from n to 1 by using a while loop. Note that the value of n is entered by the user. Assume
that n is a positive integer greater than one, and the user does not enter invalid inputs.

Sample Run:

Enter n: 5

Positive integer numbers from 5 to 1: 5 4 3 2 1

Page 11 of 13
2. (15 points) Write a Python program that generates a list with 10 random integer
numbers between 1 and 50. Then write a function called substitute that accepts the
list as an argument and evaluates its elements. If an element is a multiple of 5, then
that element will be substituted by its square. For example, if the first element of the list
is 10, then it will be substituted by its square which is 100.

Sample Run:

Before substitution, the list is: [10 6 5 9 2 11 17 1 15 20]


After substitution, the list is: [100 6 25 9 2 11 17 1 225 400]

Page 12 of 13
3. (15 points) Write a Python program using a function def translate (dictionary,
phone_number) that takes a dictionary and a phone number (entered by the user) as
arguments, translates the digits in phone number to words and returns it as a string.
Consider the following two functions (main and translate) and write their definitions.
Remember that the translate function should return a string and then the output will be
printed in the main function.

Sample Run:

Enter your phone number: 93535352


Your phone number is: Nine Three Five Three Five Three Five Two

def main ():

def translate (dictionary, phone_number):

main ()
--- END OF EXAM ---

Page 13 of 13

You might also like