Python Sample QP PDF
Python Sample QP PDF
Python Sample QP PDF
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))
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)
Common Data for (2), (3)
Statement
Common data for questions (2) and (3)
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
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)
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)
Question-5
Statement
What will be the output of the following snippet of code? (NAT)
Answer
4
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)
(b)
(c)
(d)
Answer
(c)
Common Data for (7), (8) and (9)
Statement
Common data for questions (7), (8) and (9)
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.
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)
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]
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
What will be the output of the following snippet of code?
Options
(a)
['a', 'chair']
(b)
['statement', 'true']
(c)
['this', 'is']
(d)
[]
Answer
(a)
Question-14
A matrix is represented as a list of lists. For example:
is represented as:
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)
Answer
(c), (e)
Question-15
Two matrices and are said to be equal only if both the conditions given below are satisfied:
Write a function named equality that accepts two matrices A and B as arguments. It should
return True if the two matrices are equal and False otherwise. Select all correct
implementations of the function. (MSQ)
Options
(a)
(b)
(c)
(d)
(e)
Answer
(c), (e)
Question-16
What is the output of the following snippet of code? (NAT)
Answer
1
Question-17
Consider the following function.
1 def is_pal(S):
2 if len(S) == 1:
3 return True
4 if S[0] == S[-1]:
5 return is_pal(S[1 : -1])
6 else:
7 return False
word is a non-empty string that has already been defined. Is the following statement true or
false?
Options
(a)
True
(b)
False
Answer
(b)