Python MCQ Questions Set-11

Here is an interesting Python Multiple choice questions Quiz. Attempting this MCQ will help you to evaluate your knowledge and skills.

1. What will be the output of the following Python code?

count={}
count[(1,2,4)] = 5
count[(4,2,1)] = 7
count[(1,2)] = 6
count[(4,2,1)] = 2

tot = 0

for i in count:
tot=tot+count[i]

print(len(count)+tot)

a) 25
b) 17
c) 16
d) Tuples can’t be made keys of a dictionary

2. What is the output of the following code?

a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])
a) [2,3,4]
b) 3
c) 2
d) An exception is thrown

3. What will be the output of the following Python code?

a={i: i*iforiinrange(6)}
print (a)

a) Dictionary comprehension doesn’t exist
b) {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6:36}
c) {0: 0, 1: 1, 4: 4, 9: 9, 16: 16, 25: 25}
d) {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

4. Which of the statements about dictionary values is false?

a) More than one key can have the same value
b) The values of the dictionary can be accessed as dict[key]
c) Values of a dictionary must be unique
d) Values of a dictionary can be a mixture of letters and numbers

5. What will be the output of the following Python code snippet?

total={}
def insert(items):
if items in total:
total[items] +=1
else:
total[items]=1

insert(‘Apple’)
insert(‘Ball’)
insert(‘Apple’)
print(len(total))

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

6. What will be the output of the following Python code snippet?

a ={}
a[1]=1
a[‘1’]=2
a[1]=a[1]+1
count =0

foriin a:
count += a[i]
print(count)

a) 1
b) 2
c) 4
d) Error, the keys can’t be a mixture of letters and numbers

7.What will be the output of the following Python code snippet?

numbers ={}
letters ={}
comb ={}
numbers[1]=56
numbers[3]=7
letters[4]=’B’
comb[‘Numbers’]= numbers
comb[‘Letters’]= letters
print(comb)

a) Error, dictionary in a dictionary can’t exist
b) ‘Numbers’: {1: 56, 3: 7}
c) {‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}
d) {‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}

8. Which of these about a dictionary is false?

a) The values of a dictionary can be accessed using keys
b) The keys of a dictionary can be accessed using values
c) Dictionaries aren’t ordered
d) Dictionaries are mutable

9. What will be the output of the following Python code snippet?

a={1:”A”,2:”B”,3:”C”}
fori,jina.items():
print(i,j,end=” “)
a) 1 A 2 B 3 C
b) 1 2 3
c) A B C
d) 1:”A” 2:”B” 3:”C”

10. What will be the output of the following Python code snippet?

a={1:”A”,2:”B”,3:”C”}
print(a.get(1,4))
a) 1
b) A
c) 4
d) Invalid syntax for get method

11. What will be the output of the following code?

a={1:”A”,2:”B”,3:”C”}
print(a.get(5,4))

a) Error, invalid syntax
b) A
c) 5
d) 4

12. What will be the output of the following Python code?

a={1:”A”,2:”B”,3:”C”}
b=a.copy()
b[2]=”D”
print(a)
a) Error, copy() method doesn’t exist for dictionaries
b) {1: ‘A’, 2: ‘B’, 3: ‘C’}
c) {1: ‘A’, 2: ‘D’, 3: ‘C’}
d) “None” is printed

13. The character Dot (that is, ‘.’) in the default mode, matches any character other than _

a) caret
b) ampersand
c) percentage symbol
d) newline

14. The expression a{5} will match _ characters with the previous regular expression.

a) 5 or less
b) exactly 5
c) 5 or more
d) exactly 4

15. matches the start of the string.
matches the end of the string.

a) ‘^’, ‘$’
b) ‘$’, ‘^’
c) ‘$’, ‘?’
d) ‘?’, ‘^’

Leave a Comment