0% found this document useful (0 votes)
27 views22 pages

Mock Quiz-1 Solutions

IITM Python quiz1 mock

Uploaded by

an7alpha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views22 pages

Mock Quiz-1 Solutions

IITM Python quiz1 mock

Uploaded by

an7alpha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Mock | Quiz-1

Mock | Quiz-1
Note to Learners
Question-1
Statement
Options
(a)
(b)
(c)
(d)
Answer
Feedback
Common Data for (2), (3)
Statement
Question-2
Statement
Answer
Feedback
Question-3
Statement
Options
(a)
(b)
Answer
Feedback
Question-4
Statement
Options
(a)
(b)
(c)
(d)
Answer
Feedback
Question-5
Statement
Answer
Feedback
Question-6
Statement
Options
(a)
(b)
(c)
(d)
Answer
Feedback
Common Data for (7), (8) and (9)
Statement
Feedback
Question-7
Statement
Options
(a)
(b)
(c)
(d)
Answer
Question-8
Statement
Answer
Question-9
Statement
Answer
Common Data for (10), (11), (12)
Statement
Feedback
Question-10
Statement
Options
(a)
(b)
Answer
Question-11
Statement
Answer
Question-12
Statement
Options
(a)
(b)
(c)
(d)
Answer
Question-13
Statement
Options
(a)
(b)
(c)
(d)
Answer
Feedback
Question-14
Statement
Options
(a)
(b)
(c)
(d)
(e)
Answers
Feedback
Note to Learners
There are two types of blocks that you would see in most of the questions:

Code

1 for i in range(10):
2 if i % 2 == 0:
3 print(i)

Input or Output

1 0
2 2
3 4
4 6
5 8

In both the blocks, please note that the region to the left of the thin vertical line — | —
corresponds to line-numbers. Do not confuse the line numbers with the content of the code or
the input-output. Just to be clear:
Question-1
Statement
Consider the following snippet of code.

1 first_row = 'qwertyuiop'
2 second_row = 'asdfghjkl'
3 third_row = 'zxcvbnm'
4
5 word = input()
6 for char in word:
7 if char in first_row:
8 print('first_row', first_row.index(char))
9 elif char in second_row:
10 print('second_row', second_row.index(char))
11 elif char in third_row:
12 print('third_row', third_row.index(char))

What is the output of this code for the following input?

Input

1 brisk

Options
(a)

1 third_row 5
2 first_row 4
3 first_row 8
4 second_row 2
5 second_row 8

(b)

1 third_row 4
2 first_row 3
3 first_row 7
4 second_row 1
5 second_row 7

(c)

1 second_row 4
2 third_row 3
3 first_row 7
4 second_row 1
5 second_row 7
(d)

1 second_row 5
2 third_row 4
3 first_row 8
4 second_row 2
5 second_row 8

Answer
(b)

Feedback
For each letter, you have to see if it belongs to the first row, second row or the third row. These
rows are actually the letter-combinations that you find in a typical QWERTY keyboard!

1 'abc'.index('a') # this returns 0, the letter 'a' is at index 0 in the string


'abc'
2 'abc'.index('b') # this returns 1, the letter 'b' is at index 1 in the string
'abc'
3 'abc'.index('c') # this returns 2, the letter 'c' is at index 2 in the string
'abc'
Common Data for (2), (3)
Statement
Common data for questions (2) and (3)

Consider the following snippet of code. A , B and C are Boolean variables.

1 a, b, c = 0, 0, 0
2 if A:
3 a += 1
4 if B:
5 a += 1
6 b += 1
7 if C:
8 a += 1
9 b += 1
10 c += 1
11 print(a + b + c)
Question-2
Statement
What is the output of the above code snippet if all of A , B and C are True ? NAT

Answer
6

Feedback
If all three variables are True, then we enter the body of each of the if blocks. So, we have a =
3, b = 2, c = 1 .
Question-3
Statement
Is the following statement true or false?

The statement a >= b >= c will always evaluate to True . This is independent of the values of A ,
B and C .

Options
(a)

True

(b)

False

Answer
(a)

Feedback
Note that a is present and is incremented in all three blocks, b in two of the blocks and c in just
the final block. So, a >= b >= c will always evaluate to True.
Question-4
Statement
We wish to find the number of occurrences of the digit 1 in the string word and store it in the
variable count . Select all correct code snippets that achieve this. (MSQ)

Options
(a)

1 count = 0
2 for char in word:
3 if char == 1:
4 count += 1

(b)

1 count = 0
2 for char in word:
3 if char == '1':
4 count += 1

(c)

1 count = 0
2 for char in word:
3 if char == str(1):
4 count += 1

(d)

1 count = 1
2 for char in word:
3 if char == '1':
4 count += 1

Answer
(b), (c)

Feedback
The main point to note is this:

1 '1' == str(1)

char == 1 is wrong as we comparing a string and an integer which doesn't make sense. We
should be comparing two items of the same type.
Question-5
Statement
What will be the output of the following snippet of code? (NAT)

1 word = "This IS not SO hard AFTER all."


2 count = 0
3 for char in word:
4 if 'A' <= char < 'Z' and char in ['A', 'E', 'I', 'O', 'U']:
5 count += 1
6 print(count)

Answer
4

Feedback
We are counting the number of upper case vowels. In fact, the first part of the if-condition is
actually redundant and it can be removed.

1 word = "This IS not SO hard AFTER all."


2 count = 0
3 for char in word:
4 if char in ['A', 'E', 'I', 'O', 'U']:
5 count += 1
6 print(count)
Question-6
Statement
If is a positive integer, what is the output of the following code? Assume that natural numbers
start from 1, that is, 0 is not a natural number.

1 a = 0
2 for i in range(1, n + 1):
3 b = 1
4 for j in range(1, i + 1):
5 b = b * j
6 a = a + b
7 print(a)

Options
(a)

Sum of the first natural numbers

(b)

Product of the first natural numbers

(c)

Sum of the factorial of the first natural numbers

(d)

Factorial of the sum of the first natural numbers

Answer
(c)

Feedback
The inner loop computes the factorial of . This is stored in the variable b . Summing all such
factorials is done by the outer loop. The sum is stored in the variable a .
Common Data for (7), (8) and (9)
Statement
Common data for questions (7), (8) and (9)

In the following code-block, b is an integer that has already been defined.

1 x = 0
2 while True:
3 if x < 10:
4 x = int(input())
5 continue
6 elif b < x < b + 2:
7 break

The code block runs without throwing any error and terminates after accepting 10 inputs — all of
which are integers — from the user. Assume that all questions that follow are independent of
each other.

Feedback
For questions (7), (8) and (9):

Code

1 x = 0
2 while True:
3 if x < 10:
4 x = int(input())
5 continue
6 elif b < x < b + 2:
7 break

The loop terminates only when the input lies between b and b + 2 . As b and the input are
integers, the loop terminates only when the input is exactly equal to b + 1 .
If the input lies between and b , endpoints inclusive, or greater than or equal to b + 2 ,
then the loop never terminates.
For any input less than , the code will keep accepting input from the user.
As the code terminates after accepting inputs, the input has to be b + 1 .
This means, the first inputs have to be less than .
Question-7
Statement
If b is equal to , which of the following could have been one of the ten inputs entered by the
user? MSQ

Options
(a)

(b)

(c)

200

(d)

40

Answer

(a), (b)
Question-8
Statement
If b is equal to , what is the maximum possible value that the user could have entered among
the first nine inputs? (NAT)

Answer
9
Question-9
Statement
What is the value of b if the final input entered by the user is ? NAT

Answer
99
Common Data for (10), (11), (12)
Statement
Common Data for Questions (10), (11), (12)

Consider the following snippet of code.

1 L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2
3 S = 0
4 while len(L) >= 2:
5 S += L[0] + L[-1]
6 L = L[1:]
7 L = L[:-1]

Feedback
Line-5 adds the first and last elements in the list L .

Lines 6 and 7 shrink the list by discarding the first and last elements.

This is the sequence of values taken by the list L :

1 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # before entering the loop


2 [2, 3, 4, 5, 6, 7, 8, 9] # after first iteration
3 [3, 4, 5, 6, 7, 8] # after second iteration
4 [4, 5, 6, 7] # after third iteration
5 [5, 6] # after fourth iteration
6 [ ] # after fifth iteration
Question-10
Statement
Will this code run without any error?

Options
(a)

Yes

(b)

No

Answer
(a)
Question-11
Statement
What is the value of S at the end of execution of the code given above? NAT

Answer
55
Question-12
Statement
What is the value of L at the end of execution of the code given above?

Options
(a)

[ ]

(b)

[5, 6]

(c)

[5]

(d)

[6]

Answer
(a)
Question-13
Statement
What will be the output of the following snippet of code?

1 # Hint: 'useful;hint'.split(';') returns the list ['useful', 'hint']


2 P = 'this,is,a,chair'
3 Q = 'this,statement,is,true'
4 P_split = P.split(',')
5 Q_split = Q.split(',')
6
7 R = [ ]
8 for word in P_split:
9 if word not in Q_split:
10 R.append(word)
11
12 print(R)

Options
(a)

['a', 'chair']

(b)

['statement', 'true']

(c)

['this', 'is']

(d)

[]

Answer
(a)

Feedback

1 P.split(',') ---> ['this', 'is', 'a', 'chair'] ---> P_split


2 Q.split(',') ---> ['this', 'statement', 'is', 'true'] ---> Q_split

While P and Q are strings (sentences), P_split and Q_split are the list of words in those
sentences respectively. R is the list of words that are in P_split but not in Q_split .
Question-14
Statement
Let L be a matrix of numbers that has already been defined and populated. We wish to
find the sum of the elements in each column and store all such column-sums in a list called
colsum . Columns are indexed from 0 to m - 1 . For the column , colsum[j] should be the
sum of all elements in that column. Select all correct code snippets that achieve this. [MSQ].

Options
(a)

1 colsum = [ ]
2 m = len(L)
3 row, col = 0, 0
4 while col < m:
5 colsum.append(0)
6 while row < m:
7 colsum[col] += L[row][col]
8 row += 1

(b)

1 colsum = [ ]
2 m = len(L)
3 row, col = 0, 0
4 while col < m:
5 colsum.append(0)
6 while row < m:
7 colsum[col] += L[row][col]
8 row += 1
9 col += 1

(c)

1 colsum = [ ]
2 m = len(L)
3 row, col = 0, 0
4 while col < m:
5 row = 0
6 colsum.append(0)
7 while row < m:
8 colsum[col] += L[row][col]
9 row += 1
10 col += 1

(d)
1 colsum = [ ]
2 m = len(L)
3 for j in range(m):
4 val = 0
5 for i in range(m):
6 val = val + L[j][i]
7 colsum.append(val)

(e)

1 colsum = [ ]
2 m = len(L)
3 for j in range(m):
4 val = 0
5 for i in range(m):
6 val = val + L[i][j]
7 colsum.append(val)

Answers
(c), (e)

Feedback
Option-(a) is wrong for a couple of reasons, chief among which is that col is not being
incremented just outside the inner loop.
Option-(b) is also wrong. But it at least corrects the defect in option-(a) by introducing line-9
that increments c . But it is still incomplete as the variable row is not being initialized to 0
just before entering the inner loop.
Option-(c) is correct. It rectifies the error in option-(b) by initializing row to zero before
entering the inner loop.
Option-(d) is actually computing the row-sum and not the column sum.
Option-(e) is correct.

You might also like