WorksheetsPython Programming Quiz-03
Total questions: 23
Worksheet time: 25mins
Q-1. What will be the output of the following Python code?
a=[1,2,3,4,5,6,7,8,9]
print(a[::2])
[1,2]
[8,9]
[1,3,5,7,9]
[1,2,3]
Q-2. The code below manipulates a Python list using the slicing operator. What will be the output?
a=[1,2,3,4,5,6,7,8,9]
a[::2]=10,20,30,40,50,60
print(a)
ValueError: Attempt to assign a sequence of size 6 to an extended slice of size 5
[10, 2, 20, 4, 30, 6, 40, 8, 50, 60]
[1, 2, 10, 20, 30, 40, 50, 60]
[1, 10, 3, 20, 5, 30, 7, 40, 9, 50, 60]
Q-5. What is the correct command to shuffle the given Python list?
fruit=['apple', 'banana', 'papaya', 'cherry']
fruit.shuffle()
shuffle(fruit)
random.shuffle(fruit)
random.shuffleList(fruit)
The code below uses a Python list and iterates using the range() function. What will be the output?
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], end = " ")
1 2 3 4 5 6
2 3 4 5 6 1
1 1 2 3 4 5
2 3 4 5 6 6
Draw the flow chart diagram to print the swap two numbers without using intermediate/
temporary variables and also take the input from users.

Make the use of a python program that accepts a sequence of
lines as input and prints the line after making all characters in the
sentence capitalized.
Input: “Hello GL BAJAJ..
Now we start to learn Python Programming Language”.
Output:
HELLO GL BAJAJ..
NOW WE START TO LEARN PYTHON PROGRAMMING
LANGUAGE.

What will be the output of the following code snippet?
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)
22
21
0
43
What will be the output of the following Python coding snippet?
init_tuple_a = 'a', 'b'
init_tuple_b = ('a', 'b')
print (init_tuple_a == init_tuple_b)
0
1
false
true
Which of the following statements given below is/are true about a Python tuple?
Tuples have structure, and lists have an order.
Tuples are homogeneous, and lists are heterogeneous.
Tuples are immutable, and lists are mutable.
All of them.
What will be the output of the following Python code?
init_tuple = ((1, 2),) * 7
print(len(init_tuple[3:8]))
Exception
5
4
None
What is the output of the below Python code?
a = {(1,2):1,(2,3):2}
print(a[1,2])
Key Error
1
{(2,3):2}
{(1,2):1}
What will be the output of the following Python coding snippet?
a = {'a':1,'b':2,'c':3}
print (a['a','b'])
Key Error
[1,2]
{‘a’:1,’b’:2}
(1,2)
The code below has a Python function writing to a dictionary. What will it print in the end?
fruit = {}
def addone(index):
if index in fruit:
fruit[index] += 1
else:
fruit[index] = 1
addone('Apple')
addone('Banana')
addone('apple')
print (len(fruit))
1
2
3
4
What will be the output of the following Python coding snippet?
arr = {}
arr[1] = 1
arr['1'] = 2
arr[1] += 1
sum = 0
for k in arr:
sum += arr[k]
print (sum)
1
2
3
4
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)
Syntax error
30
{(1, 2): 12, (4, 2, 1): 10, (1, 2, 4): 8}
47
{(1, 2): 12, (4, 2, 1): 10, (1, 2, 4): 8}
30
{[1, 2]: 12, [4, 2, 1]: 10, [1, 2, 4]: 8}
What will the following Python dictionary code print in its result?
box = {}
jars = {}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
jars['jam'] = 4
crates['box'] = box
crates['jars'] = jars
print (len(crates[box]))
1
3
4
Type Error
What will be the result of the below Python code?
rec = {"Name" : "Python Programmer", "Age":"20", "Addr" : "NJ", "Country" : "USA"}
id1 = id(rec)
del rec
rec = {"Name" : "Python Programmer", "Age":"20", "Addr" : "NJ", "Country" : "USA"}
id2 = id(rec)
print(id1 == id2)
TRUE
FALSE
1
EXEPTION
Which of the following concepts is the most relevant in Python?
function invocation in Python
pass-by-value in Python
pass by object reference in Python
What is the output of the following Python code? Will it behave differently from the previous question?
def test(x = 1, y = 2):
x = x + y
y += 1
print(x, y)
test(2, 1)
1 3
2 3
3 2
3 3
Which of the following function calls can be used to invoke the below function definition?
def test(a, b, c, d)
test(1, 2, 3, 4)
test(a = 1, 2, 3, 4)
test(a = 1, b = 2, c = 3, 4)
test(a = 1, b = 2, c = 3, d = 4)
test(1, 2, 3, d = 4)
What is the output of the following code snippet?
def test(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
test(3, 7)
test(25, c = 24)
test(c = 50, a = 100)
a is 7 and b is 3 and c is 10
a is 25 and b is 5 and c is 24
a is 5 and b is 100 and c is 50
a is 3 and b is 7 and c is 10
a is 5 and b is 25 and c is 24
a is 50 and b is 100 and c is 5
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
None of the above
What is the output of the following code snippet?
def func(x = 1, y = 2):
return x + y, x - y
x, y = func(y = 2, x = 1)
print(x, y)
1 3
3 1
The program has a runtime error because the function returns the multiple values
3 -1
-1 3
What is the order of usage of args, *kwargs, and formal args in the function header?
some_func(formal_args, args, *kwargs)
some_func(**kwargs, *args, formal_args)
some_func(*args, **kwargs, formal_args)
some_func(*args, formal_args, **kwargs)
