Data Structure - 1 : Linear List  || Type B || Sumita arora || Class 12 || Computer Science || Solution



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

Numlst = [2, 5, 1, 7, 3, 6, 8, 9]
Squst = NumLst * 2




Q2 Change the above code so that it works as stated in previous question.



Q3. Modify your previous code so that SqLst stores the doubled numbers in ascending order.



Q4. Consider a list ML = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write code 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.




Q5. Write equivalent list comprehension for the following code:

target1 = []
for number in source:
    if number & 1:
        target1.append(number)





Q6 Write equivalent for loop for the following list comprehension:

gen = [i/2 for i in [0, 9, 21, 32]]
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)




Q8. Predict the output:

def h_t(NLst):
    from_back = NLst.pop
    from_front = NLst.pop()
    NLst.append(from_front)
    NLst.insert(0, from_back)

NLst1 = [ [21, 12], 31 ]
NLst3 = NLst1.copy()
NLst2 = NLst1
NLst2[-1] = 5
NLst2.insert(1,6)
h_t(NLst1)
print(NLst1[0], NLst1[-1], len(NLst1))
print (NLst2[0], NLst2[-1], len(NLst2))
print (NLst3[0], NLst3[-1], len(NLst3))





Q9. Predict the output:

ages = [11, 14, 15, 17, 13, 18, 25]
    print(ages)
Elig = [x for x in ages if x in range(14, 18)]
print (Elig)




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)





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)




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)




Q13. Find the Error. Consider the following code, which runs correctly at times but gives error at other times. Find the error and its reason.


Lst1 = [23, 34, 12, 77, 34, 26, 28, 93, 48, 69, 73, 23, 19, 88]
Lst2 = []
print("List1 originally 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:
    print(Lst1.index(100))
    print(Lst2.index(100))
elif ch == 3:
    print (Lst1.pop())
    print(Lst2.pop()





Q14. Suggest the correction for the error(s) in previous question's code.



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)




Q16. Find the error in the following list comprehension:

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





Q17. Suggest corrections for the errors in both the previous questions.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post