[Type B] Chapter – 9 Class 12 CS – Sumita Arora Assignment | Q/A

Here is class 12 computer science Unit 9 [Type B] solutions for Sumita Arora back exercise assignment. Below includes both textual and video solutions wherever required. View all the answers in assignment for chapter 9 and for all chapters here.

Watch all video tutorials for chapter 9: Data Structures – 1 Linear Lists.

Q1: Create a list SqLst that stores the doubles of elements of another list NumLst. The following code is trying to achieve this.  Will this code work as desired?  What will be stored in SqLst after the following code?

NumLst: = [2, 5,1,7,3,6,8,9]
SqLst   = NumLst * 2
Output:
[2, 5, 1, 7, 3, 6, 8, 9, 2, 5, 1, 7, 3, 6, 8, 9]

No, this code will just duplicate and add the list not as the desired purpose in question.
Q2: Change the above code so that it works as stored in the previous question.
To make the code to work according to the purpose.
SqLst = [2*i for i in NumLst]
Q3: Modify your previous code so that SqLst stores the doubled numbers in ascending order.
SqLst = sorted([2*i for i in NumLst])
Q4: Consider a List ML =(1,4,9,16,25,36,49,64,81,100); Write coding using a list comprehension that takes the List ML and makes a new list that has only the even elements of this list in it.
The code for above condition
[i for i in ML if(i%2 == 0)]

Output:
[4, 16, 36, 64, 100]
Q5: Write equivalent list comprehension for the following code

Target1 = []
for number in source:
    If number & 1 :
       Target1.append (number)
[i for i in ML if(i%2 != 0)]
Q6: Write equivalent for loop for the following list comprehension :
  gen=( i/2 for i in [0,9,21,32] )
  print(gen)
gen = []
for i in [0, 9, 21, 32]:
  gen.append(i/2)
print(gen)
Q7: Predict the output of following code if the input is:
(i) 12, 3, 4, 5, 7, 12, 8, 23, 12
(ii) 8, 9, 2, 3, 7, 8

Code:
s = eval(input(“Enter a list : “))
n = len(s)
t = s[1:n-1]
print(s[0] == s[n-1] and t.count(s[0] ==0)
Output:
(i) False
(ii) True
Q8: Predict the output:
Chapter 9 Type-B Sumita Arora
Question 8

Solution:

Output:
5 [21, 12] 3
5 [21, 12] 3
[21, 12] 31 2
Q9: Predict the output:
ages = [11, 14, 15, 17, 13, 18, 25]
  print(ages)
Elig = [x for x in ages if x in range(12, 18)]
print(Elig)

Solution:

Indentation error
Q10: Predict the output:
L1 = [x**2 for x in range(10) if x%3 == 0]
L2 = L1
L1.append(len(L1))
print(L1)
print(L2)
L2.remove(len(L2)-1)
print(L1)

Solution:

Output:
[0, 9, 36, 81, 4]
[0, 9, 36, 81, 4]
[0, 9, 36, 81]
Q11: Predict the output:
def even(n):
    return n%2==0
list1 = [1,2,3,4,5,6,7,8,9]
ev = [n for n in list1 if n%2==0]
evp = [n for n in list1 if even(n)]
print(evp)

Solution:

Output:
[2, 4, 6, 8]
Q12: Predict the output:
(i)
b = [[9,6], [4,5], [7,7]]
x = b[:2]
x.append(10)
print(x)

(ii)
b = [[9,6], [4,5], [7,7]]
x = b[:2]
x[1].append(10)
print(x)

Output:

(i)
[[9, 6], [4, 5], 10]

(ii)
[[9, 6], [4, 5, 10]]
Q13: Find the Error: Consider the following code, which runs correctly at time but gives error at other times.  Find the error and its reason.
Chapter 9 Type-B Sumita Arora
Question 13

Solution:

The code will cause error if ch is 2 because then value 100 being absent in Lst1 and Lst2 and its index being asked.

Also, the error will encounter on ch is 3 as Lst2 is empty and underflow case (nothing to pop out) arise.
Q14: Suggest the correction for the error(s) in the previous question’s code.
# corrected code

Lst1 = [23, 34, 12, 77, 34, 26, 28, 93, 48, 69, 73, 23, 19, 88]
Lst2 = []
print("List1 original is :", Lst1)
ch = int(input("Enter 1/2/3 and predict which operation was performed?"))
if ch == 1:
  Lst1.append(100)
  Lst2.append(100)
elif ch == 2:
  if Lst1.count(100) >0:
    print(Lst1.index(100))
  if Lst2.count(100) >0:
    print(Lst2.index(100))
elif ch == 3:
  print(Lst1.pop())
Q15: Find the error. Consider the following code(s) and predict the error(s):

(i) y for y in range(100) if y % 2 == 0 and if y % 5 == 0
(ii) (y for y in range(100) if y % 2 == 0 and if y % 5 == 0 )
(i) The if statement is called twice and list comprehension is called in incorrect way
#correct code
[y for y in range(100) if (y % 2 == 0 and y % 5 == 0)]

(ii) The twice if statement
# correct code
(y for y in range(100) if (y % 2 == 0 and y % 5 == 0 ))
Q16: Find the error in the following list comprehension :
[“good” if i < 3: else : “better” for i in range(6)]
#correct code
["good" if i < 3 else "better" for i in range(6)]

No requirement of the colon in list comprehension.
Q17: Suggest corrections for the errors in both the previous questions:
Q15:
(i) The if statement is called twice and list comprehension is called in incorrect way
#correct code
[y for y in range(100) if (y % 2 == 0 and y % 5 == 0)]

(ii) The twice if statement is the error in this case.
# correct code
(y for y in range(100) if (y % 2 == 0 and y % 5 == 0 ))

Q16:
#correct code
["good" if i < 3 else "better" for i in range(6)]

No requirement of the colon in list comprehension.

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

You cannot copy content of this page