Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Sample Test

April 10, 2023

1. (10 points) Write the following mathematical expressions in Python.

a. 𝑠 = 𝑠0 + 𝑣0 𝑡 + 12 𝑔𝑡2 + 13 𝑔𝑡3

𝐶 𝐷
b. 𝐴 = 𝐵5 (1 + 100 )

2. (10 points) Write a program that reads four integers and prints “two pairs” if the input
consists of two matching pairs (in some order) and “not two pairs” otherwise. For example,
1 2 2 1 two pairs
1 2 2 3 not two pairs
2 2 2 2 two pairs
3. (10 points) Write a function that takes in a string and returns a new string with all vowels
replaced with the letter ‘x’. For example,
Take a string: "Hello World!"
The new string will be "Hxllx, wxrld!"
4. (10 points) Use recursion to determine the number of digits in an integer n. Hint: If n is <
10, it has one digit. Otherwise, it has one more digit than n // 10.
5. (10 points) Write a python program that reads a file containing a list of names and sorts
them alphabetically. Use exception handling to handle cases where the file cannot be opened
or an error occurs during sorting. For example,
Sorted names:
Anh
Cuong
Hai
Lam
Thai
Tuan
6. (10 points) Write a program that allows a user to input two sets of integers and then prints
the union, intersection, and difference of the sets. For example,
set1 = {1, 3, 5, 7, 9}
set2 = {2, 3, 4, 6, 8}
Union of the sets is: {1, 2, 3, 4, 5, 6, 7, 8, 9}
Intersection of the sets is: {3}
Difference of set1 and set2 is: {1, 5, 9, 7}
Difference of set2 and set1 is: {8, 2, 4, 6}

1
7. Write a program to create a dictionary with student names and their corresponding test
scores, and returns the name of the student with the highest score. For example,
{'Alice': 85, 'Bob': 92, 'Charlie': 80, 'David': 95}
Return the name: David
8. (10 points) Write a program to:
• Create a class called “Animal” with methods “eat” and “move”.
• Create inheriting classes “Bird” and “Mammal”. Each should have a unique attribute, and
their own implementation of “move”.
For example,
Sparrow is eating.
Sparrow is flapping its wings and flying.
Cheetah is eating.
Cheetah is running at 70 mph.
9. (10 points) Write a program to create a 2D array of random integers using
numpy.random.randint() function and calculate the average value of each column. For
example,
Array:
[[ 7 7 1 3]
[ 9 6 3 6]
[ 9 3 10 6]
[ 5 8 7 5]
[ 4 7 2 5]]
Column averages:
[6.8 6.2 4.6 5. ]
10. (10 points) Write a program to creat and sort the below dataframe in pandas based on the
column “Salary”.
Name Age Salary
0 John 25 50000
1 Alice 30 60000
2 Bob 18 40000
3 Jane 21 35000
4 Dan 27 70000

You might also like