Here is an interesting Python Quiz. Attempting these Multiple choice questions will help you to evaluate your knowledge and skills.
- Which of the following commands will create a list?
a) list1 = list()
b) list1 = []
c)list1=[1,2,3,4]
d) all of the mentioned - What is the output when we execute list(“hello”)?
a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) [‘hello’]
c) [‘llo’]
d) [‘olleh’] - Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?
a) 5
b) 4
c) None
d) Error - Suppose list1 is [2445,133,12454,123], what is max(list1)?
a) 2445
b) 133
c) 12454
d) 123 - Suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?
a) 3
b) 5
c) 25
d) 1 - Suppose list1 is [1, 5, 9], what is sum(list1)?
a) 1
b) 9
c) 15
d) Error - Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is the correct syntax for slicing operation?
a) print(list1[0])
b) print(list1[:2])
c) print(list1[:-2])
d) all of the mentioned - 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] - >>> names = [‘Amir’, ‘Bear’, ‘Charlton’, ‘Daman’]
>>> names[-1][-1]
a) A
b) Daman
c) Error
d) n - To add a new element to a list we use which command?
a) list1.add(5)
b) list1.append(5)
c) list1.addLast(5)
d) list1.addEnd(5) - Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5)?
a) 0
b) 1
c) 4
d) 2 - Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?
a) 0
b) 4
c) 1
d) 2
- names1 = [‘Amir’, ‘Bala’, ‘Chales’]
if ‘amir’ in names1:
print(1)
else:
print(2)
a) None
b) 1
c) 2
d) Error
- numbers = [1, 2, 3, 4]
numbers.append([5,6,7,8])
print(len(numbers))
a) 4
b) 5
c) 8
d) 12
- >>> numbers = [1, 2, 3, 4]
numbers.extend([5,6,7,8])
len(numbers)
a) 4
b) 5
c) 8
d) 12
- what is the output of following code?
list1=[10,10,10,10]
for x in list1:
list1.remove(x)
print(list1)
a) [ ]
b) [10,10,10,10]
c) [10,10]
d) Error - What will be the output of the following Python code?
veggies = [‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’]
veggies.insert(veggies.index(‘broccoli’), ‘celery’)
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’]
- What will be the output of the following Python code?
m = [[x, x + 1, x + 2] for x in range(0, 3)]
print(m)
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] - What will be the output of the following Python code snippet?
print(‘ab\ncd\nef’.splitlines())
a) [‘ab’, ‘cd’, ‘ef’]
b) [‘ab\n’, ‘cd\n’, ‘ef\n’]
c) [‘ab\n’, ‘cd\n’, ‘ef’]
d) [‘ab’, ‘cd’, ‘ef\n’] - What will be the output of the following Python code snippet?
print(‘Ab!2’.swapcase())
a) AB!@
b) ab12
c) aB!2
d) Error - What will be the output of the following Python code snippet?
print(‘ab cd-ef’.title())
a) Ab cd-ef
b) Ab Cd-ef
c) Ab Cd-Ef
d) None of the mentioned - What will be the output of the following Python code snippet?
print(‘abcdef12’.replace(‘cd’, ’12’))
a) ab12ef12
b) abcdef12
c) ab12efcd
d) none of the mentioned - What will be the output of the following Python code snippet?
print(‘abef’.replace(‘cd’, ’12’))
a) abef
b) 12
c) error
d) none of the mentioned
26.What will be the output of the following Python code snippet?
print(‘xyyxyyxyxyxxy’.replace(‘xy’, ’12’, 0))
a) xyyxyyxyxyxxy
b) 12y12y1212x12
c) 12yxyyxyxyxxy
d) xyyxyyxyxyx12
- What will be the output of the following Python code?
print(‘Hello!2@#World’.istitle())
a) True
b) False
c) None
d) error - What will be the output of the following Python code snippet?
print(‘abc’.islower())
a) True
b) False
c) None
d) Error - What will be the output of the following Python code?
print(‘ab12’.isalnum())
a) True
b) False
c) None
d) Error - What will be the output of the following Python code?
print(‘ab,12’.isalnum())
a) True
b) False
c) None
d) Error - What will be the output of the following Python code?
print(“ccdcddcd”.find(“c”))
a) 4
b) 0
c) Error
d) True - What will be the output of the following Python code?
print(“Hello {0} and {1}”.format(‘foo’, ‘bin’))
a) Hello foo and bin
b) Hello {0} and {1} foo bin
c) Error
d) Hello 0 and 1 - What will be the output of the following Python code?
print(“Hello {name1} and {name2}”.format(‘foo’, ‘bin’))
a) Hello foo and bin
b) Hello {name1} and {name2}
c) Error
d) Hello and - What will be the output of the following Python code?
print(“Hello {} and {}”.format(‘foo’, ‘bin’))
a) Hello foo and bin
b) Hello {} and {}
c) Error
d) Hello and - What will be the output of the following Python code?
str1=”helloworld”
str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld
- Which of the following statement prints hello\example\test.txt?
a) print(“hello\example\test.txt”)
b) print(“hello\example\test.txt”)
c) print(“hello\”example\”test.txt”)
d) print(“hello”\example”\test.txt”)
- What will be the output of the following Python statement?
chr(ord(‘A’))
a) A
b) B
c) a
d) Error
- What will be the output of the “hello” +1+2+3 ?
a) hello123
b) hello
c) Error
d) hello6 - Which of the following is a Python tuple?
a) [1, 2, 3]
b) (1, 2, 3)
c) {1, 2, 3}
d) {}
- Suppose t = (1, 2, 4, 3), which of the following is incorrect?
a) print(t[3])
b) t[3] = 45
c) print(max(t))
d) print(len(t)) - What will be the output of the following Python code?
t=(1,2,4,3)
t[1:3]
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
- What will be the output of the following Python code?
t1 = (1, 2, 4, 3)
t2 = (1, 2, 3, 4)
t1 < t2
a) True
b) False
c) Error
d) None - What will be the output of the following Python code?
t = (1, 2)
2 * t
a) (1, 2, 1, 2)
b) [1, 2, 1, 2]
c) (1, 1, 2, 2)
d) [1, 1, 2, 2]
- What will be the output of the following Python code?
a=1,2,3,4
print(a)
a) (1, 2, 3, 4)
b) 1
c) 4
d) 1,2,3,4
- What will be the output of the following Python code?
a=(1,2)
b=(3,4)
c=a+b
c
a) (4,6)
b) (1,2,3,4)
c) Error as tuples are immutable
d) None
- What will be the output of the following Python code?
a=(2,3,1,5)
a.sort()
a
a) (1,2,3,5)
b) (2,3,1,5)
c) None
d) Error, tuple has no attribute sort
- What will be the output of the following Python code?
s1={3, 4}
s2={1, 2}
s3=set()
i=0
j=0
for i in s1:
for j in s2:
s3.add((i,j))
i+=1
j+=1
print(s3)
a) {(3, 4), (1, 2)}
b) Error
c) {(4, 2), (3, 1), (4, 1), (5, 2)}
d) {(3, 1), (4, 2)}
- The __ function removes the first element of a set and the last element of a list.
a) remove
b) pop
c) discard
d) dispose
- What will be the output of the following Python code?
s=set([1, 2, 3])
s.union([4, 5])
print(s)
a){1, 2, 3}
b)[1,2,3]
c){1,2,3,4,5}
d)[1,2,3,4,5]