Mid2 2
Mid2 2
Instructions
This exam contains 38 questions, each worth equal points. Answer all questions
in the space provided.
1
Question 1: Write a Python function is even(n) that returns True if n is
even and False otherwise.
2
Question 2: Write a Python loop that prints all numbers from 1 to 100 that
are divisible by 3.
3
Question 3: Define a Python function reverse string(s) that returns the
reverse of the input string s.
4
Question 4: Write a Python function count vowels(s) that returns the num-
ber of vowels in the input string s.
5
Question 5: Consider the list numbers = [3, 7, 2, 9, 5]. Write code to
sort the list in descending order and print the result.
6
Question 6: Write a Python function factorial(n) that returns the factorial
of a positive integer n using a loop.
7
Question 7: Create a dictionary that stores the squares of numbers from 1 to
5. Write code to print the dictionary.
8
Question 8: Write a Python function is prime(n) that returns True if the
number n is prime and False otherwise.
9
Question 9: Write code to count the number of words in the string s =
"python is fun".
10
Question 10: Write a Python function sum even numbers(n) that returns the
sum of all even numbers from 1 to n.
11
Question 11: What will the following code print?
my_list = [1, 2, 3]
my_list[1] = 10
print(my_list)
12
Question 12: Write a Python function concat strings(list of strings)
that takes a list of strings and returns them concatenated into a single string.
13
Question 13: Write code to create a Python dictionary grades with keys as
student names and values as their scores. Add an entry for a student named
”Alice” with a score of 95.
14
Question 14: Write Python code to open a file named "data.txt", read its
contents, and print each line.
15
Question 15: Write code to import the math module and use it to calculate
the square root of 16.
16
Question 16: Write a function get length(lst) that returns the length of a
list lst.
17
Question 17: Write Python code to check if the string "apple" is in the list
fruits = ["apple", "banana", "cherry"].
18
Question 18: Write a Python function double values(numbers) that takes a
list of numbers and returns a list with each value doubled.
19
Question 19: Create a list containing the numbers from 1 to 10 and print it.
20
Question 20: Write code to remove the last element from the list items =
[1, 2, 3, 4, 5].
21
Question 21: Write a Python function square(n) that returns the square of
a number n.
22
Question 22: Write code to check if the key ’Alice’ exists in the dictionary
person = {’name’: ’John’, ’age’: 25}.
23
Question 23: Write a function add five(numbers) that takes a list of numbers
and returns a new list with 5 added to each element.
24
Question 24: Use a Python list comprehension to create a list of squares from
1 to 10.
25
Question 25: Write a function count even(lst) that returns the number of
even numbers in a list lst.
26
Question 26: Write Python code to open a file named "output.txt" in write
mode and write the text "Hello, World!" into it.
27
Question 27: Write a Python function find max(lst) that returns the maxi-
mum value in a list lst.
28
Question 28: Use a loop to print the numbers from 10 down to 1.
29
Question 29: Write Python code to calculate and print the sum of numbers
from 1 to 50.
30
Question 30: Write a function replace vowels(s) that returns a string with
all vowels replaced by "*".
31
Question 31: Write code to check if the file "example.txt" exists using the
os module.
32
Question 32: Write a function merge dictionaries(dict1, dict2) that re-
turns a new dictionary containing all items from both dictionaries.
33
Question 33: Write Python code to print the length of the string "Python
programming".
34
Question 34: Write a function find min(lst) that returns the smallest num-
ber in a list lst.
35
Question 35: Write Python code to convert the string "123" to an integer and
print it.
36
Question 36: Write a function unique elements(lst) that returns a list with
all unique elements from lst.
37
Question 37: Use a list comprehension to create a list of even numbers from
1 to 20.
38
Question 38: Write code to reverse the list letters = [’a’, ’b’, ’c’,
’d’].
39