Programming With Python MCQ Quiz - Objective Question with Answer for Programming With Python - Download Free PDF
Last updated on Jun 24, 2025
Latest Programming With Python MCQ Objective Questions
Programming With Python Question 1:
The variable declared inside the function is called a variable
Answer (Detailed Solution Below)
Programming With Python Question 1 Detailed Solution
The correct answer is local.
Key Points
- A variable declared inside a function is referred to as a local variable.
- Local variables are only accessible within the function where they are declared; they cannot be accessed outside that function.
- These variables are created when the function starts execution and are destroyed when the function ends.
- Local variables are used to store temporary data that is only needed while the function is running.
- Since they are function-specific, local variables help prevent conflicts with variables of the same name in other parts of the program.
Additional Information
- Global Variables:
- Global variables are declared outside any function and can be accessed and modified from any part of the program.
- They have a global scope, meaning their value persists throughout the program's execution.
- While convenient, excessive use of global variables can lead to unintended side effects, making debugging difficult.
- External Variables:
- External variables are declared using the
extern
keyword, typically in multiple files. - They are used to share data between different files in a program.
- External variables are not initialized within the file they are declared but are initialized elsewhere in the program.
- External variables are declared using the
- Static Variables:
- Static variables retain their value even after the function in which they are declared has completed execution.
- They are initialized only once and have a scope limited to the function or file where they are declared.
- Useful for maintaining state information between function calls.
- Dynamic Variables:
- Dynamic variables are allocated memory at runtime using functions like
malloc()
ornew
in programming languages like C and C++. - They provide flexibility in memory management but require explicit deallocation to prevent memory leaks.
- Dynamic variables are allocated memory at runtime using functions like
Programming With Python Question 2:
Which of the following can be used as valid variable identifier(s) in Python?
Answer (Detailed Solution Below)
Programming With Python Question 2 Detailed Solution
The correct answer is Option 3 (total).
Key Points
- In Python, variable identifiers must begin with a letter (A-Z or a-z) or an underscore (_) and can be followed by letters, digits (0-9), or underscores.
- The identifier total is valid because it follows Python's naming conventions and does not conflict with reserved keywords.
- Reserved keywords such as global cannot be used as variable names in Python as they have predefined meanings in the programming language.
- Other valid identifiers from the options include question and salute, but they are not the correct answer as per the provided question context.
- Python is case-sensitive, meaning that identifiers like Total (with an uppercase 'T') would be treated differently from total.
Additional Information
- Reserved Keywords in Python
- Python has 35 reserved keywords as of Python 3.10, including global, def, return, class, if, else, etc.
- These keywords are case-sensitive and cannot be used as identifiers.
- Rules for Variable Naming in Python
- Variable names can include letters, numbers, and underscores but must not begin with a digit.
- Special characters such as @, $, %, etc., are not allowed in variable names.
- Variable names should not conflict with reserved keywords.
- Case Sensitivity in Python
- Python is case-sensitive, meaning myVariable and MyVariable would be considered different identifiers.
- Best Practices for Variable Naming
- Use meaningful and descriptive names, such as total_price or user_age, to improve code readability.
- Follow snake_case (e.g., my_variable) for variable names as per Python's PEP 8 guidelines.
Programming With Python Question 3:
Which of the following best describes the behavior of the head() and tail() functions in Pandas?
Answer (Detailed Solution Below)
Programming With Python Question 3 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))
Programming With Python Question 4:
Which of the following statements about Pandas DataFrame operations is correct?
Answer (Detailed Solution Below)
Programming With Python Question 4 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.
Programming With Python Question 5:
Which of the following best describes the difference between a Pandas Series and a NumPy ndarray?
Answer (Detailed Solution Below)
Programming With Python Question 5 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.
Top Programming With Python MCQ Objective Questions
In object oriented programming, what is the relation of an object to a class
Answer (Detailed Solution Below)
Programming With Python Question 6 Detailed Solution
Download Solution PDFobject oriented programming: object-oriented programming is an approach to designing modular reusable software systems. The object-oriented approach is an evolution of good design practices that go back to the very beginning of computer programming. object-orientation is simply the logical extension of older techniques such as structured programming and abstract data types.
oops objects
Classes are regarded as types for instance. (option 4 is correct)
Instances can not change their type at runtime.
The list of classes is fully known at compile-time and cannot change after that.
Compilers are used at build-time. Compile-time errors indicate problems.
Classes encode much of their meaning and behavior through imperative functions and methods.
Instances are anonymous insofar that they cannot easily be addressed from outside of an executing program.
Closed world: If there is not enough information to prove a statement true, then it is assumed to be false.
Which of the following declarations is incorrect in python language?
Answer (Detailed Solution Below)
Programming With Python Question 7 Detailed Solution
Download Solution PDFThe correct answer is option 1.
Options are described as:
Option 1: xyzp = 5000 6000 7000 8000
False initialization, Spaces are not allowed in variable names or values. Commas are required between values. The correct declaration is: xyzp = 5000,6000,7000,8000.
Option 2: x_y_z_p = 5,000,000
It is a valid declaration and x_y_z_p prints the (5, 0, 0).
Option 3: x, y, z, p = 5000, 6000, 7000, 8000
It is a valid declaration and x store and prints the 5000, And y tore and prints the 6000, Z Store and prints the7000 and p store and prints the 8000.
Option 4:xyzp = 5,000,000
It is a valid declaration and xyzp prints the (5, 0, 0).
Hence the correct answer is xyzp = 5000 6000 7000 8000.
What is the output of the following code
list1 = ['xyz', 'zara', 'Wscubetech']
print (max(list1))
Answer (Detailed Solution Below)
Programming With Python Question 8 Detailed Solution
Download Solution PDFThe correct answer is:
option 2) zara
Explanation:
The max() function in Python returns the largest item in an iterable or the largest of two or more arguments. When applied to a list of strings, it returns the string that would come last if the list were sorted alphabetically.
In this case, 'xyz', 'zara', and 'Wscubetech' are the elements of the list. 'zara' is considered the largest because the alphabetical comparison is case-sensitive and uppercase letters are considered smaller than lowercase letters in ASCII comparison. So 'Wscubetech' is not the largest even though 'W' comes after 'z' in the alphabet. Hence, 'zara' is the largest item in the list.
What will be the output of the following Python function?
len(["hello", 2, 4, 6])
Answer (Detailed Solution Below)
Programming With Python Question 9 Detailed Solution
Download Solution PDFThe correct answer is 4.
Key Points
- The Python function
len()
is used to determine the number of items in an object. - In this case, the object is a list:
["hello", 2, 4, 6]
. - The list contains four items: a string ("hello"), and three integers (2, 4, 6).
- Therefore,
len(["hello", 2, 4, 6])
returns4
.
Which of the following is not a core data type in Python programming?
Answer (Detailed Solution Below)
Programming With Python Question 10 Detailed Solution
Download Solution PDFThe correct answer is Class.
Key Points
- In Python, core data types are built-in types that are used to store and manipulate data.
- Classes are not considered a core data type in Python. Instead, they are a blueprint for creating user-defined objects.
Additional Information
- Tuples: Tuples are immutable sequences in Python, meaning their elements cannot be changed once assigned. They are used to store collections of items.
- Lists: Lists are mutable sequences, meaning their elements can be changed. They are used to store collections of items and are very versatile.
- Dictionaries: Dictionaries are mutable mappings in Python. They store key-value pairs and provide a way to quickly retrieve values based on their keys.
Which statement is/are true about Exception handling?
i. There can be try block without catch block but vice versa is not possible.
ii. There is no limit on the number of catch, block corresponding to a try block.
iii. The object must be created of a specific class of which the error has occurred otherwise runtime error will occur.
iv. To execute a code with each and every run of program, the code is written in finally block.
Answer (Detailed Solution Below)
Programming With Python Question 11 Detailed Solution
Download Solution PDFThe correct answer is option 1.
Concept:
Statement 1: There can try block without catch block but vice versa is not possible.
True, there may or may not be a catch block in the try block. But without a try block, a catch block cannot exist in a program. Similar to if-block, else-block can only be written if and only if it is present in the program.
Statement 2: There is no limit on the number of catches, or blocks corresponding to a try block.
True, there is no limit on the number of catch blocks corresponding to a try block. This is because the error can be of any type and for each type, a new catch block can be defined. This is to make sure all types of exceptions can be handled.
Statement 3: The object must be created of a specific class of which the error has occurred otherwise runtime error will occur.
False, We can also create an object of a class and access it in another class. This is often used for better organization of classes.
Statement 4: To execute a code with each and every run of the program, the code is written in the finally block.
True, Whether an exception is handled or not, the finally block is always run. Since the exception may or may not occur, it comprises all the required statements that must be printed. After the try-catch block comes the finally block.
Hence the correct answer is i and ii, iv.
Which of the following is/are advantage of using object oriented programming?
Answer (Detailed Solution Below)
Programming With Python Question 12 Detailed Solution
Download Solution PDFThe correct answer is option 4.
Concept:
Object-oriented programming:
Object-oriented programming (OOP) is a programming approach that organizes software design around data, rather than functions and logic. An object is a data field that has distinct features and behavior.
Advantages of object-oriented programming:
- Oops supports code reusability. Inheritance supports the concept of “reusability”.
- Oops, can create more than one instance of a class without interference. It can always create multiple instances of a class. Each object will hold its own individual inner variables.
- OOP does not have platform independence. Although C++ supports OOP, it is not a platform-independent language. The programming language determines platform independence.
Hence the correct answer is All options are correct.
Mistake Points option 3 is not a feature for all oops supported languages.
The variable declared inside the function is called a variable
Answer (Detailed Solution Below)
Programming With Python Question 13 Detailed Solution
Download Solution PDFThe correct answer is local.
Key Points
- A variable declared inside a function is referred to as a local variable.
- Local variables are only accessible within the function where they are declared; they cannot be accessed outside that function.
- These variables are created when the function starts execution and are destroyed when the function ends.
- Local variables are used to store temporary data that is only needed while the function is running.
- Since they are function-specific, local variables help prevent conflicts with variables of the same name in other parts of the program.
Additional Information
- Global Variables:
- Global variables are declared outside any function and can be accessed and modified from any part of the program.
- They have a global scope, meaning their value persists throughout the program's execution.
- While convenient, excessive use of global variables can lead to unintended side effects, making debugging difficult.
- External Variables:
- External variables are declared using the
extern
keyword, typically in multiple files. - They are used to share data between different files in a program.
- External variables are not initialized within the file they are declared but are initialized elsewhere in the program.
- External variables are declared using the
- Static Variables:
- Static variables retain their value even after the function in which they are declared has completed execution.
- They are initialized only once and have a scope limited to the function or file where they are declared.
- Useful for maintaining state information between function calls.
- Dynamic Variables:
- Dynamic variables are allocated memory at runtime using functions like
malloc()
ornew
in programming languages like C and C++. - They provide flexibility in memory management but require explicit deallocation to prevent memory leaks.
- Dynamic variables are allocated memory at runtime using functions like
Which of the following can be used as valid variable identifier(s) in Python?
Answer (Detailed Solution Below)
Programming With Python Question 14 Detailed Solution
Download Solution PDFThe correct answer is Option 3 (total).
Key Points
- In Python, variable identifiers must begin with a letter (A-Z or a-z) or an underscore (_) and can be followed by letters, digits (0-9), or underscores.
- The identifier total is valid because it follows Python's naming conventions and does not conflict with reserved keywords.
- Reserved keywords such as global cannot be used as variable names in Python as they have predefined meanings in the programming language.
- Other valid identifiers from the options include question and salute, but they are not the correct answer as per the provided question context.
- Python is case-sensitive, meaning that identifiers like Total (with an uppercase 'T') would be treated differently from total.
Additional Information
- Reserved Keywords in Python
- Python has 35 reserved keywords as of Python 3.10, including global, def, return, class, if, else, etc.
- These keywords are case-sensitive and cannot be used as identifiers.
- Rules for Variable Naming in Python
- Variable names can include letters, numbers, and underscores but must not begin with a digit.
- Special characters such as @, $, %, etc., are not allowed in variable names.
- Variable names should not conflict with reserved keywords.
- Case Sensitivity in Python
- Python is case-sensitive, meaning myVariable and MyVariable would be considered different identifiers.
- Best Practices for Variable Naming
- Use meaningful and descriptive names, such as total_price or user_age, to improve code readability.
- Follow snake_case (e.g., my_variable) for variable names as per Python's PEP 8 guidelines.
Which mathematical operator is represented by slash (/)?
Answer (Detailed Solution Below)
Programming With Python Question 15 Detailed Solution
Download Solution PDFThe correct answer is Division.
Key Points
- The slash (/) symbol is commonly used in mathematics and programming to represent the division operation.
- Division is a basic arithmetic operation where one number (the dividend) is divided by another number (the divisor) to get a quotient.
- For example, in the expression 10 / 2, 10 is divided by 2, resulting in a quotient of 5.
Additional Information
- Exponentiation (Option 1): This is represented by the caret (^) symbol or sometimes by double asterisks (**). It is used to raise a number to the power of another number. For example, 2^3 or 2**3 represents 2 raised to the power of 3, which equals 8.
- Multiplication (Option 3): This is represented by the asterisk (*) symbol. It is used to multiply two numbers. For example, 4 * 3 equals 12.
- Subtraction (Option 4): This is represented by the minus (-) symbol. It is used to subtract one number from another. For example, 7 - 5 equals 2.