Data Structures MCQ Quiz - Objective Question with Answer for Data Structures - Download Free PDF
Last updated on Apr 15, 2025
Latest Data Structures MCQ Objective Questions
Data Structures Question 1:
Comprehension:
|
State |
Avg Temp (°C) |
Rainfall (in cm) |
Humidity |
1 |
Assam |
20 |
150 |
10.6 |
2 |
Delhi |
30 |
70 |
7.5 |
3 |
Kerala |
20 |
120 |
10.9 |
4 |
Rajasthan |
35 |
50 |
5.6 |
5 |
Telangana |
28 |
90 |
8.7 |
Using above dataframe answer the questions
Give the output of the given python code:
statedf.loc[:, 'Rainfall (in cm)'] > 90
Answer (Detailed Solution Below)
1 | True |
2 | False |
3 | True |
4 | False |
5 | False |
Data Structures Question 1 Detailed Solution
The correct answer is: Option 2.
Key Points
Concept:
The code checks which states have rainfall greater than 90 cm using a condition on the 'Rainfall (in cm)' column in the DataFrame.
Given
Rainfall data from the table:
- Assam – 150
- Delhi – 70
- Kerala – 120
- Rajasthan – 50
- Telangana – 90
Calculation
Check which values are > 90:
- Assam → True
- Delhi → False
- Kerala → True
- Rajasthan → False
- Telangana → False
Final Boolean output for the 5 rows:
[True, False, True, False, False]
Final Answer: ✅ Option 2
Data Structures Question 2:
Comprehension:
|
State |
Avg Temp (°C) |
Rainfall (in cm) |
Humidity |
1 |
Assam |
20 |
150 |
10.6 |
2 |
Delhi |
30 |
70 |
7.5 |
3 |
Kerala |
20 |
120 |
10.9 |
4 |
Rajasthan |
35 |
50 |
5.6 |
5 |
Telangana |
28 |
90 |
8.7 |
Using above dataframe answer the questions
command is used to display first five records and
command is used to display bottom five records of the dataframe.
Answer (Detailed Solution Below)
Data Structures Question 2 Detailed Solution
The correct answer is Option 3.
Key Points
- The head() function in pandas is used to display the first n records of a DataFrame.
- The tail() function in pandas is used to display the last n records of a DataFrame.
- In the given example,
statedf.head(5)
is used to display the first five records of the DataFrame statedf. - Similarly,
statedf.tail(5)
is used to display the bottom five records of the DataFrame statedf. - Therefore, the correct syntax to achieve the desired result is
statedf.head(5)
andstatedf.tail(5)
.
Additional Information
- The head() and tail() functions are very useful for quickly inspecting the contents of a DataFrame.
- These functions can be used without arguments to display the first and last 5 records by default, i.e.,
statedf.head()
andstatedf.tail()
. - Using negative indices with
head()
ortail()
functions is not valid and will result in an error. - It is a common practice to inspect the first few and last few records of a DataFrame to understand the data structure and contents.
Data Structures Question 3:
Comprehension:
|
State |
Avg Temp (°C) |
Rainfall (in cm) |
Humidity |
1 |
Assam |
20 |
150 |
10.6 |
2 |
Delhi |
30 |
70 |
7.5 |
3 |
Kerala |
20 |
120 |
10.9 |
4 |
Rajasthan |
35 |
50 |
5.6 |
5 |
Telangana |
28 |
90 |
8.7 |
Using above dataframe answer the questions
Name the attribute used to display a tuple showing the dimensions of statedf dataframe i... (5, 4), 5 rows and 4 columns respectively.
Answer (Detailed Solution Below)
Data Structures Question 3 Detailed Solution
The correct answer is : Option 2.
Key Points
Concept:
To find the number of rows and columns in a pandas Dataframe, the .shape attribute is used. It returns a tuple of the form (rows, columns).
Given
The DataFrame 'statedf' has 5 rows and 4 columns as shown in the table.
Calculation
Using the statement: statedf.shape
will return the tuple:
\((5, 4)\)
Which represents 5 rows and 4 columns.
Final Answer: ✅ statedf.shape
Data Structures Question 4:
Comprehension:
|
State |
Avg Temp (°C) |
Rainfall (in cm) |
Humidity |
1 |
Assam |
20 |
150 |
10.6 |
2 |
Delhi |
30 |
70 |
7.5 |
3 |
Kerala |
20 |
120 |
10.9 |
4 |
Rajasthan |
35 |
50 |
5.6 |
5 |
Telangana |
28 |
90 |
8.7 |
Using above dataframe answer the questions
Identify the correct statement from the following to display all the data of "Rajasthan" state.
Answer (Detailed Solution Below)
Data Structures Question 4 Detailed Solution
The correct answer is statedf.loc['Rajasthan'].
Key Points
- The correct way to access all the data of "Rajasthan" state in a DataFrame is using
statedf.loc['Rajasthan']
. - The
loc
accessor is used to access a group of rows and columns by labels or a boolean array. - In this case, we are using the label 'Rajasthan' to retrieve the row corresponding to the state of Rajasthan.
- Other options are incorrect due to syntax errors or incorrect usage of the
loc
accessor.
Additional Information
- The
loc
accessor is a powerful tool in pandas for data selection by label. It allows for both row and column selection. - Using
loc
, you can also select a subset of the DataFrame by specifying a range of labels. - Here is an example of using
loc
to select a specific row:import pandas as pd # Sample DataFrame data = {'State': ['Maharashtra', 'Gujarat', 'Rajasthan', 'Punjab'], 'Population': [112374333, 60439692, 68548437, 27743338]} statedf = pd.DataFrame(data) statedf.set_index('State', inplace=True) # Accessing data for Rajasthan rajasthan_data = statedf.loc['Rajasthan'] print(rajasthan_data)
- This code will output the data for the state of Rajasthan as stored in the DataFrame.
- Always ensure that the label you use with
loc
exists in the DataFrame's index to avoid KeyError.
Data Structures Question 5:
Comprehension:
|
State |
Avg Temp (°C) |
Rainfall (in cm) |
Humidity |
1 |
Assam |
20 |
150 |
10.6 |
2 |
Delhi |
30 |
70 |
7.5 |
3 |
Kerala |
20 |
120 |
10.9 |
4 |
Rajasthan |
35 |
50 |
5.6 |
5 |
Telangana |
28 |
90 |
8.7 |
Using above dataframe answer the questions
Identify the correct code to remove the column Humidity from the given dataframe, statedf:
Answer (Detailed Solution Below)
Data Structures Question 5 Detailed Solution
The correct answer is Option 4.
Key Points
- To remove a column from a DataFrame in pandas, the
drop()
method is used with the parameteraxis
set to 1. - The
axis
parameter specifies whether to drop a row (0) or a column (1). - The correct syntax to remove the column "Humidity" from the DataFrame
statedf
is:
import pandas as pd
# Sample DataFrame
data = {
'State': ['California', 'Texas', 'New York'],
'Humidity': [50, 60, 55],
'Temperature': [70, 80, 75]
}
statedf = pd.DataFrame(data)
# Correct code to remove the column "Humidity"
statedf = statedf.drop(['Humidity'], axis=1)
print(statedf)
Additional Information
- The
pop()
method can also be used to remove a column, but it returns the removed column, not the modified DataFrame. - To drop multiple columns, you can pass a list of column names to the
drop()
method. - Using
axis=0
would attempt to drop rows rather than columns, which is incorrect in this context.
Top Data Structures MCQ Objective Questions
Data Structures Question 6:
When a * b is written as *ab, it is called:
Answer (Detailed Solution Below)
Data Structures Question 6 Detailed Solution
Correct answer: Option 3
Explanation:
- When the operands are followed by the operator, we call such an expression a postfix expression. A postfix expression is also called a Reverse Polish Notation (RPN).
- When the operands are preceded by the operator, we call such an expression a prefix expression. A prefix expression is also called a Polish notation.
- When the operator is placed in between the operators, it is called an infix expression.
Important points:
Here, a * b is the infix expression and *ab is the prefix expression or the Polish notation.
Data Structures Question 7:
Which type of data structure stack is it?
Answer (Detailed Solution Below)
Data Structures Question 7 Detailed Solution
The correct answer is option 1.
Concept:
Linear data structure:
The elements are accessed in sequential order but it is not compulsory to store all the elements sequentially.
Example:
Queue, Stack, and Linked List.
Stack:
A stack is a linear data structure in which operations are carried out in a specific order. The sequence might be LIFO (Last In First Out) or FILO (First In Last Out).
Example:
Consider the canteen, where plates are heaped on top of one another. The plate at the top is the first to be removed, but the plate at the bottom is the one that stays in the stack the longest.
The first insert 1 is poped at last so 1 is there in the stack for the longest period of time.
Explanation:
A data structure stack is a linear data structure because the elements of the stack are accessed in sequential order but it is not compulsory to store all the elements sequentially. In the above example, element 1 is accessed only after elements 2 and 3. Hence it follows the linear fashion.
Hence the correct answer is Linear.
Additional Information
Basic Operations of Stack:
There are a few fundamental operations that we may use to conduct various tasks on a stack.
- Push: Add an element to the top of a stack.
- Pop: Remove an element from the top of a stack.
- IsEmpty: Check if the stack is empty.
- IsFull: Check if the stack is full.
- Peek: Get the value of the top element without removing it.
Non-linear data structure:
Elements of the data structures are stored or accessed in a non-linear order.
Example:
Trees and Graphs.
Data Structures Question 8:
What is the value after evaluation of the following expression?
78+52-*
Answer (Detailed Solution Below)
Data Structures Question 8 Detailed Solution
The correct answer is option 4.
Concept:
Stack:
A Stack is a data structure that is linear. It uses the Last In, First Out (LIFO) storage mechanism. A new element is always added to the top of a stack when it is added, and the top element is always removed first from a stack.
Explanation:
Postfix Expression Evaluation using Stack:
- In the provided Postfix Expression, read each symbol one by one from left to right.
- If the reading symbol is an operand, it should be pushed to the Stack.
- Perform TWO pop operations and store the two popped operands in two distinct variables if the reading symbol is operator (+, -, *, /, etc). (operand1 and operand2). Then, using operand1 and operand2, do a reading symbol operation and return the result to the Stack.
- Finally! pop the value and show it as the final result.
It prints as output 45.
Hence the correct answer is 45.
Data Structures Question 9:
Trying to add an element to a full stack results in an exception called ____________.
Answer (Detailed Solution Below)
Data Structures Question 9 Detailed Solution
The correct option is overflow
CONCEPT:
The basic operations of the stack may hit a special condition or raise an error like:
Underflow happens when we try to pop (remove) an item from the stack when the stack is already empty.
Overflow happens when we try to push an item onto a stack, when the stack is full or out of memory.
Additional Information
Stack is a linear data structure that follows a last in first out order.
In python, we can implement a stack using a list data structure by using append() and pop() methods
Basic Operations of Stack:
- Push: Adding an item to the top of the stack is done using the push operation.
- Pop: Removing an item from the top of the stack is done using the pop operation.
In python, we can perform
Push item using the append() method
Pop top item using the pop() method
Data Structures Question 10:
A ____________ notation is used for writing an expression in which binary operators are written in between the operands.
Answer (Detailed Solution Below)
Data Structures Question 10 Detailed Solution
Correct option is Infix
CONCEPT:
An expression mainly consists of operands, operators, and symbols.
These must be arranged according to a set of rules so that expressions can be evaluated using the set of rules.
There are mainly three notations used for writing expression:
Infix notation: When the operator is written in between the operands it is known as infix notation. Example 2+5
Prefix notation: When the operator is written before the operands it is known as Prefix notation. Example +25
Postfix notation: When the operator is written after the operands it is known as Postfix notation. Example 25+
Data Structures Question 11:
Two fundamental operations performed on the stack are ________________.
Answer (Detailed Solution Below)
Data Structures Question 11 Detailed Solution
The correct answer is option 4.
Concept:
Stack:
A stack is a linear data structure in which operations are carried out in a specific order. The sequence might be LIFO (Last In First Out) or FILO (First In Last Out). A stack is an abstract data type that acts as a collection of components and has two primary operations: Pop and Push
Example:
Consider the canteen, where plates are heaped on top of one another. The plate at the top is the first to be removed, but the plate at the bottom is the one that stays in the stack the longest.
The first insert 1 is poped at last so 1 is there in the stack for the longest period of time.
Explanation:
Stack has some basic operations which are push and pops without these operations we can not perform the insert and delete the elements Hence the push and pop are the basic operations.
Basic Operations of Stack:
There are a few fundamental operations that we may use to conduct various tasks on a stack.
- Push: Add an element to the top of a stack.
- Pop: Remove an element from the top of a stack.
- IsEmpty: Check if the stack is empty.
- IsFull: Check if the stack is full.
- Peek: Get the value of the top element without removing it.
Hence the correct answer is PUSH & POP.
Data Structures Question 12:
In python, which data structure is preferred over a list when there is a need for quicker append and pop operations from both the ends of the container?
Answer (Detailed Solution Below)
Data Structures Question 12 Detailed Solution
append means to insert an element into the queue.
Because deque takes constant time to do that, but list takes more time for that.
Deque refers to double ended queue , so, it have the provision to insert and delete from both front side as well as from back side.
but in list , We have to traverse from the beginning to do that , which takes obviously more time,
So, option 1 will be the answer
.
Why option 2,3 got eliminated -
Stack is not for this operation , in stack , we can insert and delete from one end only ,
in queue we can insert from rear side and delete from front side , still that will take more time.
Data Structures Question 13:
Which of the following operation will be performed by pops function on stack "stackname" in the following code?
def pops(stackname):
return len(stackname)
Answer (Detailed Solution Below)
Data Structures Question 13 Detailed Solution
Correct option is Returns the size of stack.
CONCEPT:
In python def keyword is used to define a function , it is placed before a function name.
Syntax:
def function_name:
definition statements...
In the above question pops is a user-defined function that takes a stack named "stackname" as argument
and returns the length of stack using len() function.
Example: Let stackname=[1,2,3] is a stack which contains 3 elements
On calling the pops(stackname) function, it will return 3 as length of stack.
Data Structures Question 14:
Elements “5”, “9”, “2” and “4” are placed in a queue and are deleted one at a time. In what order will they be removed?
Answer (Detailed Solution Below)
Data Structures Question 14 Detailed Solution
The correct option is 5924
CONCEPT:
A queue is an ordered linear data structure, following the FIFO strategy.
We will add the elements using enqueue() method and the queue looks like:
On deleting elements one by one from the front using the dequeue() method we get 5 9 2 4 as a sequence.
Data Structures Question 15:
While conversion of an Infix notation to its equivalent Prefix/Postfix notation, only ______________ are PUSHED onto the Stack.
Answer (Detailed Solution Below)
Data Structures Question 15 Detailed Solution
The correct Option is operators
CONCEPT:
While performing the conversion of an expression, we need to follow some set of rules (operator precedence and associativity rules) to evaluate the result.
The position of operators is an important factor to decide the type of expression and to differentiate between the precedence and associativity of two different operators stack data structure is used.
That is why only operators are pushed onto the stack.
Example: Converting infix expression to postfix using stack
Infix: (a+b)-c
Postfix: ab+c-
Input Symbol | Stack Contents | Postfix Expression | Explanation |
( | ( | If the incoming symbol is '(', push it onto the stack. | |
a | ( | a |
Print operands as they arrive. |
+ | (+ | a | If the stack is empty or contains a left Parenthesis on top, push the incoming operator onto the stack. |
b | (+ | b | Print operands as they arrive. |
) | ab+ | If the incoming symbol is ')', pop the stack & Print the operators until the left parenthesis is found. | |
- | - | ab+ |
If the stack is empty or contains a left Parenthesis on top, push the incoming operator onto the stack. |
c | - | ab+c | Print operands as they arrive. |
ab+c- | At the end of the expression, pop & print all operators of the stack. |
With the above example, we can say that only operators are PUSHED onto the stack.