Datastructures programMCQ
Datastructures programMCQ
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]
def reverse_stack(stack):
return stack[::-1]
print(reverse_stack([1, 2, 3]))
queue = []
queue.append(10)
queue.append(20)
queue.pop(0)
queue.append(30)
print(queue)
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
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
A) O(1)
B) O(n)
C) O(log n)
D) O(n²)
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
class Stack:
def __init__(self):
self.stack = []
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
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
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]
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
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)
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
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
stack = []
stack.append('A')
stack.append('B')
stack.append('C')
print(stack.pop())
A) A
B) B
C) C
D) Error
A) 2
B) 1
C) 0
D) -1
queue = []
queue.insert(0, 'X')
queue.insert(0, 'Y')
queue.insert(0, 'Z')
print(queue.pop())
A) Z
B) Y
C) X
D) Error
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(4))
A) 4
B) 24
C) 10
D) 12
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
graph = {
0: [1, 2],
1: [0],
2: [0, 3],
3: [2]
}
print(len(graph[2]))
A) 0
B) 1
C) 2
D) 3
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
A) True
B) False
C) Error
D) None
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
graph = {
'A': ['B', 'C'],
'B': ['D'],
'C': ['E'],
'D': [],
'E': []
}
print(bfs(graph, 'A'))
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?
print(get_balance(3, 1))
A) 2
B) -2
C) 1
D) 3