Data Structure MCQs

What data structure would you mostly likely see in non recursive implementation of a recursive algorithm?

A. a) Stack
B. b) Linked List
C. c) Tree
D. d) Queue
Correct answer is: A. a) Stack
A stack is the data structure most likely seen in a non-recursive implementation of a recursive algorithm.

Why a Stack?

LIFO (Last In, First Out) property: This mirrors the behavior of recursive function calls, where the most recent function call is the first to finish.

Simulates function call stack: In recursion, the system implicitly uses a stack to keep track of function calls and their return addresses. A non-recursive implementation can explicitly use a stack to mimic this behavior.

How it works:

Push function arguments and local variables onto the stack: Before processing a recursive call, store relevant information on the stack.

Process the current iteration: Handle the current problem without making recursive calls.

Pop values from the stack for the next iteration: Retrieve stored information to simulate returning from a recursive call.

By using a stack, you can effectively transform a recursive algorithm into an iterative one, often improving performance and memory efficiency.

What is the value of the postfix expression 6 3 2 4 + – *?

A. a) 74
B. b) -18
C. c) 22
D. d) 40
Correct answer is: B. b) -18
Postfix notation evaluates expressions by processing operands first, followed by operators.

Given expression: 6 3 2 4 + - *

Scan the expression from left to right:

6, 3, 2, 4: These are operands, so we push them onto a stack.

+: We encounter an operator. Pop the top two elements from the stack (4 and 2), add them (4 + 2 = 6), and push the result (6) back onto the stack.

-: We encounter another operator. Pop the top two elements from the stack (6 and 3), subtract them (3 - 6 = -3), and push the result (-3) back onto the stack.

*: We encounter the last operator. Pop the top two elements from the stack (6 and -3), multiply them (6 * -3 = -18), and push the result (-18) back onto the stack.

Since we've reached the end of the expression, the final result is the only element left on the stack, which is -18.

Therefore, the value of the postfix expression 6 3 2 4 + - * is -18.

Which data structure is needed to convert infix notation to postfix notation?

A. a) Tree
B. b) Branch
C. c) Stack
D. d) Queue
Correct answer is: C. c) Stack
LIFO (Last In, First Out) property: This is crucial for handling operators and their precedence.

Efficient push and pop operations: The stack allows for quick insertion and removal of operators, which is necessary for the conversion process.

How it works:

Scan the infix expression from left to right:

If an operand is encountered, add it to the postfix expression.

If an operator is encountered:While the stack is not empty and the precedence of the top operator is greater than or equal to the precedence of the current operator, pop the top operator from the stack and add it to the postfix expression. Push the current operator onto the stack.

When the end of the expression is reached:

Pop all remaining operators from the stack and add them to the postfix expression.

By using a stack and following these steps, we can effectively convert infix expressions to their postfix equivalents.

Which of the following is not the application of stack?

A. a) Data Transfer between two asynchronous process
B. b) Compiler Syntax Analyzer
C. c) Tracking of local variables at run time
D. d) A parentheses balancing program
Correct answer is: A. a) Data Transfer between two asynchronous process
Stacks follow LIFO (Last In, First Out) principle: This means the last element added is the first one to be removed.

Data transfer between asynchronous processes requires a FIFO (First In, First Out) structure, where the first element added is the first one to be removed.

Queues are the ideal data structure for this purpose, as they adhere to the FIFO principle.

Other options:

b) Compiler Syntax Analyzer: Stacks are used extensively in compilers for parsing expressions and checking for syntax errors.

c) Tracking of local variables at run time: Function calls and their corresponding local variables are managed using stacks.

d) A parentheses balancing program: As we discussed earlier, stacks are essential for checking the balance of parentheses in an expression.

Therefore, while stacks are crucial for many applications, they are not suitable for data transfer between asynchronous processes.

The data structure required to check whether an expression contains a balanced parenthesis is?

A. ”a)
B. ”b)
C. ”c)
D. ”d)
Correct answer is: B. ”b)
”LIFO

Which data structure is used for implementing recursion?

A. a) Stack
B. b) Queue
C. c) List
D. d) Array
Correct answer is: A. a) Stack
Stack is the data structure used for implementing recursion.

Explanation:

Recursion involves a function calling itself directly or indirectly.

Each time a function is called, a new activation record is created. This record contains information about the function's parameters, local variables, and return address.

To keep track of these activation records and ensure proper execution, a LIFO (Last In, First Out) data structure is needed.

Stack is the perfect LIFO data structure for this purpose.

When a function is called, its activation record is pushed onto the stack.

When the function returns, its activation record is popped off the stack.

This process allows the system to manage multiple function calls efficiently and correctly.

What are the disadvantages of arrays?

A. a) Index value of an array can be negative
B. b) Elements are sequentially accessed
C. c) Data structure like queue or stack cannot be implemented
D. d) There are chances of wastage of memory space if elements inserted in an array are lesser than the allocated size
Correct answer is: D. d) There are chances of wastage of memory space if elements inserted in an array are lesser than the allocated size
There are chances of wastage of memory space if elements inserted in an array are lesser than the allocated size is the correct answer.

Explanation:

Fixed size: Arrays have a fixed size, which means you need to specify the number of elements it can hold when you declare it.

Memory allocation: The system allocates a contiguous block of memory for the array based on the specified size.

Wastage: If you only need to store fewer elements than the allocated size, the remaining memory space in the array goes unused, leading to memory wastage.

Other options:

a) Index value of an array can be negative: This is incorrect. Array indices typically start from 0 and go up to the length of the array minus 1. Negative indices are not valid.

b) Elements are sequentially accessed: This is a characteristic of arrays, not a disadvantage. Sequential access means elements can be accessed one after the other, which is often efficient.

c) Data structure like queue or stack cannot be implemented: This is also incorrect. While arrays are not the optimal choice for implementing queues or stacks, it's possible to do so with additional logic.

Therefore, the most significant disadvantage of arrays in terms of memory efficiency is the potential wastage of space when the array is not fully utilized.

What is a data structure?

A. A programming language
B. A collection of algorithms
C. A way to store and organize data
D. A type of computer hardware
Correct answer is: C. A way to store and organize data
Join Our WhatsApp Channel! ×
Scroll to Top