Python Sample QP PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Mock Exam | Diploma

Mock Exam | Diploma


Note to Learners
Question-1
Statement
Options
(a)
(b)
(c)
(d)
Answer
Common Data for (2), (3)
Statement
Question-2
Statement
Answer
Question-3
Statement
Options
(a)
(b)
Answer
Question-4
Statement
Options
(a)
(b)
(c)
(d)
Answer
Question-5
Statement
Answer
Question-6
Statement
Options
(a)
(b)
(c)
(d)
Answer
Common Data for (7), (8) and (9)
Statement
Question-7
Statement
Options
(a)
(b)
(c)
(d)
Answer
Question-8
Statement
Answer
Question-9
Statement
Answer
Common Data for (10), (11), (12)
Statement
Question-10
Statement
Options
(a)
(b)
Answer
Question-11
Statement
Answer
Question-12
Statement
Options
(a)
(b)
(c)
(d)
Answer
Question-13
Options
(a)
(b)
(c)
(d)
Answer
Question-14
Options
(a)
(b)
(c)
(d)
(e)
Answer
Question-15
Options
(a)
(b)
(c)
(d)
(e)
Answer
Question-16
Answer
Question-17
Options
(a)
(b)
Answer
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)
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
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)

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
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)
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.
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]
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?

1 def diff(P, Q):


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

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:

1 [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

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:

The dimensions of and are the same.


If the common dimensions are , the element in the row of both the matrices
are the same, for .

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)

1 def equality(A, B):


2    if len(A) != len(B):
3        return False
4    if len(A[0]) != len(B[0]):
5        return False
6    return True

(b)

1 def equality(A, B):


2    if len(A) != len(B):
3        return False
4    if len(A[0]) != len(B[0]):
5        return False
6    
7    m, n = len(A), len(A[0])
8    
9    is_equal = True
10    for i in range(m):
11        for j in range(n):
12            if A[i][j] != B[i][j]:
13                is_equal = False
14            else:
15                is_equal = True
16
17    return is_equal

(c)

1 def equality(A, B):


2    if len(A) != len(B):
3        return False
4    if len(A[0]) != len(B[0]):
5        return False
6    
7    m, n = len(A), len(A[0])
8    
9    is_equal = True
10    for i in range(m):
11        for j in range(n):
12            if A[i][j] != B[i][j]:
13                is_equal = False
14                break
15        if not is_equal:
16            break
17    
18    return is_equal

(d)

1 def equality(A, B):


2    if len(A) != len(B):
3        return False
4    if len(A[0]) != len(B[0]):
5        return False
6    
7    m, n = len(A), len(A[0])
8    
9    for i in range(m):
10        for j in range(n):
11            if A[i][j] != B[i][j]:
12                return False

(e)

1 def equality(A, B):


2    if len(A) != len(B):
3        return False
4    if len(A[0]) != len(B[0]):
5        return False
6    
7    m, n = len(A), len(A[0])
8    
9    for i in range(m):
10        for j in range(n):
11            if A[i][j] != B[i][j]:
12                return False
13    return True

Answer
(c), (e)
Question-16
What is the output of the following snippet of code? (NAT)

1 def exact_count(para, n):


2    D = dict()
3    for word in para.split(' '):
4        if word not in D:
5            D[word] = 0
6        D[word] += 1
7    for word in D:
8        if D[word] == n:
9            return 1
10    return 0
11
12 print(exact_count('good word many good works good real choice', 3))

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?

If word is a palindrome, then is_pal(word) always returns True .

Options
(a)

True

(b)

False

Answer
(b)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy