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

Here is class 12 computer science Unit 2 [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 2 and for all chapters here.

Watch all tutorials for chapter 2: Python Revision Tour.

Q1: What will be the output produced by the following code fragments?
Chapter 2 Type-B Sumita Arora
Question 1

Solution:

(a) Output:
hellohellohello 123
10 helloworld
(b) Output:
h :
e :
l :
l :
o :
t :
o :
:
P :
y :
t :
h :
o :
n :
w :
o :
r :
l :
d :
(c) Output:
he hello wor ld
w ll
llo wo or
Q2: Write a short Python code segment that adds up the lengths of all the words in a list and then prints the average (mean) length. Use the final list from the previous question to test your program.
x=["hello", "world", "python", "a", "symbol"]
add =0
for ch in x:
    add +=len(ch)
avg_len =add/len(x)
print(avg_len)
Q3: Predict the output of the following code snippet?

a=[1,2,3,4,5]
print(a[3:0:-1])
Output:
[4, 3, 2]
Q4: Predict the output of the following code snippet?
Chapter 2 Type-B Sumita Arora
Question 4

Solution:

(a) Output:

2 3 4 5 6 6

(b) Output:

1 #
1 # 2 #
1 # 2 # 3 #
Q5: Find the errors. State reasons.
Chapter 2 Type-B Sumita Arora
Question 5

Solution:

(a) tuples are immutable. Hence, t[0]=6 will cause error.

(b) The command in this case will run as the list are mutable.

(c) Error will rise as the index is out of range.

(d) Strings are immutable. Hence, t[0]="H" will cause error.

(e) Syntax error will cause error as absence of ":", "If".
Q6: Assuming words is a valid list of words, the program below tries to print the list in reverse. Does it have an error? If so, why? (Hint There are two problems with the code.)

for i in range(len(words), 0, -1):
print(words[i], end=” “)
Yes, it will show as because out of index. In first case reading len(words) will return value out of index of list words.
Q7: What would be the output of following code if ntp1 = (“Hello”, “Nita”, “How’s”, “life ?”) ?

(a,b,c,d) =ntp1
print(“a is :”, a)
print(“b is :”, b)
print(“c is :”, c)
print(“d is :”, d)
ntpl1 = (a,b,c,d)
print(ntp1[0][0]+ntp1[1][1]+ntp1[1])
a is : Hello
b is : Nita
c is : How’s
d is : life ?
HiNita
Q8: What will be the output of the following code?

tuple_a = ‘a’, ‘b’
tuple_b = (‘a’, ‘b’)
print(tuple_a == tuple_b)

(a) 0 (b) 1 (c) False (d) True
(d) True
Q9: What will be the output of the following code snippet?

rec = {“Name”:”Python”, “Age”:”20″, “Addr”:”NJ”, “Country”:”USA”}
id1 =id(rec)
del rec
rec = {“Name”:”Python”, “Age”:”20″, “Addr”:”NJ”, “Country”:”USA”}
id2 =id(rec)
print(id1==id2)

(a) True (b) False (c) 1 (d) Exception
(a) True
Q10: What will be the output of the following code snippet?

my_dict = {}
my_dict[(1,2,4)]=8
my_dict[(4,2,1)]=10
my_dict[(1,2)]=12
sum=0

for k in my_dict:
sum+=my_dict[k]

print(sum)
print(my_dict)
30
{(1, 2, 4): 8, (4, 2, 1): 10, (1, 2): 12}
Q11: Write a method in python to display the elements of list thrice if it is a number and display the element terminated with ‘#’ if it is not a number.

This List = [’41’, ‘DROND’, ‘GIRIRAJ’, ’13’, ‘ZARA’]
for i in List:
     if i.isnumeric() == True:
        print(i*3)
     else:
        print(i+'#')
Q12: Name the function/method required to:
(i) check if string contain only uppercase letter
(ii) given the total length of the list
(i) string_name.upper()
(ii) len(string_name)

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