EXAMEN PYTHON
Exercise I: Circle the correct answer (17 pts)
1. What does the file mode "wb" do?
a) Write text
b) Write binary
c) Read binary
d) Append binary
2. Which function is used to remove a file in Python?
a) [Link]()
b) remove()
c) [Link]()
d) [Link]()
3. What happens if a file opened with open() is not closed?
a) Python closes it automatically immediately
b) Data may not be saved properly
c) Nothing happens
d) Syntax error
4. Which keyword is used to handle exceptions in file handling?
a) error
b) catch
c) try
d) handle
5. In OOP, what is encapsulation?
a) Using many classes
b) Hiding internal data using access control
c) Inheriting attributes
d) Redefining methods
6. Which keyword is used for inheritance in Python?
a) inherits
b) extends
c) super
d) Class name in parentheses
7. What does super() do?
a) Calls a static method
b) Calls the parent class constructor
c) Deletes the parent class
d) Creates an object
8. Create a NumPy array of zeros with shape (2,3):
a) [Link](2,3)
b) [Link]((2,3))
c) [Link](2,3)
d) zeros(2,3)
9. What is the output of the following code?
import numpy as np
a = [Link]([1, 2, 3, 4])
print(a[1:3])
a) [1 2]
b) [2 3]
c) [3 4]
d) Error
10.Which NumPy function changes the shape of an array?
a) resize()
b) reshape()
c) shape()
d) change()
11.Which Pandas function shows the first 5 rows of a DataFrame?
a) [Link]()
b) [Link]()
c) [Link]()
d) [Link]()
12.Replace missing values with the mean:
a) [Link](mean)
b) [Link]([Link]())
c) [Link]()
d) [Link]()
13.Remove a column named "Age" from DataFrame df:
a) [Link]("Age")
b) [Link]("Age")
c) [Link](columns=["Age"])
d) del df"Age"
14.Sort DataFrame by column "Salary" in descending order:
a) [Link]("Salary")
b) df.sort_values("Salary", ascending=False)
c) [Link]("Salary")
d) [Link](descending)
15.Which Matplotlib function displays the plot?
a) [Link]()
b) [Link]()
c) [Link]()
d) [Link]()
16.Which parameter sets color transparency in Matplotlib?
a) opacity
b) alpha
c) transparent
d) shade
17.Which plot is best for comparing categories?
a) Line plot
b) Histogram
c) Bar chart
d) Scatter plot
Exercise II: Object-Oriented Programming (4 pts)
1. Create a class Student with the following attributes:
● name
● grade
2. Add to the class:
● A constructor
● A method __str__() that returns:
Student: name - Grade: grade
3. Create an object of the class and display its information.
Exercise III: Pandas & Matplotlib (4 pts)
Given the following DataFrame:
Name Dept Salary
Ali IT 12000
Sara HR 15000
Omar IT 18000
Lina Financ 20000
e
Write Python code to:
1. Create the DataFrame
2. Display the average salary by department
3. Sort the salaries in ascending order
4. Plot a bar chart of the average salary by department:
○ Add a title
○ Add labels to both axes