β˜‘ MCQ PRACTICE

Python Programming Unit 2

Practice objective questions for quick revision and examination preparation. Try answering each question before revealing the answer.

πŸ“š Python Programming
πŸ“– Unit 2
🎯 MCQs
Python Programming MCQs - Unit 2

Python Programming MCQs - Unit 2

1

Which of the following is invalid identifier declaration?

A _a = 1
B __a = 1
C __str__ = 1
D none of the mentioned
Correct Answer none of the mentioned
2

Which of the following is an invalid statement?

A abc = 1,000,000
B a b c = 1000 2000 3000
C a,b,c = 1000, 2000, 3000
D a_b_c = 1,000,000
Correct Answer a b c = 1000 2000 3000
3

What error occurs when you execute?>>>apple = mango

A SyntaxError
B NameError
C ValueError
D TypeError
Correct Answer NameError
4

What is the output of the following?x = ['ab', 'cd'] for i in x: x.append(i.upper()) print(x)

A [β€˜AB’, β€˜CD’].
B [β€˜ab’, β€˜cd’, β€˜AB’, β€˜CD’].
C [β€˜ab’, β€˜cd’].
D none of the mentioned
Correct Answer none of the mentioned
5

True = False while True: print(True) break

A True
B False
C Error
D none of the mentioned
Correct Answer Error
6

What is the output of the following?for i in range(10): if i == 5: break else: print(i) else: print("Here")

A 0 1 2 3 4 Here
B 0 1 2 3 4 5 Here
C 0 1 2 3 4
D 1 2 3 4 5
Correct Answer 0 1 2 3 4
7

What is the output of the following?print("xyyzxyzxzxyy".count('xyy', -10, -1))

A 2
B 0
C 1
D Error
Correct Answer 0
8

What is the output of the following?print("ab\tcd\tef".expandtabs(1))

A ab cd ef
B abcdef
C ab cd ef
D Error
Correct Answer ab cd ef
9

What is the output of the following?print('ab,12'.isalnum())

A True
B False
C Error
D none of the mentioned
Correct Answer False
10

What is the output when following code is executed ?myList = [1, 2, 3, 4, 5, 6] for i in range(1, 6): myList[i - 1] = myList[i] for i in range(0, 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
Correct Answer 2 3 4 5 6 6
11

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

A 3
B 5
C 6
D 33
Correct Answer 33
12

What will be the output?>>>my_tuple = (1, 2, 3, 4) >>>my_tuple.append( (5, 6, 7) ) >>>print(len(my_tuple))

A 7
B 4
C 3
D Error
Correct Answer Error
13

Is the following piece of code valid?>>> a=(1,2,3,4) >>> del a

A Yes, the entire tuple is deleted
B Yes, first element in the tuple is deleted
C No because tuple is immutable
D No, invalid syntax for del method
Correct Answer Yes, the entire tuple is deleted
14

What is the output for the following piece of code?>>> a={1,2,3} >>> b=a >>> b.remove(3) >>> print(a)

A {1,2,3}
B {1,2}
C Error, copying of sets isn’t allowed
D Error, invalid syntax for remove
Correct Answer {1,2}
15

What is the output of the following code?>>> a={1,2,3} >>> b=a.add(4) >>>print(b)

A None
B {1,2,3,4}
C {1,2,3}
D Error
Correct Answer None
16

The ____________ function removes the first element of a set and the last element of a list.

A remove()
B discard()
C clear()
D pop()
Correct Answer pop()
17

The difference between the functions discard and remove is that:

A Discard removes the last element of the set whereas remove removes the first element of the set
B Discard throws an error if the specified element is not present in the set whereas remove does not throw an error in case of absence of the specified element
C Remove removes the last element of the set whereas discard removes the first element of the set
D Remove throws an error if the specified element is not present in the set whereas discard does not throw an error in case of absence of the specified element
Correct Answer Remove throws an error if the specified element is not present in the set whereas discard does not throw an error in case of absence of the specified element
18

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 1
Correct Answer 3
19

What is the output of the following piece of code?>>> a={'B':5,'A':9,'C':7} >>> print(sorted(a))

A [β€˜A’,’B’,’C’].
B [β€˜B’,’C’,’A’].
C [5,7,9].
D [9,5,7].
Correct Answer [β€˜A’,’B’,’C’].
20

What is the output when following code is executed ?>>>names = ['Amir', 'Bear', 'Charlton', 'Daman'] >>>print(names[-1][-1])

A A
B Daman
C n
D Charlton
Correct Answer n
21

What is the output of the following?a = [0, 1, 2, 3] for a[0] in a: print(a[0],end=" ")

A 0 0 0 0
B 0 1 2 3
C 0 1 2 2
D 3 3 3 3
Correct Answer 0 1 2 3
22

Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1] ?

A [2, 33, 222, 14].
B Error
C 25
D [25, 14, 222, 33, 2].
Correct Answer [2, 33, 222, 14].
23

What is the output when following code is executed ?def f(values): values[0] = 44 v = [1, 2, 3] f(v) print(v)

A [1, 44].
B [1, 2, 3, 44].
C [44, 2, 3].
D [1, 2, 3].
Correct Answer [44, 2, 3].
24

What will be the output?>>>t=(1,2,4,3) >>>print(t[1:-1])

A (1,2)
B (2,3)
C (1,3)
D (2,4)
Correct Answer (2,4)
25

What is the output of the following piece of code?a={1:"A",2:"B",3:"C"} print(a.get(1,4))

A 1
B A
C 4
D Error
Correct Answer A
← Back to All MCQs