Wednesday 29 November 2023

Python Pandas Series MCQs

 

Python Programming MCQs

Pandas Series


10 important MCQs related to Python Pandas Series, along with their answers:


1. What is a Pandas Series?

(A) One-dimensional labelled array 
(B) Two-dimensional labelled array 
(C) Three-dimensional labelled array 
(D) None of the above

2. How do you access a value from a Pandas Series?

(A) series[index] 
(B) series.index[value] 
(C) series.values[index] 
(D) series.data[index]

3. How do you sort a Pandas Series?

(A) series.index() 
(B) series.sort_values() 
(C) series.ascending() 
(D) series.descending()

4. How do you create a Pandas Series?

(A) import pandas as pd
    s = pd.Series([1, 2, 3, 4, 5])
(B) import pandas as pd
    s = pd.Series(data=[1, 2, 3, 4, 5])
(C) import pandas as pd
    s = pd.Series(index=['a', 'b', 'c', 'd', 'e'], data=[1, 2, 3, 4, 5])
(D) All of the above

5. Which of the following is a valid way to create a slice from Pandas Series "s"?

(A) s[:3]
(B) s[2:]
(C) s[1:4]
(D) All of the above

6. How can you find the sum of a Pandas Series (assuming all elements are numbers) ?

(A) series.sum()
(B) series.mean()
(C) series.std()
(D) None of the above.

7. How can you plot a Pandas Series as a line graph?

(A) series.plot(kind=’pie’)
(B) series.plot(kind='line')
(C) series.plot(kind='bar')
(D) None of the above.

8. Predict output of following python pandas code:

import pandas as pd
series1 = pd.Series([1, 2, 3, 5, 8, 10])
print(series1[1]+series1[3])

(A) 7
(B) 4
(C) 5
(D) 13

9. How can you check if a Pandas Series has any NaN values?

(A) Check the Series' dtype.
(B) Use the isna() method.
(C) Use the notnull() method.
(D) Check the Series' length.

10. Predict output of following python code:

import pandas as pd
series1 = pd.Series([3, 2, 4])
print(series1-1)

(A) Syntax error
(B) 1
(C) -2
(D) 

0 2
1 1
2 3
dtype: int64



ANSWERS: Python Pandas Series MCQs:

1.  (A) One-dimensional labelled array

2.  (A) series[index]

3.  (B) series.sort_values()

4.  (D) All of the above

5.  (D) All of the above

6.  (A) series.sum()

7.  (B) series.plot(kind='line')

8.  (A) 7

9.  (B) Use the isna() method

10. (D)

0    2

1    1

2    3

dtype: int64

 

Back to Python MCQs >

Monday 27 November 2023

Python Pandas MCQs Set 1

 

MCQs based on Python Pandas


10 important MCQs related to Python Pandas, along with their answers:


1. What is the purpose of the pandas library?

(a) To perform data analysis and manipulation

(b) To create web applications

(c) To handle machine learning tasks

(d) To visualize data


2. What is the difference between a DataFrame and a Series in Pandas?

(a) A DataFrame is a two-dimensional data structure, while a Series is a one-dimensional data structure.

(b) A DataFrame can store data of different types, while a Series can only store data of a single type.

(c) A DataFrame is like an Excel worksheet, while a Series is like a column of an Excel worksheet.

(d) All of the above


3. What is the output of the following code?

import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [32, 25, 27]}
df = pd.DataFrame(data)

print(df['Age'].max())


(a) 27

(b) 25

(c) 32

(d) None of these


4. Which of the following is not a built-in data structure in Pandas?

(a) Series

(b) DataFrame

(c) Panel

(d) Numpy ndarray


5. What is the purpose of the head() function in Pandas?

(a) To print the first few rows of a DataFrame

(b) To sort a DataFrame by a specific column

(c) To calculate the mean of a DataFrame

(d) To create a new DataFrame from an existing DataFrame


6. What is the purpose of the tail() function in Pandas?

(a) To print the first few rows of a DataFrame

(b) To sort a DataFrame by a specific column

(c) To calculate the mean of a DataFrame

(d) To create a new DataFrame from an existing DataFrame


7. What is the purpose of the dropna() function in Pandas?

(a) To print the first few rows of a DataFrame

(b) To sort a DataFrame by a specific column

(c) To calculate summary statistics for a DataFrame

(d) To remove rows from a DataFrame that contain missing values


8. What is the purpose of the fillna() function in Pandas?

(a) To print the first few rows of a DataFrame

(b) To sort a DataFrame by a specific column

(c) To calculate summary statistics for a DataFrame

(d) To replace missing values in a DataFrame


9. What is the output of the following python pandas code?

import pandas as pd

data = {'Name': ['Sunit', 'Bob', 'Amit'], 'Age': [32, 25, 27]}
df = pd.DataFrame(data)

print(df['Age'].mean())


(a) 32

(b) 28.0

(c) 25

(d) 27


10. How do you access a specific column in a pandas DataFrame?

(a) df[column_name]

(b) df.column_name

(c) df.get_column(column_name)

(d) df['column_name']



Python Pandas MCQs Answers:

1. (a) To perform data analysis and manipulation

2. (d) All of the above

3. (c) 32

4. (d) Numpy ndarray

5. (a) To print the first few rows of a DataFrame

6. (d) To print the last few rows of a DataFrame

7. (d) To remove rows from a DataFrame that contain missing values

8. (d) To replace missing values in a DataFrame

9. (b) 28.0

10.(d) df['column_name']

 

Back to Python MCQs >

Python Basics MCQs Set 2

MCQs based on Python Basics (Set 2)


10 important MCQs related to Python basic syntax, along with their answers:


1. Which of the following is the correct syntax for assigning a value to a variable in Python?

(a) my_variable := 10

(b) my_variable = 10

(c) my_variable = "10"

(d) my_variable '10'


2. What is the output of the following Python code?

print("Hello, World!)


(a) Hello, World!

(b) "Hello, World!

(c) Syntax Error

(d) Import Error


3. What is the data type of the variable my_number in the following Python code?

my_number = 10


(a) int

(b) float

(c) string

(d) boolean


4. Which of the following is the correct syntax for an if statement in Python?

(a) if condition:

      body

(b) if condition;

      body

(c) if condition: {

      body }

(d) if condition; {

      body }


5. What is the difference between the + and = operators in Python?

(a) The + operator is used for addition of numbers, while the = operator is used for assignment.

(b) The + operator is used for concatenation of numbers, while the = operator is used for assignment.

(c) The + operator is used for assignment while the = operator is used for addition.

(d) None of these


6. What is the purpose of the print() function in Python?

(a) To print output to the console

(b) To read input from the user

(c) To convert data types

(d) To check if a string is empty


7. What is the purpose of the indentation in Python code?

(a) To make the code more readable

(b) To define code blocks

(c) To prevent errors

(d) All of the above


8. What is the purpose of the for loop in Python?

(a) To prevent errors from occurring

(b) To check if a condition is true

(c) To handle errors in code

(d) To iterate over a sequence of items


9. Which of the following is not a valid Python identifier?

(a) my_variable

(b) 1st_name

(c) while1

(d) _my_variable


10. What is the purpose of the input() function in Python?

(a) To convert data types

(b) To print output to the console

(c) To read input from the user

(d) To check if a string is empty



Answers

1. (b) my_variable = 10

2. (c) Syntax Error

3. (a) int

4. (a) if condition:

         body

5. (a) The + operator is used for addition of numbers, while the = operator is used for assignment. 

6. (a) To print output to the console

7. (b) To define code blocks

8. (d) To iterate over a sequence of items

9. (b) 1st_name

10. (c) To read input from the user


Back to Python MCQs >


Wednesday 22 November 2023

Python String MCQs Set 1

MCQs based on Python String


1. Which of the following methods is used to convert a string to uppercase?

A.   upper()

B.    capitalize()

C.    title()

D.   all()

 

2. Which of the following methods is used to remove whitespace from the beginning and end of a string?

A.   lstrip()

B.    rstrip()

C.    strip()

D.   replace()

 

3. Which of the following methods is used to count the number of occurrences of a substring in a string?

A.   count()

B.    find()

C.    index()

D.   rfind()

 

4. Which of the following methods is used to split a string into a list of words?

A.   split()

B.    splitlines()

C.    partition()

D.   rsplit()

 

5. Which of the following methods is used to format a string with placeholder values?

A.   format()

B.    f-strings

C.    % operator

D.   str.format()

 

6. Which of the following is the correct syntax for replacing all occurrences of a substring within a string with another substring?

A.   string.replace(old, new)

B.    replace(string, old, new)

C.    string.replaceAll(old, new)

D.   string.replaceSubstring(old, new)


7. Which of the following is the correct syntax for splitting a string into a list of words based on spaces?

A.   string.split()

B.    split(string)

C.    string.splitWords()

D.   string.wordSplit()

 


Answers:

1. (A) upper()

2. (C) strip()

3. (A) count()

4. (A) split()

5. (D) str.format()

6. (A) string.replace(old, new)

7. (A) string.split()


Back to Python MCQs >