SECTION-WISE QUESTIONS & ANSWERS
🔢 1. Lists
🟩 Question 24 – List Operations
(I)
Q: Write a statement to count the occurrences of 4 in L1 = [1,2,3,2,1,2,4,2,...]
✅ A: L1.count(4)
OR
Q: Write a statement to sort the elements of list L1 in ascending order.
✅ A: L1.sort()
(II)
Q: Write a statement to insert all the elements of L2 = [10,20,30,...] at the end of L1.
✅ A: L1.extend(L2)
OR
Q: Write a statement to reverse the elements of list L2.
✅ A: L2.reverse()
🌀 2. Tuples
🟩 Question 26 – Tuple Error Correction
Q: The function below is intended to swap the first and last elements of a tuple.
Correct all syntax and logical errors.
Original:
def swap_first_last(tup)
if len(tup) < 2:
return tup
new_tup = (tup[-1],) + tup[1:-1] + (tup[0])
return new_tup
result = swap_first_last((1, 2, 3, 4))
print("Swapped tuple: " result)
✅ Corrected Version:
python
CopyEdit
def swap_first_last(tup):
if len(tup) < 2:
return tup
new_tup = (tup[-1],) + tup[1:-1] + (tup[0],)
return new_tup
result = swap_first_last((1, 2, 3, 4))
print("Swapped tuple:", result)
📚 3. Dictionaries
🟩 Question 31(A) – Predict Output of Dictionary Iteration
Q:
d = {"apple": 15, "banana": 7, "cherry": 9}
str1 = ""
for key in d:
str1 = str1 + str(d[key]) + "@\n"
str2 = str1[:-1]
print(str2)
✅ A:
CopyEdit
15@
7@
9
🧱 4. Stacks
🟩 Question 30(A) – Stack Operations on Book Records
Q: You have a stack named BooksStack containing records [title, author, year].
Write Python functions to:
● Push a book
● Pop a book
● Peek at the top book
✅ A:
def push_book(BooksStack, new_book):
BooksStack.append(new_book)
def pop_book(BooksStack):
if not BooksStack:
print("Underflow")
else:
return BooksStack.pop()
def peep(BooksStack):
if not BooksStack:
print("None")
else:
print(BooksStack[-1])
🟩 Question 30(B) – Stack of Even Numbers
Q: Write a function push_even(N) to push even numbers from a list into a stack named
EvenNumbers.
Then write:
● pop_even() to remove top element.
● Disp_even() to display top element without removing it.
✅ A:
def push_even(N):
EvenNumbers = []
for num in N:
if num % 2 == 0:
EvenNumbers.append(num)
return EvenNumbers
VALUES = []
for i in range(5):
VALUES.append(int(input("Enter an integer: ")))
EvenNumbers = push_even(VALUES)
def pop_even():
if not EvenNumbers:
print("Underflow")
else:
print(EvenNumbers.pop())
def Disp_even():
if not EvenNumbers:
print("None")
else:
print(EvenNumbers[-1])
📝 Example:
If input list is [10, 5, 8, 3, 12], then stack becomes [10, 8, 12]
1. Lists
Q23(A)
Q: Remove the item with key "NISHA" from the dictionary Students.
✅ A:
python
CopyEdit
Students.pop("NISHA")
# or del Students["NISHA"]
Q: Count how many times "is" appears in the string message.
✅ A:
python
CopyEdit
message.count("is")
Q23(B)
Q: Convert a tuple subject to a list and delete its last element.
✅ A:
python
CopyEdit
subject = list(subject)
subject.pop() # or del subject[-1]
Q30 – Stack from list using digit count
Q: Write PushBig() and PopBig() to handle 5-digit or larger integers from list Nums using a
stack BigNums.
Sample Input:
python
CopyEdit
Nums = [213, 10025, 167, 254923, 14, 1297653, 31498, 386, 92765]
✅ A:
python
CopyEdit
def PushBig(Nums, BigNums):
for N in Nums:
if len(str(N)) >= 5:
BigNums.append(N)
def PopBig(BigNums):
while BigNums:
print(BigNums.pop())
print("Stack Empty")
✅ Expected Output:
mathematica
CopyEdit
92765
31498
1297653
254923
10025
Stack Empty
🔁 2. Tuples
Q23(B)
✅
Q: Convert a tuple to a list and delete the last element.
A (repeated for reference):
python
CopyEdit
subject = list(subject)
subject.pop()
📦 3. Dictionaries
Q21(A)
Q: Write showGrades(S) to display grades based on average marks in a dictionary:
python
CopyEdit
S = {"AMIT": [92, 86, 64], "NAGMA": [65, 42, 43], "DAVID": [92, 90,
88]}
✅ A:
python
CopyEdit
def showGrades(S):
for K, V in S.items():
avg = sum(V)/3
if avg >= 90:
Grade = "A"
elif avg >= 60:
Grade = "B"
else:
Grade = "C"
print(K, "–", Grade)
✅ Output:
css
CopyEdit
AMIT – B
NAGMA – C
DAVID – A
Q22
Q: Output of the following code:
python
CopyEdit
LS = ["HIMALAYA", "NILGIRI", "ALASKA", "ALPS"]
D = {}
for S in LS:
if len(S) % 4 == 0:
D[S] = len(S)
for K in D:
print(K, D[K], sep="#")
✅ A:
bash
CopyEdit
HIMALAYA#8
ALPS#4
📚 4. Stacks (List as Stack)
Q30 (Detailed above)
Q21(B)
Q: Write Puzzle(W, N) to replace every Nth letter with _.
✅ A:
python
CopyEdit
def Puzzle(W, N):
result = ""
for i in range(len(W)):
if (i + 1) % N == 0:
result += "_"
else:
result += W[i]
return result
print(Puzzle("TELEVISION", 3)) # Output: TE_EV_SI_N
1. Lists
Q2.
Q: Given the list L = [1, 3, 6, 82, 5, 7, 11, 92], what will print(L[2:5]) output?
✅ A: [6, 82, 5]
Q24(a)
Q: Evaluate: 2 * 3 + 4 ** 2 - 5 // 2
✅ A: 20
Q33
Q: What will the following output?
python
CopyEdit
def change(A):
S = 0
for i in range(len(A)//2):
S += (A[i]*2)
return S
B = [10,11,12,30,32,34,35,38,40,2]
C = change(B)
print('Output is', C)
✅ A: Output is 190
🌀 2. Tuples
Q5.
Q: If T = (10, 12, 43, 39), which statement is incorrect?
● a) print(T[1])
● b) T[2] = -29
● c) print(max(T))
● d) print(len(T))
✅ A: b) T[2] = -29 (Tuples are immutable)
Q7.
Q: Given T = (2, 5, 6, 9, 8), what will sum(T) return?
✅ A: 30
Q30.
Q: What are tuples in a SQL table? Give an example.
✅ A:
● Tuples are rows of a table.
● Example:
text
CopyEdit
TNO NAME START END
T1 RAVI KUMAR DELHI MUMBAI
T2 NISHANT JAIN DELHI KOLKATA
📦 3. Dictionaries
Q2 (MCQ):
Q: What is the output of:
python
CopyEdit
D = {'Rno': 1, 'Name': 'Suraj'}
print(D('Name'))
✅ A: TypeError (Should be D['Name'])
📚 4. Stacks
Q37 (a).
Q: Write POP_PUSH(LPop, LPush, N) to pop last N elements from LPop and push to LPush.
✅ A:
python
CopyEdit
def POP_PUSH(LPop, LPush, N):
if len(LPop) < N:
print("Pop not possible")
else:
for i in range(N):
LPush.append(LPop.pop())
Q37 (b).
Q: Write POPSTACK(L) to pop from a stack L implemented using a list.
✅ A:
python
CopyEdit
def POPSTACK(L):
if len(L) > 0:
return L.pop()
else:
return None