Select Page
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

## Python MCQ Set 1

1. What will be the output?

1. `>>>m = [[x, x + 1, x + 2] for x in range(0, 3)]`

a) [[1, 2, 3], [4, 5, 6], [7, 8, 9]].
b) [[0, 1, 2], [1, 2, 3], [2, 3, 4]].
c) [1, 2, 3, 4, 5, 6, 7, 8, 9].
d) [0, 1, 2, 1, 2, 3, 2, 3, 4].

Answer: b [Reason:] Execute in the shell to verify.

2. How many elements are in m?

1. `m = [[x, y] for x in range(0, 4) for y in range(0, 4)]`

a) 8
b) 12
c) 16
d) 32

Answer: c [Reason:] Execute in the shell to verify.

3. What will be the output?

1. `values = [[3, 4, 5, 1], [33, 6, 1, 2]]`
2. ` `
3. `v = values[0][0]`
4. `for row in range(0, len(values)):`
5. `    for column in range(0, len(values[row])):`
6. `        if v < values[row][column]:`
7. `            v = values[row][column]`
8. ` `
9. `print(v)`

a) 3
b) 5
c) 6
d) 33

Answer: d [Reason:] Execute in the shell to verify.

4. What will be the output?

1. `values = [[3, 4, 5, 1], [33, 6, 1, 2]]`
2. ` `
3. `v = values[0][0]`
4. `for lst in values:`
5. `    for element in lst:`
6. `        if v > element:`
7. `            v = element`
8. ` `
9. `print(v)`

a) 1
b) 3
c) 5
d) 6

Answer: a [Reason:] Execute in the shell to verify.

5. What will be the output?

1. `values = [[3, 4, 5, 1 ], [33, 6, 1, 2]]`
2. ` `
3. `for row in values:`
4. `    row.sort()`
5. `    for element in row:`
6. `        print(element, end = " ")`
7. `    print()`

a) The program prints two rows 3 4 5 1 followed by 33 6 1 2
b) The program prints on row 3 4 5 1 33 6 1 2
c) The program prints two rows 3 4 5 1 followed by 33 6 1 2
d) The program prints two rows 1 3 4 5 followed by 1 2 6 33

Answer: d [Reason:] Execute in the shell to verify.

6. What is the output?

1. `matrix = [[1, 2, 3, 4],`
2. `       [4, 5, 6, 7],`
3. `       [8, 9, 10, 11],`
4. `       [12, 13, 14, 15]]`
5. ` `
6. `for i in range(0, 4):`
7. `    print(matrix[i][1], end = " ")`

a) 1 2 3 4
b) 4 5 6 7
c) 1 3 8 12
d) 2 5 9 13

Answer: d [Reason:] Execute in the shell to verify.

7. What will be the output?

1. `def m(list):`
2. `    v = list[0]`
3. `    for e in list:`
4. `      if v < e: v = e`
5. `    return v`
6. ` `
7. `values = [[3, 4, 5, 1], [33, 6, 1, 2]]`
8. ` `
9. `for row in values: `
10. `    print(m(row), end = " ")`

a) 3 33
b) 1 1
c) 5 6
d) 5 33

Answer: d [Reason:] Execute in the shell to verify.

8. What will be the output?

1. `data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]`
2. ` `
3. `print(data[1][0][0])`

a) 1
b) 2
c) 4
d) 5

Answer: d [Reason:] Execute in the shell to verify.

9. What will be the output?

1. `data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]`
2. ` `
3. `def ttt(m):`
4. `    v = m[0][0]`
5. ` `
6. `    for row in m:`
7. `        for element in row:`
8. `           if v < element: v = element`
9. ` `
10. `    return v`
11. ` `
12. `print(ttt(data[0]))`

a) 1
b) 2
c) 4
d) 5

Answer: c [Reason:] Execute in the shell to verify.

10. What will be the output?

1. `points = [[1, 2], [3, 1.5], [0.5, 0.5]]`
2. `points.sort()`
3. `print(points)`

a) [[1, 2], [3, 1.5], [0.5, 0.5]].
b) [[3, 1.5], [1, 2], [0.5, 0.5]].
c) [[0.5, 0.5], [1, 2], [3, 1.5]].
d) [[0.5, 0.5], [3, 1.5], [1, 2]].

Answer: c [Reason:] Execute in the shell to verify.

## Python MCQ Set 2

1. What is the output of the code shown?

```l=list('HELLO')
'first={0[0]}, third={0[2]}'.format(l)```

a) ‘first=H, third=L’
b) ‘first=0, third=2’
c) Error
d) ‘first=0, third=L’

Answer: a [Reason:] In the code shown above, the value for first is substituted by l[0], that is H and the value for third is substituted by l[2], that is L. Hence the output of the code shown above is: ‘first=H, third=L’. The list l= [‘H’, ‘E’, ‘L’, ‘L’, ‘O’].

2. What is the output of the code shown below?

```l=list('HELLO')
p=l[0], l[-1], l[1:3]
'a={0}, b={1}, c={2}'.format(*p)```

a) Error
b) “a=’H’, b=’O’, c=(E, L)”
c) “a=H, b=O, c=[‘E’, ‘L’]”
d) Junk value

Answer: c [Reason:] In the code shown above, the value for a is substituted by l[0], that is ‘H’, the value of b is substituted by l[-1], that is ‘O’ and the value for c is substituted by l[1:3]. Here the use of *p is to unpack a tuple’s items into individual function arguments.

3. Fill in the blanks:
The formatting method {1:<10} represents the ___________ positional argument, _________ justified in a 10 character wide field.
a) first, right
b) second, left
c) first, left
d) second, right

Answer: b [Reason:] The formatting method {1:<10} represents the second positional argument, left justified in a 10 character wide field.

4. What is the output of the following code?

`hex(255), int('FF', 16), 0xFF`

a) [0xFF, 255, 16, 255]
b) (‘0xff’, 155, 16, 255)
c) Error
d) (‘0xff’, 255, 255)

Answer: d [Reason:] The code shown above converts the value 255 into hexadecimal, that is, 0xff. The value ‘FF’ into integer. Hence the output of the code shown is: (‘0xff’, 255, 255).

5. The output of the two codes shown below is the same. State whether this statement is true or false.

``` bin((2**16)-1)
'{}'.format(bin((2**16)-1))```

a) True
b) False

Answer: a [Reason:] The output of both of the codes shown above is ‘0b1111111111111111’. Hence the statement is true.

6. What is the output of the code shown below?

`'{a}{b}{a}'.format(a='hello', b='world')`

a) ‘hello world’
b) ‘hello’ ‘world’ ‘hello’
c) ‘helloworldhello’
d) ‘hello’ ‘hello’ ‘world’

Answer: c [Reason:] The code shown above prints the values substituted for a, b, a, in the same order. This operation is performed using the format function. Hence the output of the code is: ‘helloworldhello’.

7. What is the output of the code shown below?

`'The {} side {1} {2}'.format('bright', 'of', 'life')`

a) Error
b) ‘The bright side of life’
c) ‘The {bright} side {of} {life}’
d) No output

Answer: a [Reason:] The code shown above results in an error. This is because we have switched from automatic field numbering to manual field numbering, that is, from {} to {1}. Hence this code results in an error.

8. The output of the code shown below is:

`'{0:f}, {1:2f}, {2:05.2f}'.format(1.23456, 1.23456, 1.23456)`

a) Error
b) ‘1.234560, 1.22345, 1.23’
c) No output
d) ‘1.234560, 1.234560, 01.23’

Answer: d [Reason:] In the code shown above, various formatting options are displayed using the format option. Hence the output of this code is: ‘1.234560, 1.234560, 01.23’

9. What is the output of the code shown below?

`'%.2f%s' % (1.2345, 99)`

a) ‘1.2345’, ‘99’
b) ‘1.2399’
c) ‘1.234599’
d) 1.23, 99

Answer: b [Reason:] In this code, we must notice that since multiple values haven been given, they should be enclosed in a tuple. Since the formatting format is %.2f, the value 1.2345 is reduced to two decimal places. Hence the output of the code shown above: ‘1.2399’.

11. What is the output of the code shown below?

`'%s' %((1.23,),)`

a) ‘(1.23,)’
b) 1.23,
c) (,1.23)
d) ‘1.23’

Answer: a [Reason:] The formatting expression accepts either a single substitution value, or a tuple of one or more items. Since single item can be given either by itself or within the tuple, a tuple to be formatted must be provided as a tested tuple. Hence the output of the code is: >>> ‘%s’ %((1.23,),).

12. What is the output of the two codes shown below?

```'{0}'.format(4.56)
'{0}'.format([4.56,])```

a) ‘4.56’, ‘4.56,’
b) ‘4.56’, ‘[4.56]’
c) 4.56, [4.56,]
d) 4.56, [4.56,]

Answer: b [Reason:] The code shown above shows the formatting option on the same value, that is 4.56, where in the second case, the value is enclosed in a list. Hence the output of the code shown above is: ‘4.56’, ‘[4.56]’

## Python MCQ Set 3

1. What is the result of the snippet of code shown below if x=1?

`x<<2`

a) 8
b) 1
c) 2
d) 4

Answer: d [Reason:] The binary form of 1 is 0001. The expression x<<2 implies we are performing bitwise left shift on x. This shift yields the value: 0100, which is the binary form of the number 4.

2. The output of the expression is:

`bin(29)`

a) ‘0b10111’
b) ‘0b11101’
c) ‘0b11111’
d) ‘0b11011’

Answer: b [Reason:] The binary form of the number 29 is 11101. Hence the output of this expression is ‘0b11101’.

3. What is the value of x if:

`x>>2=2`

a) 8
b) 4
c) 2
d) 1

Answer: a [Reason:] When the value of x is equal to 8 (1000), then x>>2 (bitwise right shift) yields the value 0010, which is equal to 2. Hence the value of x is 8.

4. What is the result of the expression:

`int(1011)?`

a) 1011
b) 11
c) 13
c) 1101

Answer: a [Reason:] The result of the expression shown will be 1011. This is because we have not specified the base in this expression. Hence it automatically takes the base as 10.

5. To find the decimal value of 1111, that is 15, we can use the function:
a) int(1111,10)
b) int(‘1111’,10)
c) int(1111,2)
d) int(‘1111’,2)

Answer: d [Reason:] The expression int(‘1111’,2) gives the result 15. The expression int(‘1111’, 10) will give the result 1111.

6. What is the result of the expression if x=15 and y=12:

`x & y`

a) b1101
b) 0b1101
c) 12
d) 1101

Answer: c [Reason:] The symbol ‘&’ represents bitwise AND. This gives 1 if both the bits are equal to 1, else it gives 0. The binary form of 15 is 1111 and that of 12 is 1100. Hence on performing the bitwise AND operation, we get 1100, which is equal to 12.

7. Which of the following expressions results in an error?
a) int(1011)
b) int(‘1011’,23)
c) int(1011,2)
d) int(‘1011’)

Answer: c [Reason:] The expression int(1011,2) results in an error. Had we written this expression as int(‘1011’,2), then there would not be an error.

8. Which of the following represents the bitwise XOR operator?
a) &
b) ^
c) |
d) !

Answer: b [Reason:] The ^ operator represent bitwise XOR operation. &: bitwise AND, | : bitwise OR and ! represents bitwise NOT.

9. What is the value of this expression?

`bin(0x8)`

a) ‘0bx1000’
b) 8
c) 1000
d) ‘0b1000’

Answer: d [Reason:] The prefix 0x specifies that the value is hexadecimal in nature. When we convert this hexadecimal value to binary form, we get the result as: ‘0b1000’.

10. What is the result of the expression:

`0x35 | 0x75`

a) 115
b) 116
c) 117
d) 118

Answer: c [Reason:] The binary value of 0x35 is 110101 and that of 0x75 is 1110101. On OR-ing these two values we get the output as: 1110101, which is equal to 117. Hence the result of the above expression is 117.

## Python MCQ Set 4

1. It is not possible for the two’s complement value to be equal to the original value in any case. State whether this statement is true or false.
a) True
b) False

Answer: b [Reason:] In most cases the value of two’s complement is different from the original value. However, there are cases in which the two’s complement value may be equal to the original value. For example, the two’s complement of 10000000 is also equal to 10000000. Hence the statement is false.

2. The one’s complement of 110010101 is:
a) 001101010
b) 110010101
c) 001101011
d) 110010100

Answer: a [Reason:] The one’s complement of a value is obtained by simply changing all the 1’s to 0’s and all the 0’s to 1’s. Hence the one’s complement of 110010101 is 001101010.

3. Bitwise _________ gives 1 if either of the bits is 1 and 0 when both of the bits are 1.
a) OR
b) AND
c) XOR
d) NOT

Answer: c [Reason:] Bitwise XOR gives 1 if either of the bits is 1 and 0 when both of the bits are 1.

4. The result of the expression shown below is:

`4^12`

a) 2
b) 4
c) 8
d) 12

Answer: c [Reason:] ^ is the XOR operator. The binary form of 4 is 0100 and that of 12 is 1100. Therefore, 0100^1100 is 1000, which is equal to 8.

5. Any odd number on being AND-ed with ________ always gives 1. Hint: Any even number on being AND-ed with this value always gives 0.
a) 10
b) 2
c) 1
d) 0

Answer: c [Reason:] Any odd number on being AND-ed with 1 always gives 1. Any even number on being AND-ed with this value always gives 0.

6. What is the value of this expression:

` bin(10-2)+bin(12^4)`

a) 0b10000
b) 0b10001000
c) 0b1000b1000
d) 0b10000b1000

Answer: d [Reason:] The output of bin(10-2)= 0b1000 and that of bin(12^4) is ob1000. Hence the output of the above expression is: 0b10000b1000.

7. Which of the following expressions can be used to multiply a given number ‘a’ by 4?
a) a<<2
b) a<<4
c) a>>2
d) a>>4

Answer: a [Reason:] Let us consider an example wherein a=2. The binary form of 2 is 0010. When we left shift this value by 2, we get 1000, the value of which is 16. Hence if we want to multiply a given number ‘a’ by 4, we can use the expression: a<<2.

8. What is the output of the code show below if a=10 and b =20?

```a=10
b=20
a=a^b
b=a^b
a=a^b
print(a,b)```

a) 10 20
b) 10 10
c) 20 10
d) 20 20

Answer: c [Reason:] The code shown above is used to swap the contents of two memory locations using bitwise X0R operator. Hence the output of the code shown above is: 20 10.

9. What is the two’s complement of -44?
a) 1011011
b) 11010100
c) 11101011
d) 10110011

Answer: b [Reason:] The binary form of -44 is 00101100. The one’s complement of this value is 11010011. On adding one to this we get: 11010100 (two’s complement).

10. What is the value of the expression:

`~100?`

a) 101
b) -101
c) 100
d) -100

Answer: b [Reason:] Suppose we have an expression ~A. This is evaluated as: -A – 1. Therefore, the expression ~100 is evaluated as -100 – 1, which is equal to -101.

## Python MCQ Set 5

1. The output of the snippet of code shown below?

```bool(‘False’)
bool()```

a) True
True
b) False
True
c) False
False
d) True
False

Answer: d [Reason:] The Boolean function returns true if the argument passed to the bool function does not amount to zero. In the first example, the string ‘False’ is passed to the function bool. This does not amount to zero and hence the output is true. In the second function, an empty list is passed to the function bool. Hence the output is false.

2. What is the output of the snippet of code shown below?

`['hello', 'morning'][bool('')]`

a) error
b) no output
c) hello
d) morning

Answer: c [Reason:] The line of code shown above can be simplified to state that ‘hello’ should be printed if the argument passed to the Boolean function amounts to zero, else ‘morning’ will be printed.

3. What is the output of the code shown below?

```not(3>4)
not(1&1)```

a) True
True
b) True
False
c) False
True
d) False
False

Answer: b [Reason:] The function not returns true if the argument amounts to false, and false if the argument amounts to true. Hence the first function returns false, and the second function returns false.

4. What is the output of the code shown?

`['f', 't'][bool('spam')]`

a) t
b) f
c) No output
d) Error

Answer: a [Reason:] The line of code can be translated to state that ‘f’ is printed if the argument passed to the Boolean function amount to zero. Else ‘t’ is printed. The argument given to the Boolean function in the above case is ‘spam’, which does not amount to zero. Hence the output is: t.

5. What is the output of the code shown below?

```l=[1, 0, 2, 0, 'hello', '', []]
list(filter(bool, l))```

a) Error
b) [1, 0, 2, 0, ‘hello’, ”, []]
c) [1, 0, 2, ‘hello’, ”, []]
d) [1, 2, ‘hello’]

Answer: d [Reason:] The code shown above returns a new list containing only those elements of the list l which do not amount to zero. Hence the output is: [1, 2, ‘hello’]

6. What is the output of the following code if the system date is 21st June, 2017 (Wednesday)?
[ ] or {}
{} or [ ]
a) [ ]
{}
b) [ ]
[ ]
c) {}
[ ]
d) {}
{}

Answer: c [Reason:] The code shown above shows two functions. In both the cases the right operand is returned. This is because each function is evaluated from left to right. Since the left operand is false, it is assumed that the right operand must be true and hence the right operand is returned in each of the above case.

7. What is the output of the code shown below?

```class Truth:
pass
x=Truth()
bool(x)```

a) pass
b) true
c) false
d) error

Answer: b [Reason:] If the truth method is not defined, the object is considered true. Hence the output of the code shown above is true.

8. What is the output of the code shown below?

```if (9 < 0) and (0 < -9):
print("hello")
elif (9 > 0) or False:
print("good")
else:

a) error
b) hello
c) good

Answer: c [Reason:] The code shown above prints the appropriate option depending on the conditions given. The condition which matches is (9>0), and hence the output is: good.

9. Which of the following Boolean expressions is not logically equivalent to the other three?
a) not(-6<0 or-6>10)
b) -6>=0 and -6<=10
c) not(-6<10 or-6==10)
d) not(-6>10 or-6==10)

Answer: d [Reason:] The expression not(-6<0 or -6>10) returns the output False. The expression -6>=0 and -6<=10 returns the output False. The expression not(-6<10 or -6==10) returns the output False. The expression not(-6>10 or -6==10) returns the output True.

10. The output of the line of code shown below is:

`not(10<20) and not(10>30)`

a) True
b) False
c) Error
d) No output