You are on page 1of 8

Q-1 What Will Be The Output Of The Following Code ?

dict_value={“c”: 5,”a”: 4, “d”: 1}


for x in sorted(dict_value):
print(dict_value[x])

Q-2 What Will Be The Output Of The Following Code ?


Box={}
Jars={}
Crates={}
Box['biscuit']=1
Box['cake']=3
Jars['jam']=4
Crates['box']=Box
Crates['jar']=Jars
Print(len(Crates[Box]))

Q-3 What Will Be The Output Of The Following Code ?


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)
Q-4 What Will Be The Output Of The Following Code ?

my_dict = {}
my_dict[1] = 1
my_dict['1'] = 2
my_dict[1.0] = 4
sum = 0
for k in my_dict:
sum += my_dict[k]
print (sum)

Q-5 What Will Be The Output Of The Following Code ?


arr = {}
arr[1] = 1
arr['1'] = 2
arr[1] += 1
sum = 0
for k in arr:
sum += arr[k]
print (sum)

Q-6 What Will Be The Output Of The Following Code ?


a = {'a':1,'b':2,'c':3}
print (a['a','b'])

Q-7 What Will Be The Output Of The Following Code ?


a = {(1,2):1,(2,3):2}
print(a[1,2])
Q-8 What Will Be The Output Of The Following Code ?
init_tuple = ((1, 2),) * 7
print(len(init_tuple[3:8]))

Q-9 What Will Be The Output Of The Following Code ?


init_tuple = (1,) * 3
init_tuple[0] = 2
print(init_tuple)

Q-10 What Will Be The Output Of The Following Code ?


init_tuple = ('Python') * 3
print(type(init_tuple))

Q-11 What Will Be The Output Of The Following Code ?


init_tuple_a = '1', '2'
init_tuple_b = ('3', '4')
print (init_tuple_a + init_tuple_b)

Q-12 What Will Be The Output Of The Following Code ?

init_tuple_a = 'a', 'b'


init_tuple_b = ('a', 'b')
print (init_tuple_a == init_tuple_b)

Q-13 What Will Be The Output Of The Following Code ?


fruit_list1 = ['Apple', 'Berry', 'Cherry', 'Papaya']
fruit_list2 = fruit_list1
fruit_list3 = fruit_list1[:]
fruit_list2[0] = 'Guava'
fruit_list3[1] = 'Kiwi'
sum = 0
for ls in (fruit_list1, fruit_list2, fruit_list3):
if ls[0] == 'Guava':
sum += 1
if ls[1] == 'Kiwi':
sum += 20
print (sum)

Q-14 What Will Be The Output Of The Following Code ?


arr = [1, 2, 3, 4, 5, 6]
for i in range(1, 6):
arr[i - 1] = arr[i]
for i in range(0, 6):
print(arr[i])

Q-15 What Will Be The Output Of The Following Code ?


a=[1,2,3,4,5]
print(a[3:0:-1])

Q-16 What Will Be The Output Of The Following Code ?


a=[1,2,3,4,5,6,7,8,9]
a[::2]=10,20,30,40,50,60
print(a)

Q-17 What Will Be The Output Of The Following Code ?


a=[1,2,3,4,5,6,7,8,9]
print(a[::2])

Q-18 what gets printed? Assuming python version 2.x ?


print type(1/2)

Q-19 What Will Be The Output Of The Following Code ?


name = "snow storm"
name[5]='X'
print(name)

Q-20 What Will Be The Output Of The Following Code ?


for i in range(2):
print(i)
for i in range(4,6):
print(i)

Answers:
1-> 4
5
1
2->Type Error
3->30
{(1, 2): 12, (4, 2, 1): 10, (1, 2, 4): 8}
4->6
5->4
6->Key Error
7->1
8->4
9->TypeError: ‘tuple’ object does not support item assignment
10-><class ‘str’>
11->(‘1’, ‘2’, ‘3’, ‘4’)
12->True

13->22
14->2 3 4 5 6 6
15->[4,3,2]
16->ValueError: attempt to assign sequence of size 6 to extended slice of size 5
17->[1,3,5,7,9]

18-><type 'int'>

19->TypeError: 'str' object does not support item assignment


20->0 1 4 5

Miscellaneous :
Q-1 Write a Python program to get the key, value and item in a dictionary.
dict_num = {“a”: 10, “b”: 20, “c”: 30, “d”: 40, “e”: 50, “f”: 60}

key value
a 10
b 20
c 30
d 40
e 50
f 60

solution:
dict_num = {“a”: 10, “b”: 20, “c”: 30, “d”: 40, “e”: 50, “f”: 60}
print(' key'+” ”+”value”)
for i in dict_num:
print(str(i)+" "+str(dict_num[i]))

Q-2 Write a Python program to add a key to a dictionary.


{0: 10, 1: 20}
{0: 10, 1: 20, 2: 30}
d = {0:10, 1:20}
print(d)
d[2]=30
print(d)

Q-3 Write a Python program to check if a given key already exists in a dictionary.
Solution
d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
if x in d:
print('Key is present in the dictionary')
else:
print('Key is not present in the dictionary')

Q-4 Write python program to print following:


*
**
***
****
*****

solution
for i in range(5):
print("* "*(i+1))

Q-5 Write python program to print following:


*
**
***
****
*****

solution :
for i in range(5):
print(" "*(5-i)+"*"*(i+1))

You might also like