0% found this document useful (0 votes)
5 views6 pages

Stack and Exception Handling Worksheet

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)
5 views6 pages

Stack and Exception Handling Worksheet

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

Advanced Worksheet – Stack & Exception Handling (40 Marks)

Section A: MCQs (1 × 5 = 5 Marks)


1. Which of the following situations will definitely raise an exception?
a) Accessing an empty stack with pop()
b) Pushing an item into a non-full stack
c) Checking the size of the stack
d) Printing the stack elements

2. Consider the code:


s = []
for i in range(3):
[Link](i)
for i in range(4):
print([Link]())
Which error will be raised?
a) IndexError
b) ValueError
c) KeyError
d) No error

3. Which statement is true for exception handling?


a) else block executes only if an exception occurs
b) finally block executes only when no exception occurs
c) raise keyword is used to explicitly throw an exception
d) Multiple except blocks for the same exception type are allowed

4. Output of:
try:
num = int('12A')
except ValueError:
print('Error1')
except:
print('Error2')
else:
print('Success')
finally:
print('Done')
a) Error1 Done
b) Error2 Done
c) Success Done
d) Done only

5. Which exception will occur for the code?


d = {'A':1,'B':2}
print(d['C'])
a) KeyError
b) IndexError
c) ValueError
d) TypeError

Section B: Short Answer (2 × 5 = 10 Marks)


6. Differentiate between user-defined exceptions and built-in exceptions with examples.

7. Write a Python function reverse_string() that uses a stack to reverse a given string
without using slicing.
def reverse_string(s):
stack = []
for ch in s:
[Link](ch)
rev = ''
while stack:
rev += [Link]()
return rev

8. Explain the difference between: raise Exception('Error') and assert False, 'Error'.
raise Exception('Error') → explicitly throws exception.
assert False, 'Error' → raises AssertionError if condition False.

9. Consider the stack: stk = [5, 15, 25]. Write Python code to:
- Push the sum of all elements.
- Pop two elements and display the stack.
stk = [5, 15, 25]
[Link](sum(stk)) # Push 45
[Link](); [Link]() # Pop two elements
print(stk) # Remaining stack

10. Output-based:
try:
a = [1, 2, 3]
for i in range(5):
print(a[i])
except IndexError:
print('Out of range')
else:
print('All good')
finally:
print('Complete')

Section C: Application/Programming (3 × 5 = 15 Marks)

13. A school stores student roll numbers in a stack. Write Python code to:
- Push roll numbers 101, 102, 103, 104.
- Pop until the stack becomes empty.
- Use try-except to handle underflow and print a message when trying to pop from an
empty stack.
stk = []
for r in [101,102,103,104]: [Link](r)
try:
while True:
print([Link]())
except IndexError:
print('Stack Underflow')

You might also like