Python Libraries MCQ Quiz - Objective Question with Answer for Python Libraries - Download Free PDF
Last updated on Mar 13, 2025
Latest Python Libraries MCQ Objective Questions
Python Libraries Question 1:
Which of the following best describes the behavior of the head() and tail() functions in Pandas?
Answer (Detailed Solution Below)
Python Libraries Question 1 Detailed Solution
The correct answer is Option 1.
Key Points
-
The head() and tail() functions are used in the Pandas library to access specific portions of a DataFrame.
head() returns the first five rows of the DataFrame by default. You can specify the number of rows to return by passing an integer parameter.
tail() returns the last five rows of the DataFrame by default. Similar to head(), you can specify a different number of rows by passing an integer parameter.
These functions are useful for quickly inspecting the beginning or end of a DataFrame.
For example, df.head(3) would return the first three rows, while df.tail(3) would return the last three rows.
Additional Information These functions are particularly helpful in large datasets where you want to verify the structure and contents without loading the entire dataset into memory.
They are often used in data exploration and data cleaning stages of data analysis.
Here is a source code example demonstrating the use of head() and tail() functions:import pandas as pd
# Creating a sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
'Age': [24, 27, 22, 32, 29],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
}df = pd.DataFrame(data)
# Displaying the first five rows
print("First five rows using head():")
print(df.head())# Displaying the last five rows
print("Last five rows using tail():")
print(df.tail())# Displaying the first three rows using head()
print("First three rows using head(3):")
print(df.head(3))# Displaying the last three rows using tail(3)
print("Last three rows using tail(3):")
print(df.tail(3))
Python Libraries Question 2:
Which of the following statements about Pandas DataFrame operations is correct?
Answer (Detailed Solution Below)
Python Libraries Question 2 Detailed Solution
The correct answer is Option 1.
Key Points
- The drop() method in Pandas is used to remove rows or columns from a DataFrame.
- By default, the drop() method does not modify the original DataFrame. Instead, it returns a new DataFrame with the specified rows or columns removed.
- To permanently remove rows or columns from the original DataFrame, you need to either use the inplace=True parameter or explicitly assign the returned DataFrame back to the original DataFrame.
- Example of using drop() method:
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8],
'C': [9, 10, 11, 12]
})
# Dropping column 'B' and assigning it back to the original DataFrame
df = df.drop('B', axis=1)
print(df)
Additional Information
- The rename() method can rename both column labels and row labels (index).
- The append() method returns a new DataFrame and does not modify the original DataFrame.
- The fillna() method is used to fill NaN values with a specified value, and it does not delete NaN values.
Python Libraries Question 3:
Which of the following best describes the difference between a Pandas Series and a NumPy ndarray?
Answer (Detailed Solution Below)
Python Libraries Question 3 Detailed Solution
The correct answer is A Pandas Series allows labeled indexing, whereas a NumPy ndarray only supports integer-based indexing..
Key Points
- A Pandas Series is a one-dimensional array-like object capable of holding any data type (integers, strings, floating point numbers, etc.). It has a labeled index, which means that each element in the Series can be accessed via labels as well as integer positions.
- The labeled indexing feature allows for more intuitive and flexible data manipulation, especially when working with time series data or other data that naturally carries labels.
- For example, a Pandas Series can be indexed by dates, names, or any other custom labels defined by the user.
- Here is a code example demonstrating labeled indexing in a Pandas Series:
import pandas as pd
# Creating a Pandas Series with labeled indexing
data = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
# Accessing elements via labels
print(data['a']) # Output: 1
# Accessing elements via integer positions
print(data[0]) # Output: 1
- In contrast, a NumPy ndarray is a multi-dimensional, homogeneous array of fixed-size items. It supports integer-based indexing but does not have a built-in mechanism for labeled indexing.
- NumPy arrays are efficient and fast for numerical computations but lack the flexibility of labeled indexing provided by Pandas Series.
- Here is a code example demonstrating integer-based indexing in a NumPy ndarray:
import numpy as np
# Creating a NumPy ndarray
data = np.array([1, 2, 3])
# Accessing elements via integer positions
print(data[0]) # Output: 1
Additional Information
- Pandas Series is built on top of NumPy, so it inherits many of NumPy's functionalities and optimizations.
- While NumPy arrays are ideal for numerical operations, Pandas Series is more suited for data analysis tasks where labeled data is beneficial.
- Both Pandas and NumPy are essential tools in the data science ecosystem, and understanding their differences can help in choosing the right tool for the task at hand.
Python Libraries Question 4:
What will happen if we perform an arithmetic operation between two Pandas Series with different indices?
Answer (Detailed Solution Below)
Python Libraries Question 4 Detailed Solution
The correct answer is Option 2.
Key Points
- When performing an arithmetic operation between two Pandas Series with different indices, the operation will align the Series based on their indices.
- If an index is present in one Series but not the other, the result will have a NaN (Not a Number) value for that index.
- This behavior ensures that the Series are combined correctly, even if they do not have matching indices.
- Example:
Consider two Series: Series A with indices [1, 2, 3] and Series B with indices [2, 3, 4].
Performing an addition operation will result in a new Series with indices [1, 2, 3, 4].
The values for index 1 and 4 will be NaN since they are not present in both Series.
Additional Information
- This alignment feature in Pandas helps in handling data that may come from different sources and might not be perfectly aligned.
- NaN values can be handled or filled using various Pandas methods like fillna() or dropna() depending on the use case.
- It is important to understand this behavior to avoid unexpected results in data analysis.
Python Libraries Question 5:
Which of the following statements about NumPy and Pandas is incorrect?
Answer (Detailed Solution Below)
Python Libraries Question 5 Detailed Solution
The correct answer is NumPy is primarily used for handling structured tabular data..
Key Points
- NumPy is primarily used for numerical computing and handling arrays and matrices, not specifically for structured tabular data.
- NumPy provides support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
- It is optimized for performance and can handle large datasets efficiently.
- Pandas is the library that is primarily used for handling structured tabular data.
- Pandas DataFrame is designed to handle heterogeneous data, making it ideal for data manipulation and analysis.
- It provides tools for reading and writing data, handling missing data, and merging or joining datasets.
- NumPy is a fundamental package for scientific computing in Python, and Pandas is built on top of NumPy to provide additional functionalities for data analysis.
Additional Information
- While NumPy requires data to be homogeneous, Pandas can handle heterogeneous data, making it more flexible for various data types.
- Pandas also provides built-in support for data visualization, whereas NumPy does not have such capabilities.
- NumPy and Pandas together form a powerful toolkit for data science and analytical workflows in Python.
- Understanding the strengths of each library can help in selecting the right tool for specific data processing tasks.
Top Python Libraries MCQ Objective Questions
Python Libraries Question 6:
In pandas python, Process of changing the structure of the DataFrame is known as_____________
Answer (Detailed Solution Below)
Python Libraries Question 6 Detailed Solution
The correct answer is option 3.
Concept:
Reshaping:
The process of changing the structure of the DataFrame is known as Reshaping. The form of data refers to how a dataset is organized into rows and columns. The process of modifying the form of a dataset to make it acceptable for particular analytical issues is referred to as reshaping data.
For reshaping data, two basic functions are available in Pandas, pivot, and pivot_table.
Hence the correct answer is Reshaping.
Additional Information
Importing:
Importing data from MySQL to Panda refers to the process of fetching data from a MySQL table or database to a pandas DataFrame.
Exporting:
Exporting data from Pandas to MySQL refers to the process of storing data from a pandas DataFrame to a MySQL table or database.
Python Libraries Question 7:
Which of the following functions can be aggregated?
Answer (Detailed Solution Below)
Python Libraries Question 7 Detailed Solution
The correct option is All of the above
CONCEPT:
Aggregation means transforming the dataset and producing a single numeric value from an array that can be applied to one or more columns together.
Aggregate functions are max(),min(), sum(), count(), std(), var()...etc.
DataFrame.aggregate() method is used to apply a function or a list of function names to be executed across one or more columns.
All of the function pairs stated in the above question can be aggregated together
>>>DataFrame.aggregate(['max','min','sum','count','mean','std','var',])
Python Libraries Question 8:
Which of the following is a correct syntax for panda's dataframe.
1) Pandas.DataFrame(data, index, dtype, copy)
2) pandas.DataFrame( data, index, columns, dtype, copy)
3) pandas.DataFrame(data, index, dtype, copy)
4) pandas.DataFrame( data, index, rows, dtype, copy)
Answer (Detailed Solution Below)
Python Libraries Question 8 Detailed Solution
Correct answer is 4
A syntax of pandas.DataFrame( data, index, columns, dtype, copy).
- data - data can be represented in a variety of ways, including ndarray, series, map, lists, dict, constants, and another DataFrame.
- index - for the row labels, the index to be used for the resulting frame is optional default index. If no index is provided, np.arange(n) is used.
- columns - the optional default syntax for column labels is np.arange, which stands for numeric range (n). When there is no index passed, the following is true.
- dtype - dtype identifies the data type of each column.
- copy - this command is used for data copying if the default value is False, else it is not used.
Python Libraries Question 9:
What is the expected output for the given code.
Answer (Detailed Solution Below)
B 54
C 20
D 17
Python Libraries Question 9 Detailed Solution
The correct answer is option 1.
Concept:
Pandas dataframe.max():
Pandas dataframe. max() returns the highest value in the supplied object. If the input is a series, the method will return a scalar containing the maximum of the series' values.
The given code is,
Statement 1: Here importing pandas as pd
import pandas as pd
Statement 2: For Creating the dataframe
df = pd.DataFrame({"A":[12, 4, 5, 44, 1],
"B":[5, 2, 54, 3, 2],
"C":[20, 16, 7, 3, 8],
"D":[14, 3, 17, 2, 6]})
The given data frame is like,
A | B | C | D | |
0 | 12 | 5 | 20 | 14 |
1 | 4 | 2 | 16 | 3 |
2 | 5 | 54 | 7 | 17 |
3 | 44 | 3 | 3 | 2 |
4 | 1 | 2 | 8 | 6 |
Statement 3: Even if we do not specify axis = 0, the method will return the max over the index axis by default.
df.max(axis = 0)
The output is like,
A 44
B 54
C 20
D 17
It will return the max over the index axis by default.
Hence the correct answer is
A 44
B 54
C 20
D 17
Python Libraries Question 10:
In Python Dictionary, which of the following is unique?
Answer (Detailed Solution Below)
Python Libraries Question 10 Detailed Solution
Dictionary: Python dictionaries are a collection of key-value pairs where each key is mapped to a specific value.
- In Python dictionaries, key is unique and not the key-value pair. Value can also be duplicate.
For example, consider the following python statement:
temp_dict = {1: 'a', 2: 'b', 3: 'c', 1: 'd', 4: 'b'}
print(temp_dict)
The above python code will print: {1: 'd', 2: 'b', 3: 'c', 4: 'b'}
- Value is not unique otherwise keys (2, 4) would have thrown error.
- Key-Value pair is not unique otherwise (1: 'a') and (1: 'd') both would have been there.
- Since Key should be unique, (1, 'a') is replaced by (1: 'd').
Python Libraries Question 11:
What is not true about python
Answer (Detailed Solution Below)
Python Libraries Question 11 Detailed Solution
Advantages of Python are:
- Python is an interpreted language. It does not require prior compilation of code and executes instructions directly.
- It is an open-source project which is publicly available to reuse. It can be downloaded free of cost.
- It is very flexible and extensible with any module.
- Itallows to implement the Object-Oriented concepts to build application solution.It has built-in data structure such as Tuple, List, and Dictionary
- It is High-Level Language
- Python programs can run on cross platforms without affecting its performance.
Python Libraries Question 12:
Which of the following does not allow slicing?
Answer (Detailed Solution Below)
Python Libraries Question 12 Detailed Solution
Slicing - In Python, it is a method where we access a range of elements. Just like while accessing an array, we use the index of the required element inside square brackets, similarly, when we need a range of elements from a List, Tuple or String, we give the index range inside square brackets.
For example, let there be a list, a = [1,2,3,4,5,6,7] and we want to access its 2nd, 3rd, 4th and 5th element, so we will slice the list a and print it - print(a[1:5])
The above print statement with give the output as: [2, 3, 4, 5]
- Slicing works on List, Tuple and Strings but not on Sets.
Python Libraries Question 13:
What will be the output of the following code?
import pandas as pd
s1 = pd.Series([2, 4, 7, 1, 3], index = ['a', 'b', 'c', 'd', 'e'])
s2 = pd.Series([1, 2, 3, 4, 5], index = ['f', 'b', 'g', 'd', 'c'])
s3 = pd.Series(s1 + s2)
print(s3.count())
Answer (Detailed Solution Below)
Python Libraries Question 13 Detailed Solution
Correct answer: Option 3
Explanation:
- The count() function only counts numerical values.
- When two series are added with the + symbol, only the index values present in both series are added. For the others, a NaN value is returned.
Important Points:
The internal working of the + symbol is given below:
Index | Values from s1 | Values from s2 | s1 + s2 |
a | 2 | NaN | |
b | 4 | 2 | 6 |
c | 7 | 5 | 12 |
d | 1 | 4 | 5 |
e | 3 | NaN | |
f | 1 | NaN | |
g | 3 | NaN |
Python Libraries Question 14:
What libraries are mandatory for facilitating import and export of data between Pandas and MySQL?
Answer (Detailed Solution Below)
Python Libraries Question 14 Detailed Solution
The correct answer is option 3.
Concept:
Pymysql and sqlalchemy are two mandatory libraries for facilitating the import and export of data between Pandas and MySQL. Before import and export, a connection needs to be established from the python script to the MySQL database.
Pymysql and sqlalchemy:
- sqlalchemy is a library used to interact with the MySQL database by providing the required credentials.
- PyMySQL is an interface for connecting to a MySQL database server from Python.
These libraries can be installed using the following command:
pip install sqlalchemy
pip install pymysql
- Importing data from MySQL to Panda refers to the process of fetching data from a MySQL table or database to a pandas DataFrame.
- Exporting data from Pandas to MySQL refers to the process of storing data from a pandas DataFrame to a MySQL table or database.
Hence the correct answer is option 1 and option 2.
Python Libraries Question 15:
NumPy stands for in Python?
Answer (Detailed Solution Below)
Python Libraries Question 15 Detailed Solution
The correct answer is option 1.
Concept:
NumPy:
NumPy stands for Numerical Python and is a Python package for scientific computing. It provides an efficient multi-dimensional array of objects as well as a variety of functions for working with them.
A NumPy array is an array of values that is homogenous. Axes are the dimensions of an array in NumPy. The rank is the number of axes. The form of an array is a tuple of non-negative integers that tells how big it is in each dimension.
Hence the correct answer is Numerical Python.