Skip to content

Module 4 Graded Quiz: Working with Data in Python :Python for Data Science, AI & Development (IBM Data Analyst Professional Certificate) Answers 2025

1. Question 1

Outcome of:

a = np.array([-1,1])
b = np.array([1,1])
np.dot(a,b)
  • ❌ array([0,2])

  • ❌ 1

  • ❌ array([[-1,-1],[1,1]])

  • ✅ 0

Explanation:
Dot product = (-1×1) + (1×1) = -1 + 1 = 0.


2. Question 2

First step before matrix multiplication?

  • ✅ The number of columns in A must equal the number of rows in B.

  • ❌ Both arrays must be square

  • ❌ Sum of dimensions must be even

  • ❌ Arrays must have same shape

Explanation:
Matrix multiplication rule: A (m×n) × B (n×p).


3. Question 3

Output of X.ndim for:

X = np.array([[1,0,1],[2,2,2]])
  • ❌ 6

  • ❌ 3

  • ✅ 2

  • ❌ 5

Explanation:
X is a 2-dimensional array (rows & columns).


4. Question 4

What happens when multiplying a NumPy array by a scalar?

  • ❌ Only first row multiplied

  • ❌ Scalar becomes a dimension

  • ❌ Only diagonal changes

  • ✅ Each element is multiplied by the scalar value

Explanation:
Scalar multiplication is element-wise.


5. Question 5

Output of:

with open("Example1.txt","r"as file1:
FileContent = file1.readline()
print(FileContent)
  • ❌ Empty output

  • ✅ This is line 1

  • ❌ Full file

  • ❌ “This”

Explanation:
.readline() reads only the first line.


6. Question 6

Advantage of using a with statement?

  • ✅ It automatically closes the file when the block is exited.

  • ❌ Improves reading speed

  • ❌ Enables writing to any file

  • ❌ Makes file read-only

Explanation:
with ensures safe file handling using context managers.


7. Question 7

What happens with "w" mode on an existing file?

  • ✅ Erases old contents and replaces them with new content

  • ❌ Raises an error

  • ❌ File unchanged

  • ❌ Appends to file

Explanation:
w = overwrite mode.


8. Question 8

Difference between "w" and "a" modes?

  • ❌ binary vs text

  • ❌ w faster

  • ✅ “w” overwrites; “a” appends

  • ❌ w creates new files only

Explanation:

  • w = overwrite

  • a = append


9. Question 9

Difference between .loc and .iloc in pandas?

  • ❌ loc = rows, iloc = columns

  • ❌ loc faster

  • ❌ loc only single cells

  • ✅ loc uses labels, iloc uses integer positions

Explanation:

  • df.loc["row_label"]

  • df.iloc[0]


10. Question 10

How to create a DataFrame from a dictionary?

  • ❌ pd.dict_to_df

  • ✅ pd.DataFrame(dictionary)

  • ❌ pd.from_dict

  • ❌ pd.create_dataframe

Explanation:
The standard constructor is pd.DataFrame().


🧾 Summary Table

Q# Correct Answer Key Concept
1 0 Dot product
2 Cols(A)=Rows(B) Matrix multiplication rule
3 2 ndim returns number of dimensions
4 Element-wise multiply Scalar × array
5 “This is line 1” readline() reads first line
6 Auto-close file with statement
7 Overwrites file Mode “w”
8 w = overwrite, a = append File modes
9 loc = labels, iloc = integers Pandas indexing
10 pd.DataFrame(dict) Create DataFrame