Python MCQ Quiz - Objective Question with Answer for Python - Download Free PDF
Last updated on Feb 10, 2023
Latest Python MCQ Objective Questions
Python Question 1:
find the output of the program
a = 1
while True:
if a % 9 = = 0:
break
print(a)
a += 1
Answer (Detailed Solution Below)
Python Question 1 Detailed Solution
Correct answer is option BImportant Points
- program start from 1
- check condition if a % 9 = = 0:
- then run and print all the value of a till a=8
- when a=9 then condition is false
- then program Break
- So the answer is 1 2 3 4 5 6 7 8
Python Question 2:
Write the output of the following:
>>> import pandas as pd
>>> series1=pd.Series([11,12,13])
>>> print(series1)
Answer (Detailed Solution Below)
0 11
1 12
2 13
dtype:int64
Python Question 2 Detailed Solution
In this code we first imports the pandas library and uses it to create a pandas series object called 'series1'. A pandas series is a one-dimensional labelled array that can hold any data type.
The pd.Series() function creates a new series and takes an iterable (such as a list, tuple, or ndarray) as the argument. In this case, the iterable is the list [11,12,13].
The print() function is then used to print the contents of the series to the console.
Output of above code in python is:
0 11
1 12
2 13
dtype:int64
In the output left column shows the index of each element in the series, and the right column shows the value of each element.
Python Question 3:
What kind of data structures are used for manipulating data in Pandas ?
Answer (Detailed Solution Below)
Python Question 3 Detailed Solution
The correct answer is option 3.
Concept:
Pandas is an open-source library designed to make it simple and natural to deal with relational or labelled data. It comes with a number of data structures and methods for manipulating numerical data and time series.
Pandas typically provide two data structures for data manipulation:
Series:
The Pandas Series is a one-dimensional label
led array that may contain any form of data (integer, string, float, python objects, etc.).DataFrame:
Pandas DataFrame is a possibly heterogeneous two-dimensional size-mutable tabular data format with labelled axes (rows and columns). A data frame is a two-dimensional data structure in which data is organised in rows and columns in a tabular format.
Hence the correct answer is Series and DataFrame.
Python Question 4:
Which of the following plots are avilable in Matplotlib?
Answer (Detailed Solution Below)
Python Question 4 Detailed Solution
The correct answer is option 4.
Concept:
Matplotlib:
One of the most widely-used Python libraries for data visualization is Matplotlib. It's a cross-platform library that generates 2D charts from array data. To get started, we just need to import the relevant files, prepare some data, then use the plot() method to begin charting.
Matplotlib is a Python library that uses NumPy, Python's numerical mathematics extension. There are various storylines in it, like as,
- Line Graph
- Bar Graph
- Scatterplot
- Histogram and many more.
Line Graph:
Bar Graph:
Scatterplot:
Histogram:
Hence the correct answer is All the above.
Python Question 5:
Which of the following data structure is supported by pandas library?
Answer (Detailed Solution Below)
Python Question 5 Detailed Solution
Concept:
Pandas is an open source data analysis library suited for various kind of data. It is a high- level data manipulation tool and is built on Numpy package.
Explanation:
There are three data structure supported by pandas library:
Series: It is a one – dimensional data structure.
Data-frame: It is the two – dimensional data structure.
Panel: It is a three-dimensional data structure and includes items major_axis and minor_axis.Top Python MCQ Objective Questions
Python Question 6:
What does this program print?
days = “Mon, Tue, Wed, Thu, Fri, Sat, Sun”
print(days[::5])Answer (Detailed Solution Below)
Python Question 6 Detailed Solution
Concept:
Slicing: It is used to slice a particular sequence. We can specify where it starts and where it will end, how many characters can be skipped.
Explanation:
Here, given code is:
days = “Mon, Tue, Wed, Thu, Fri, Sat, Sun”
print(days[::5])
Output: In this, slice starts at the first character and includes every fifth character which is : MTWTFSS
It also count space as a character.
Important Point:
“Mon, Tue” in this 4th character is spacePython Question 7:
Which one is NOT a feature of Python language?
Answer (Detailed Solution Below)
Python Question 7 Detailed Solution
Python is a dynamic, high-level, free open source, and interpreted programming language. It supports object-oriented programming as well as procedural-oriented programming.
Feature of Python language
- Easy to code
- Free and Open Source
- Object-Oriented Language
- GUI Programming Support
- High-Level Language
- Extensible feature
- Python is a Portable language
- Python is an Integrated language
- Interpreted Language
- Large Standard Library
- Dynamically Typed Language
- It case-sensitive programming language.
Hence the correct answer is Case insensitive.
Python Question 8:
What will be the result of the below command in python language:
round(0.4) - round(-0.5)
Answer (Detailed Solution Below)
Python Question 8 Detailed Solution
In python language,
the system rounds off the number away from 0 when the number which has to be rounded off is halfway through.
This means,
round(0.4) = 0 and round(-0.5) = 0
This gives, 0-(-0) = 0
Hence the answer is 0.
Python Question 9:
What is the output of the below program in python:
print(0.2 + 0.4 == 0.6)
Answer (Detailed Solution Below)
Python Question 9 Detailed Solution
0.2, 0.4 and 0.6 can not be represented accurately in binary.
The round off errors from 0.2 and 0.4 adds up
Hence there is a difference between (0.2 + 0.4) and 0.6.
This is because you can not compare floating point value, as it cannot be considered precise.
Note:- If you run above program then we will get the output false.
Python Question 10:
What will be output of the following command in python?
print (r"\nhello")
Answer (Detailed Solution Below)
Python Question 10 Detailed Solution
In python language,
when 'r' or 'R' is used before the string, it converts the string into a raw string and the escape sequence like \n are not converted.
Hence the answer is \nhello.
Python Question 11:
Which of following is keyword used in python?
Answer (Detailed Solution Below)
Python Question 11 Detailed Solution
The correct answer is option 4.
Concept:
In Python, reserved words are referred to as keywords. A keyword cannot be used as a variable name, function name, or other identifiers.
Here's a list of all keywords in Python Programming:
Python Question 12:
What is the output of the code in Python Language:
for num in range (3):
if num > 0:
print (num*100)
Answer (Detailed Solution Below)
Python Question 12 Detailed Solution
for num in range (3):
if num > 0:
print (num*100)
Here num has initially 0 and it prints num × 100 gives 0 as output and next iteration num is 1 and prints 100. After num has 2 and prints 200. And finally range become closes.
Hence the correct answer is 100,200.
Note:- for better understanding run the above program.
Python Question 13:
The given declaration statement in Python Language belongs to which data type?
>>>Student = {‘SUBJECT’ ∶ ‘Compiler’, ‘Marks’ ∶ ‘50’, ‘Grade’ ∶ ‘C’}
Answer (Detailed Solution Below)
Python Question 13 Detailed Solution
- Python Dictionary is used to store the data in a key-value pair format. The dictionary is the data type in Python, which can simulate the real-life data arrangement where some specific value exists for some particular key. Ex: Dict = {"Name": "Tom", "age": 22}
- A Python set is the collection of unordered items. Each element in the set must be unique, immutable, and the sets remove the duplicate elements. Ex: Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
- In Python programming, sequences are a generic term for an ordered set which means that the order in which we input the items will be the same when we access them.
- Python has basically three numeric data types named int, float, and complex. Since Python is a dynamically typed language we don’t have to define the data type
Hence the correct answer is Dictionary.
Python Question 14:
Which of the following data structure is supported by pandas library?
Answer (Detailed Solution Below)
Python Question 14 Detailed Solution
Concept:
Pandas is an open source data analysis library suited for various kind of data. It is a high- level data manipulation tool and is built on Numpy package.
Explanation:
There are three data structure supported by pandas library:
Series: It is a one – dimensional data structure.
Data-frame: It is the two – dimensional data structure.
Panel: It is a three-dimensional data structure and includes items major_axis and minor_axis.Python Question 15:
What will be the output of the following code snippet?
a = 3
b = 1
print(a, b)
a, b = b, a
print(a, b)
Answer (Detailed Solution Below)
Python Question 15 Detailed Solution
The correct answer is option 1.
Concept:
The given python snippet is,
a = 3
b = 1
print(a, b)
a, b = b, a
print(a, b)
Here "a" and "b" values are assigned.
a=3
b=1
The print function prints 3 and 1.
a, b = b, a
It is evaluated by pushing both the values to the stack and the top two values will be rotated (so that the values will be swapped) and the values are assigned back to "a" and "b".
So it swapped values become,
a=1
b=3
The print function prints 1 and 3.
Hence the correct answer is 3 1 1 3.