Completed
Class 12 CS – Stack Data Structure
Questions From This Challenge
Q1mcq1 mark
What will be the output? def push(stack, value): stack.append(value) def pop_item(stack): return stack.pop() stack = [10, 20] push(stack, 30) x = pop_item(stack) push(stack, x + 5) print(stack)
[10, 20]
[10, 20, 30]
[10, 20, 35] ✓
[10, 30, 35]
Q2output1 mark
What will be the output?
def process(stack, x):
stack.append(x)
return stack.pop()
stack = [10, 20]
a = process(stack, 30)
b = process(stack, a + 5)
print(stack, a, b)
Correct output:
[10, 20] 30 35Q3mcq1 mark
Consider the following functions: def push(stack, value): stack.append(value) def peek(stack): if stack: return stack[-1] return -1 stack = [10, 20] push(stack, 30) x = peek(stack) push(stack, x + 10) What will be the final stack?
[10, 20, 30]
[10, 20, 40]
[10, 20, 30, 40] ✓
[10, 20, 40, 30]
Q4output1 mark
What will be the output?
def remove_top(stack):
if stack:
return stack.pop()
return -1
stack = [10, 20, 30]
a = remove_top(stack)
b = remove_top(stack)
stack.append(a + b)
print(stack)
Correct output:
[10, 50]Q5mcq1 mark
A stack contains [10, 20, 30]. The following function is called twice: def process(stack): x = stack.pop() stack.append(x + 5) After both calls, what will be the stack?
[10, 20, 35]
[10, 20, 40] ✓
[10, 25, 35]
[15, 25, 35]
Q6output1 mark
What will be the output?
def transfer(s1, s2):
s2.append(s1.pop())
s1 = [10, 20, 30]
s2 = []
transfer(s1, s2)
transfer(s1, s2)
print(s1, s2)
Correct output:
[10] [30, 20]Q7mcq1 mark
What will be the output? def add_items(stack, a, b=10): stack.append(a) stack.append(b) stack = [5] add_items(stack, 20) add_items(stack, 30, 40) print(stack)
[5, 20, 10, 30, 40] ✓
[5, 20, 30, 40]
[5, 20, 10, 30]
[5, 30, 40]
Q8output1 mark
What will be the output?
def calculate(stack):
x = stack.pop()
y = stack.pop()
stack.append(x + y)
return stack[-1]
stack = [5, 10, 15, 20]
a = calculate(stack)
b = calculate(stack)
print(stack, a, b)
Correct output:
[5, 45] 35 45Q9mcq1 mark
A stack initially contains [10, 20, 30]. The function below is called: def change(stack): x = stack.pop() if x % 2 == 0: stack.append(x // 2) else: stack.append(x + 5) What is the final stack?
[10, 20, 15] ✓
[10, 20, 30]
[10, 15, 20]
[10, 20, 60]
Q10output1 mark
What will be the output?
def push(stack, x):
stack.append(x)
def pop_sum(stack):
return stack.pop() + stack.pop()
stack = [10]
push(stack, 20)
push(stack, 30)
x = pop_sum(stack)
push(stack, x)
print(stack, x)
Correct output:
[10, 50] 50Q11mcq1 mark
What will be the output? def modify(stack, n): while n > 0: stack.append(n) n -= 2 stack = [10] modify(stack, 5) print(stack)
[10, 5, 3, 1] ✓
[10, 5, 4, 3, 2, 1]
[10, 1, 3, 5]
[5, 3, 1, 10]
Q12output1 mark
What will be the output?
def remove_two(stack):
a = stack.pop()
b = stack.pop()
return a + b
stack = [5, 10, 15, 20]
x = remove_two(stack)
stack.append(x)
print(stack)
Correct output:
[5, 10, 35]Q13mcq1 mark
Consider: def process(stack): x = stack.pop() stack.append(x + 10) return x stack = [10, 20, 30] a = process(stack) b = process(stack) What are the values of a, b and the final stack?
a = 30, b = 40, stack = [10, 20, 50] ✓
a = 30, b = 40, stack = [10, 20, 40]
a = 30, b = 30, stack = [10, 20, 50]
a = 40, b = 50, stack = [10, 20, 30]
Q14output1 mark
What will be the output?
def transfer(s1, s2):
x = s1.pop()
s2.append(x + 5)
s1 = [10, 20, 30]
s2 = [40]
transfer(s1, s2)
transfer(s1, s2)
print(s1)
print(s2)
Correct output:
[10]
[40, 35, 25]Q15mcq1 mark
A stack is initially [5, 10]. Consider: def add_and_remove(stack, x): stack.append(x) return stack.pop() a = add_and_remove(stack, 20) b = add_and_remove(stack, a + 5) What will be the final value of the stack and the values of a and b?
stack = [5, 10], a = 20, b = 25 ✓
stack = [5, 10, 20], a = 20, b = 25
stack = [5, 10], a = 25, b = 20
stack = [5, 10, 25], a = 20, b = 25