WorksheetsPython Programming Quiz
Total questions: 31
Worksheet time: 16mins
What is the output of the following code? t = (1, 2, 3) + (4, 5) print(t)
(1, 2, 3, 4, 5)
(5, 4, 3, 2, 1)
[1, 2, 3, 4, 5]
(1, 2, 3), (4, 5)
What method is used to remove a specific value from a list?
remove()
pop()
delete()
del
The ______ function returns a new sorted list from any sequence.
sorted
sort
sorted_list
order
The update method is used to merge one dictionary into another.
True
False
Which method is used to check if a dictionary contains a specific key?
in
contains()
has_key()
key_check()
The pandas library provides high-level data structures for working with structured and tabular data.
True
False
Which of the following is a mutable object in Python?
String
Tuple
List
None
What will be the output of this code? x = 10; y = 3; print(x // y)
3.33
3
3.0
4
Output of this code: nums = [1,2,3]; print(sum(nums))
6
5
3
Error
What does the following code print? a = [1,2,3]; print(a[::-1])
[3,2,1]
[1,2,3]
[1,3,2]
Error
What is the result? import numpy as np a = np.zeros((2,3))
Array of zeros with shape (2,3)
Array of ones
Empty array
Error
What does the following code return? lst = [1,2,3,4] print(lst.index(3))
2
3
1
Error
Which of the following is not a valid input for creating a DataFrame?
A single integer
A dictionary of lists
A NumPy array
A list of dictionaries
What does this code print? import numpy as np a = np.array([[1, 2], [3, 4]]) print(a.shape)
(2, 2)
(2,)
(4,)
Error
Output of the following code? import numpy as np a = np.array([1,2,3]) print(a.dtype)
int64
float64
object
Error
What is the result? import numpy as np a = np.zeros((2,3))
Array of zeros with shape (2,3)
Array of ones
Empty array
Error
Output of this code? import numpy as np a = np.array([1,2,3,4]) print(np.where(a>2))
(array([2,3]),)
(array([0,1]),)
[3 4]
Error
Output of this code: d = {"a":1, "b":2} print(d.get("c", 0))
0
None
Error
The with statement is used to handle ______ safely and ensure they are closed properly.
files
loops
functions
variables
What is the output of the following code? import numpy as np a = np.array([1, 2, 3]) print(a[1])
2
1
3
Error
What does this print? import numpy as np print(np.unique([1,2,2,3,3,4]))
[1 2 3 4]
[1 2 2 3 3 4]
[2 3 4]
Error
What is a key difference between a pandas Series and a DataFrame?
A Series is one-dimensional, while a DataFrame is two-dimensional.
A Series can contain multiple data types, while a DataFrame cannot.
A DataFrame does not have indexes, while a Series always does.
Both are always empty by default.
Which method allows you to select data from a DataFrame by row and column labels?
loc
iloc
index
slice
What will the following code output? import pandas as pd data = {"A": [1, 2], "B": [3, 4]} df = pd.DataFrame(data) print(df.iloc[1])
A 2 B 4 (Series)
A 1 B 3
[1, 3]
Error
What will the following code output? import pandas as pd s = pd.Series([1, 2, 3], index=["a", "b", "c"]) print(s["b"])
2
1
“b”
Error
What does this code return? import pandas as pd df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6]}) print(df[df['A']>1])
Rows with A=2 and A=3
Row with A=1
[2,3]
Error
Output of this code? import pandas as pd df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6]}) print(df.iloc[1,1])
5
2
4
Error
What does the following code produce? import pandas as pd df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6]}) print(df.loc[0,'B'])
4
1
0
Error
Which library is typically used to read and parse Excel files in pandas?
openpyxl
pickle
lxml
json
Which pandas method writes a DataFrame to pickle format?
to_pickle()
write_pickle()
to_binary()
pickle_dump()
What does this code output? import pandas as pd df = pd.read_csv('data.csv', thousands=',')
Converts numbers like ‘1,000’ to 1000
Treats ‘,’ as delimiter
Reads all values as strings
Error
