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 is the output of the following?

`print("Hello {name1} and {name2}".format(name1='foo', name2='bin'))`

a) Hello foo and bin
b) Hello {name1} and {name2}
c) Error
d) Hello and

Answer: a [Reason:] The arguments are accessed by their names.

2. What is the output of the following?

`print("Hello {0!r} and {0!s}".format('foo', 'bin'))`

a) Hello foo and foo
b) Hello ‘foo’ and foo
c) Hello foo and ‘bin’
d) Error

Answer: b [Reason:] !r causes the characters ‘ or ” to be printed as well.

3. What is the output of the following?

`print("Hello {0} and {1}".format(('foo', 'bin')))`

a) Hello foo and bin
b) Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
c) Error
d) None of the mentioned

Answer: c [Reason:] IndexError, the tuple index is out of range.

4. What is the output of the following?

`print("Hello {0[0]} and {0[1]}".format(('foo', 'bin')))`

a) Hello foo and bin
b) Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
c) Error
d) None of the mentioned

Answer: a [Reason:] The elements of the tuple are accessed by their indices.

5. What is the output of the following?

`print('The sum of {0} and {1} is {2}'.format(2, 10, 12))`

a) The sum of 2 and 10 is 12
b) Error
c) The sum of 0 and 1 is 2
d) None of the mentioned

Answer: a [Reason:] The arguments passed to the function format can be integers also.

6. What is the output of the following?

`print('The sum of {0:b} and {1:x} is {2:o}'.format(2, 10, 12))`

a) The sum of 2 and 10 is 12
b) The sum of 10 and a is 14
c) The sum of 10 and a is c
d) Error

Answer: b [Reason:] 2 is converted to binary, 10 to hexadecimal and 12 to octal.

7. What is the output of the following?

`print('{:,}'.format(1112223334))`

a) 1,112,223,334
b) 111,222,333,4
c) 1112223334
d) Error

Answer: a [Reason:] A comma is added after every third digit from the right.

8. What is the output of the following?

`print('{:,}'.format('1112223334'))`

a) 1,112,223,334
b) 111,222,333,4
c) 1112223334
d) Error

Answer: d [Reason:] An integer is expected.

9. What is the output of the following?

`print('{:\$}'.format(1112223334))`

a) 1,112,223,334
b) 111,222,333,4
c) 1112223334
d) Error

Answer: d [Reason:] \$ is an invalid format code.

10. What is the output of the following?

`print('{:#}'.format(1112223334))`

a) 1,112,223,334
b) 111,222,333,4
c) 1112223334
d) Error

Answer: c [Reason:] The number is printed as it is.

## Python MCQ Set 2

1. What is returned by math.ceil(3.4)?
a) 3
b) 4
c) 4.0
d) 3.0

Answer: b [Reason:] The ceil function returns the smallest integer that is bigger than or equal to the number itself.

2. What is the value returned by math.floor(3.4)?
a) 3
b) 4
c) 4.0
d) 3.0

Answer: a [Reason:] The floor function returns the biggest number that is smaller than or equal to the number itself.

3. What is the output of print(math.copysign(3, -1))?
a) 1
b) 1.0
c) -3
d) -3.0

Answer: d [Reason:] The copysign function returns a float whose absolute value is that of the first argument and the sign is that of the second argument.

4. What is displayed on executing print(math.fabs(-3.4))?
a) -3.4
b) 3.4
c) 3
d) -3

Answer: b [Reason:] A negative floating point number is returned as a positive floating point number.

5. Is the output of the function abs() the same as that of the function math.fabs()?
a) sometimes
b) always
c) never
d) none of the mentioned

Answer: a [Reason:] math.fabs() always returns a float and does not work with complex numbers whereas the return type of abs() is determined by the type of value that is passed to it.

6. What is the value returned by math.fact(6)?
a) 720
b) 6
c) [1, 2, 3, 6].
d) error

Answer: d [Reason:] NameError, fact() is not defined.

7. What is the value of x if x = math.factorial(0)?
a) 0
b) 1
c) error
d) none of the mentioned

Answer: b [Reason:] Factorial of 0 is 1.

8. What is math.factorial(4.0)?
a) 24
b) 1
c) error
d) none of the mentioned

Answer: a [Reason:] The factorial of 4 is returned.

9. What is the output of print(math.factorial(4.5))?
a) 24
b) 120
c) error
d) 24.0

Answer: c [Reason:] Factorial is only defined for non-negative integers.

10. What is math.floor(0o10)?
a) 8
b) 10
c) 0
d) 9

Answer: a [Reason:] 0o10 is 8 and floor(8) is 8.

## Python MCQ Set 3

1. What will be the output?

1. `def f(i, values = []):`
2. `    values.append(i)`
3. `    return values`
4. ` `
5. `f(1)`
6. `f(2)`
7. `v = f(3)`
8. `print(v)`

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

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

2. What will be the output?

1. `names1 = ['Amir', 'Bala', 'Chales']`
2. ` `
3. `if 'amir' in names1:`
4. `    print(1)`
5. `else:`
6. `    print(2)`

a) None
b) 1
c) 2
d) Error

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

3. What will be the output?

1. `names1 = ['Amir', 'Bala', 'Charlie']`
2. `names2 = [name.lower() for name in names1]`
3. ` `
4. `print(names2[2][0])`

a) None
b) a
c) b
d) c

Answer: d [Reason:] List Comprehension are a shorthand for creating new lists.

4. What will be the output?

1. `numbers = [1, 2, 3, 4]`
2. ` `
3. `numbers.append([5,6,7,8])`
4. ` `
5. `print(len(numbers))`

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

Answer: b [Reason:] A list is passed in append so the length is 5.

5. To which of the following the “in” operator can be used to check if an item is in it?
a) Lists
b) Dictionary
c) Set
d) All of the mentioned

Answer: d [Reason:] In can be used in all data structures.

6. What will be the output?

1. `list1 = [1, 2, 3, 4]`
2. `list2 = [5, 6, 7, 8]`
3. ` `
4. `print(len(list1 + list2))`

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

Answer: d [Reason:] + appends all the elements individually into a new list.

7. What will be the output?

1. `def addItem(listParam):`
2. `    listParam += [1]`
3. ` `
4. `mylist = [1, 2, 3, 4]`
5. `addItem(mylist)`
6. `print(len(mylist))`

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

Answer: c [Reason:] + will append the element to the list.

8. What will be the output?

1. `def increment_items(L, increment):`
2. `    i = 0`
3. `    while i < len(L):`
4. `        L[i] = L[i] + increment`
5. `        i = i + 1`
6. ` `
7. `values = [1, 2, 3]`
8. `print(increment_items(values, 2))`
9. `print(values)`

a) None
[3, 4, 5].
b) None
[1, 2, 3].
c) [3, 4, 5].
[1, 2, 3].
d) [3, 4, 5].
None

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

9. What will be the output?

1. `def example(L):`
2. `    ''' (list) -> list`
3. `    '''`
4. `    i = 0`
5. `    result = []`
6. `    while i < len(L):`
7. `        result.append(L[i])`
8. `        i = i + 3`
9. `    return result`

a) Return a list containing every third item from L starting at index 0
b) Return an empty list
c) Return a list containing every third index from L starting at index 0
d) Return a list containing the items from L starting from index 0, omitting every third item

Answer: a [Reason:] Run the code to get a better understanding with many arguements.

10. What will be the output?

1. `veggies = ['carrot', 'broccoli', 'potato', 'asparagus']`
2. `veggies.insert(veggies.index('broccoli'), 'celery')`
3. `print(veggies)`

a) [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’] Correct 1.00
b) [‘carrot’, ‘celery’, ‘potato’, ‘asparagus’].
c) [‘carrot’, ‘broccoli’, ‘celery’, ‘potato’, ‘asparagus’].
d) [‘celery’, ‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’].

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

## Python MCQ Set 4

1. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse() ?
a) [3, 4, 5, 20, 5, 25, 1, 3].
b) [1, 3, 3, 4, 5, 5, 20, 25].
c) [25, 20, 5, 5, 4, 3, 3, 1].
d) [3, 1, 25, 5, 20, 5, 4, 3].

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

2. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34, 5]) ?
a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5].
b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5].
c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5].
d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5].

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

3. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop(1) ?
a) [3, 4, 5, 20, 5, 25, 1, 3].
b) [1, 3, 3, 4, 5, 5, 20, 25].
c) [3, 5, 20, 5, 25, 1, 3].
d) [1, 3, 4, 5, 20, 5, 25].

Answer: c [Reason:] pop() removes the element at the position specified in the parameter.

4. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop()?
a) [3, 4, 5, 20, 5, 25, 1].
b) [1, 3, 3, 4, 5, 5, 20, 25].
c) [3, 5, 20, 5, 25, 1, 3].
d) [1, 3, 4, 5, 20, 5, 25].

Answer: a [Reason:] pop() by default will remove the last element.

5. What is the output when the following code is executed ?

1. `>>>"Welcome to Python".split()`

a) [“Welcome”, “to”, “Python”].
b) (“Welcome”, “to”, “Python”)
c) {“Welcome”, “to”, “Python”}
d) “Welcome”, “to”, “Python”

Answer: a [Reason:] split() function returns the elements in a list.

6. What is the output when following code is executed ?

1. `>>>list("a#b#c#d".split('#'))`

a) [‘a’, ‘b’, ‘c’, ‘d’].
b) [‘a b c d’].
c) [‘a#b#c#d’].
d) [‘abcd’].

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

7. What is the output when following code is executed ?

1. `myList = [1, 5, 5, 5, 5, 1]`
2. `max = myList[0]`
3. `indexOfMax = 0`
4. `for i in range(1, len(myList)):`
5. `    if myList[i] > max:`
6. `        max = myList[i]`
7. `        indexOfMax = i`
8. ` `
9. `>>>print(indexOfMax)`

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

Answer: a [Reason:] First time the highest number is encountered is at index 1.

8. What is the output when following code is executed ?

1. `myList = [1, 2, 3, 4, 5, 6]`
2. `for i in range(1, 6):`
3. `    myList[i - 1] = myList[i]`
4. ` `
5. `for i in range(0, 6): `
6. `    print(myList[i], end = " ")`

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

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

9. What is the output when following code is executed ?

1. `>>>list1 = [1, 3]`
2. `>>>list2 = list1`
3. `>>>list1[0] = 4`
4. `>>>print(list2)`

a) [1, 3].
b) [4, 3].
c) [1, 4].
d) [1, 3, 4].

Answer: b [Reason:] Lists should be copied by executing [:] operation.

10. What is the output when following code is executed ?

1. `def f(values):`
2. `    values[0] = 44`
3. ` `
4. `v = [1, 2, 3]`
5. `f(v)`
6. `print(v)`

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

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

## Python MCQ Set 5

1. What is the output of the following?

`print('abcdefcdghcd'.split('cd', 2))`

a) [‘ab’, ‘ef’, ‘ghcd’].
b) [‘ab’, ‘efcdghcd’].
c) [‘abcdef’, ‘ghcd’].
d) none of the mentioned

Answer: a [Reason:] The string is split into a maximum of maxsplit+1 substrings.

2. What is the output of the following?

`print('abncdnef'.splitlines())`

a) [‘ab’, ‘cd’, ‘ef’].
b) [‘abn’, ‘cdn’, ‘efn’].
c) [‘abn’, ‘cdn’, ‘ef’].
d) [‘ab’, ‘cd’, ‘efn’].

Answer: a [Reason:] It is similar to calling split(‘n’).

3. What is the output of the following?

`print('Ab!2'.swapcase())`

a)
b) ab12
c) aB!2
d)

Answer: c [Reason:] Lowercase letters are converted to uppercase and vice-versa.

4. What is the output of the following?

`print('ab cd ef'.title())`

a) Ab cd ef
b) Ab cd eF
c) Ab Cd Ef
d) None of the mentioned

Answer: c [Reason:] The first letter of every word is capitalized.

5. What is the output of the following?

`print('ab cd-ef'.title())`

a) Ab cd-ef
b) Ab Cd-ef
c) Ab Cd-Ef
d) None of the mentioned

Answer: c [Reason:] The first letter of every word is capitalized. Special symbols terminate a word.

6. What is the output of the following?

`print('abcd'.translate('a'.maketrans('abc', 'bcd')))`

a) bcde
b) abcd
c) error
d) bcdd

Answer: d [Reason:] The output is bcdd since no translation is provided for d.

7. What is the output of the following?

`print('abcd'.translate({97: 98, 98: 99, 99: 100}))`

a) bcde
b) abcd
c) error
d) none of the mentioned

Answer: d [Reason:] The output is bcdd since no translation is provided for d.

8. What is the output of the following?

`print('abcd'.translate({'a': '1', 'b': '2', 'c': '3', 'd': '4'}))`

a) abcd
b) 1234
c) error
d) none of the mentioned

Answer: a [Reason:] The function translate expects a dictionary of integers. Use maketrans() instead of doing the above.

9. What is the output of the following?

`print('ab'.zfill(5))`

a) 000ab
b) 00ab0
c) 0ab00
d) ab000

Answer: a [Reason:] The string is padded with zeroes on the left hand side. It is useful for formatting numbers.

10. What is the output of the following?

`print('+99'.zfill(5))`

a) 00+99
b) 00099
c) +0099
d) +++99