0% found this document useful (0 votes)
9 views11 pages

Datastructures programMCQ

The document contains a series of multiple-choice questions (MCQs) related to medium-level data structures and their implementations in Python. Each question tests knowledge on various topics such as arrays, stacks, queues, linked lists, trees, graphs, and algorithms. The questions include code snippets and ask for outputs or explanations of the code behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

Datastructures programMCQ

The document contains a series of multiple-choice questions (MCQs) related to medium-level data structures and their implementations in Python. Each question tests knowledge on various topics such as arrays, stacks, queues, linked lists, trees, graphs, and algorithms. The questions include code snippets and ask for outputs or explanations of the code behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Medium-Level Data Structures - Code-Based MCQs

1. What will be the output of the following code?

arr = [1, 2, 3, 4, 5]
arr.insert(2, 10)
print(arr)

A) [1, 2, 10, 3, 4, 5]
B) [1, 2, 3, 10, 4, 5]
C) [1, 2, 3, 4, 5, 10]
D) [1, 10, 2, 3, 4, 5]

2. What does the following code do?

def reverse_stack(stack):
return stack[::-1]

print(reverse_stack([1, 2, 3]))

A) Reverses the stack


B) Sorts the stack
C) Returns the same stack
D) Removes the last element

3. What is the output of this code?

queue = []
queue.append(10)
queue.append(20)
queue.pop(0)
queue.append(30)
print(queue)

A) [10, 20, 30]


B) [20, 30]
C) [10, 30]
D) [30]

4. What will the following code print?

class Node:
def __init__(self, data):
self.data = data
self.next = None

head = Node(5)
head.next = Node(10)
print(head.next.data)

A) 5
B) 10
C) None
D) Error

5. What is the output of the below program?

a = [3, 1, 4, 1, 5]
a.sort()
print(a)

A) [1, 1, 3, 4, 5]
B) [3, 1, 4, 1, 5]
C) [5, 4, 3, 1, 1]
D) Error

6. What is the time complexity of the following search?

arr = [10, 20, 30, 40]


for i in arr:
if i == 30:
print("Found")

A) O(1)
B) O(n)
C) O(log n)
D) O(n²)

7. What will the output be?

from collections import deque

dq = deque()
dq.appendleft(1)
dq.appendleft(2)
dq.appendleft(3)
print(dq)

A) deque([3, 2, 1])
B) deque([1, 2, 3])
C) deque([1])
D) deque([3])
8. What does the below function do?

def is_palindrome(s):
return s == s[::-1]

A) Reverses a string
B) Sorts a string
C) Checks if a string is a palindrome
D) Returns True for vowels

9. What will be the output?

class Stack:
def __init__(self):
self.stack = []

def push(self, item):


self.stack.append(item)

def pop(self):
return self.stack.pop()

s = Stack()
s.push(10)
s.push(20)
print(s.pop())

A) 10
B) 20
C) [10, 20]
D) Error

10. What is the output of this Binary Tree snippet?

class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None

root = Node(1)
root.left = Node(2)
root.right = Node(3)
print(root.left.val)

A) 1
B) 2
C) 3
D) None

11. What is the output of the following heap operation?

import heapq
arr = [5, 1, 8, 3]
heapq.heapify(arr)
print(arr[0])

A) 8
B) 5
C) 1
D) 3

12. What will be the output of this postorder tree traversal function?

class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key

def postorder(root):
if root:
postorder(root.left)
postorder(root.right)
print(root.val, end=' ')

root = Node(1)
root.left = Node(2)
root.right = Node(3)
postorder(root)

A) 1 2 3
B) 2 3 1
C) 3 2 1
D) 2 1 3
13. What will be the output of this graph adjacency list creation?

graph = {}
graph[0] = [1, 2]
graph[1] = [2]
graph[2] = [0, 3]
graph[3] = [3]
print(graph[2])

A) [0, 3]
B) [2]
C) [1, 2]
D) [3]

14. What will the following code output?

stack = [1, 2, 3, 4, 5]
while stack:
print(stack.pop(), end=' ')

A) 5 4 3 2 1
B) 1 2 3 4 5
C) 1 2 3 4
D) Error

15. What will be printed by the following?

arr = [1, 2, 3, 4]
print(sum(arr) // len(arr))

A) 2
B) 3
C) 2.5
D) 10
Medium-Level Data Structures - Code-Based MCQs (Set 2)

1. What is the output of the following function?

def count_even(nums):
count = 0
for num in nums:
if num % 2 == 0:
count += 1
return count

print(count_even([1, 2, 3, 4, 5, 6]))

A) 2
B) 3
C) 4
D) 6

2. What is the result of this linked list operation?

class Node:
def __init__(self, data):
self.data = data
self.next = None

a = Node(1)
b = Node(2)
a.next = b
c = Node(3)
b.next = c

print(a.next.next.data)

A) 1
B) 2
C) 3
D) None

3. What is printed by this stack implementation?

stack = []
stack.append('A')
stack.append('B')
stack.append('C')
print(stack.pop())

A) A
B) B
C) C
D) Error

4. What is the output of the following function?

def binary_search(arr, x):


low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1

print(binary_search([1, 3, 5, 7, 9], 5))

A) 2
B) 1
C) 0
D) -1

5. What will be the result of this queue simulation?

queue = []
queue.insert(0, 'X')
queue.insert(0, 'Y')
queue.insert(0, 'Z')
print(queue.pop())

A) Z
B) Y
C) X
D) Error

6. What is the output of the following recursive function?

def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)

print(factorial(4))

A) 4
B) 24
C) 10
D) 12

7. What will be printed by this heap operation?

import heapq
h = []
heapq.heappush(h, 20)
heapq.heappush(h, 5)
heapq.heappush(h, 15)
print(heapq.heappop(h))

A) 20
B) 5
C) 15
D) 25

8. What will be printed after this graph operation?

graph = {
0: [1, 2],
1: [0],
2: [0, 3],
3: [2]
}
print(len(graph[2]))

A) 0
B) 1
C) 2
D) 3

9. What is the result of this inorder traversal?

class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data

def inorder(root):
if root:
inorder(root.left)
print(root.data, end=' ')
inorder(root.right)

root = Node(1)
root.left = Node(2)
root.right = Node(3)
inorder(root)

A) 1 2 3
B) 2 1 3
C) 3 2 1
D) 2 3 1

10. What is the result of the following operation?

arr = [2, 4, 6, 8, 10]


print(all(x % 2 == 0 for x in arr))

A) True
B) False
C) Error
D) None

11. What is the output of this advanced tree traversal?

class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key

def height(node):
if node is None:
return -1
else:
return 1 + max(height(node.left), height(node.right))

root = Node(10)
root.left = Node(20)
root.right = Node(30)
root.left.left = Node(40)
print(height(root))

A) 1
B) 2
C) 3
D) 4

12. What will be the output of this graph BFS simulation?

from collections import deque

def bfs(graph, start):


visited = set()
queue = deque([start])
result = []
while queue:
vertex = queue.popleft()
if vertex not in visited:
visited.add(vertex)
result.append(vertex)
queue.extend(graph[vertex])
return result

graph = {
'A': ['B', 'C'],
'B': ['D'],
'C': ['E'],
'D': [],
'E': []
}
print(bfs(graph, 'A'))

A) ['A', 'B', 'C', 'D', 'E']


B) ['A', 'C', 'B', 'E', 'D']
C) ['A', 'B', 'D', 'C', 'E']
D) ['A']

13. What is the output of this dictionary-based hashing simulation?

hash_table = {}
hash_table[10] = 'apple'
hash_table[20] = 'banana'
hash_table[10] = 'orange'
print(hash_table[10])

A) 'apple'
B) 'banana'
C) 'orange'
D) Error
14. What will the output of the following code be?

class Node:
def __init__(self, val):
self.val = val
self.next = None

def detect_loop(head):
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False

a = Node(1)
b = Node(2)
c = Node(3)
a.next = b
b.next = c
c.next = a
print(detect_loop(a))

A) True
B) False
C) Error
D) None

15. What is the output of this AVL tree height calculation logic?

def get_balance(left_height, right_height):


return left_height - right_height

print(get_balance(3, 1))

A) 2
B) -2
C) 1
D) 3

You might also like