MCQ of python
Q1. What is the output of the following code :
print(9//2)
A. 4
B. 4.5
c. 1
d. 4.0
Show Answer
Q2. Given a string s = “Welcome”, which of the following code is incorrect?
A. print(s[0])
B. print(s.lower())
C. s[1]=’r’
D. print(s.strip())
Show Answer
Q3. Which shortcut command is used to start a new script window in Python IDLE
A. Ctrl+O
B. Ctrl+ N
C. Ctrl+X
D. ctrl+K
Show Answer
Q4. Which shortcut command is used to run pyton script in Python IDLE
A. F3
B. F5
C. F1
D. F6
Show Answer
Q5. What data type is the object below ? L = [1, 23, ‘hello’, 1]
A. List
B. Dictionary
C. Tuple
D. Array
Show Answer
Q6. Which of the following function convert a string to a float in python?
A. str(x)
B. float(x)
C. int(x,base)
d. long(x,base)
Show Answer
Q7. What is the output of the following program :
defmyfunc(a):
a=a+2
a=a*2
return a
printmyfunc(2)
A. 2
B. 16
C. 8
D. Indentation error
Show Answer
Q8. What is the output of the expression : 3*1**3
A. 3
B. 27
C. 9
D. 1
Show Answer
Q9. What is the output of the following program :
i=0
while i< 3: print(i, end=" ") i += 1 else: print(0) A. 0 1 2 3 B. 0 1 2 0 C. 0 1 2 D. Error
[show_answer] Option B Explanation : 0 print when condition results in false.
[/show_answer]Q10. What is the output of the following program : i = 0 while i< 5: print(i)
i += 1 if i == 3: break else: print(0) A. 0 1 2 B. 0 1 2 3 C. None of the above D. Error
[show_answer] Option AExplanation : Loop terminated when the value of i is 3
[/show_answer]
1. Find the output of the following Python Code
fruits = [“apple”,”banana”,”cherry”]
for x in fruits:
print(x)
A) “apple”, “banana”, “cherry”
b) “a”, “b”,”c”
c) 0,1,2
d) None of the above
Show Answer
Q2. Find out the output of the following python Program
fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
if x == “banana”:
break
print(x)
a) “apple”, “banana”, “cherry”
b) “apple”
c) “banana”
d) None of the above
Show Answer
Q3. Find the output of the following Python Program
for x in range(1,20,3):
print(x)
a) 1,4,7,10,13,16,19
b) 1,20,3
c) 13,6,9,12,15,18
d) None of above
Show Answer
Q4. Find out the output of the following program
birds = [‘Belle’, ‘Snow’, ‘Juniper’, ‘Lilly’, ‘Swara’]
ignoreElse = False
for theBird in birds:
print(theBird )
if ignoreElse and theBird is ‘Snow’:
break
else:
print(“No birds left.”)
a) ‘Belle’
b) “Belle’, ‘Snow’, ‘No birds left’
c) ‘Belle’ , ‘No birds left’
d) ‘ ‘Belle’,’Snow’
Show Answer
Q5. Find out the output of the following Python Program
if (7 < 0) and (0 < -7):
print(“abhi”)
elif (7 > 0) or False:
print(“love”)
else:
print(“cbsetoday.com”)
a)abhi
b) love
c) cbsetoday.com
d) none of the above
Show Answer
Q6. Find out the output of the following program segment
for i in range(10):
print(i)
a) 0,1,2,3,4,5,6,7,8,9
b) 1,2,3,4,5,6,7,8,9,10
c) 10
d) None of the abobe
Q7. Find out the output of the following python program
for i in range(1,5):
print(i)
else:
print(i)
a) 0,1,2,3,4,5
b) 0,1,2,3,4
c) 1,2,3,4
d) None of the above
Show Answer
Q8. Find out the output of the following python Program
true= False
while(true):
print(‘I am confused”)
else:
print( “why confused”)
a) I am confused
b) Why confused
c) No output
b) Both a and b
Show Answer
Q9. Find out the output of the following Python Program
i = -3
while (i)
print(‘Hello my dear’)
a) Hello my dear infinite time
b) Hello single time
c) No output
d) None of the above
Show Answer
Select the reserved keyword in python
(A) else
(B) import
(C) raise
(D) All of these
Which of the following symbols are used for comments in Python?
(A) //
(B) ''
(C) /**/
(D) #
Are nested if-else are allowed in Python?
(A) Yes
(B) No
Which keyword is used to define methods in Python?
(A) function
(B) def
(C) method
(D) All of these
Which of the following is correct way to declare string variable in Python?
(A) fruit = 'banana'
(B) fruit = "banana"
(C) fruit = banana
(D) fruit = (banana)
Which predefined Python function is used to find length of string?
(A) length()
(B) len()
(C) strlen()
(D) stringlength()
Python allows string slicing. What is the output of below code:
s='cppbuzzchicago'
print(s[3:5])
(A) pbuzz
(B) buzzc
(C) bu
(D) None of these
Syntax of constructor in Python?
(A) def __init__()
(B) def _init_()
(C) _init_()
(D) All of these
How to find the last element of list in Python? Assume `bikes` is the name of list.
(A) bikes[0]
(B) bikes[-1]
(C) bikes[lpos]
(D) bikes[:-1]
What is correct syntax to copy one list into another?
(A) listA = listB[]
(B) listA = listB[:]
(C) listA = listB[]()
(D) listA = listB
If a='cpp', b='buzz' then which of the following operation would show 'cppbuzz' as output?
(A) a+b
(B) a+''+b
(C) a+""+b
(D) All of the above
If a='cpp', b='buzz' then what is the output of:
c = a-b
print(c)
(A) cpp-buzz
(B) cppbuzz
(C) TypeError: unsupported operand
(D) None of the above
a = 8.6
b=2
print a//b
(A) 4.3
(B) 4.0
(C) 4
(D) compilation error
a = True
b = False
c = True
if not a or b:
print "a"
elif not a or not b and c:
print "b"
elif not a or b or not b and a:
print "c"
else:
print "d"
(A) a
(B) b
(C) c
(D) d
class test:
def __init__(self):
print "Hello World"
def __init__(self):
print "Bye World"
obj=test()
(A) Hello World
(B) Compilation Error
(C) Bye World
(D) Ambiguity
The format function, when applied on a string returns :
(A) list
(B) bool
(C) int
(D) str
The format function, when applied on a string returns :
(A) list
(B) bool
(C) int
(D) str
print(chr(ord('b')+1))
(A) b
(B) syntax error
(C) c
(D) b+1
Which statement is correct....??
(A) List is mutable && Tuple is immutable
(B) List is immutable && Tuple is mutable
(C) Both are Immutable
(D) Both are Mutable.
What is PEP8?
PEP8 is a set of coding guidelines in Python language that programmers can use to write
readable code which makes it easy to use for other users.
State the difference between tuples and lists in Python.
Ans: Tuples can be used a key in a dictionary to store notes on locations whereas a list can be
used to store multiple locations. Lists are mutable whereas tuples are immutable which means
they cannot be edited.
Write a sorting algorithm for a numerical dataset in Python
Ans: The following code can be used to sort a list in Python:
list = ["1", "4", "0", "6", "9"]
list = [int(i) for i in list]
list.sort()
print (list)
What is pickling and unpickling?
Ans: Pickle module accepts any Python object and converts it into a string representation and
dumps it into a file by using dump function, this process is called pickling. While the process
of retrieving original Python objects from the stored string representation is called
unpickling.
What is the maximum possible length of an identifier?
(A) 32 characters
(B) 63 characters
(C) 79 characters
(D) None of the above
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
Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1] ?
(A) Error
(B) 25
(C) None
(D) 2
To open a file c:cppbuzz.txt for writing, we use
(A) outfile = open(file = “c:cppbuzz.txt”, “o”)
(B) outfile = open(“c:cppbuzz.txt”, “r”)
(C) outfile = open(“c:cppbuzz.txt”, “w”)
(D) outfile = open(“c:cppbuzz.txt”, “r”)
What is dictionary in Python?
The built-in datatypes in Python is called dictionary. It defines one-to-one relationship
between keys and values. Dictionaries contain pair of keys and their corresponding values.
Dictionaries are indexed by keys.
Which of the following date format should be used in place of ???
import time
str = '02/06/1987'
datetime_value = time.strptime(str, ???)
(A) “%d/%m/%Y”
(B) “%d/%M/%y”
(C) “%D/%M/%Y”
(D) “%d/%m/%y”
Which one of the following is not a python's predefined data type?
(A) Class
(B) List
(C) Dictionary
(D) Tuple
What will be the output of 7^10 in python?
(A) 13
(B) 2
(C) 15
(D) None of these
How following evaluates in Python?
round(0.5) – round(-0.5)
(A) 0
(B) 1
(C) 2
(D) 0.5
Find out output of following code snippet
>>>str="cppbuzz"
>>>str[:3]
(A) uzz
(B) cpp
(C) buzz
(D) cppb
Is it possible to use round function without any argument like round()
(A) Yes
(B) No
What is the data type of X in X = [12.12, 13, 'cppbuzz']
(A) Tuple
(B) Array
(C) Dictionary
(D) List
Which of the following data type is used to store values in Key & Value format?
(A) Class
(B) List
(C) Tuple
(D) Dictionary
Select the correct code to print
cppbuzz-chicago
(A) print('cppbuzz-' + 'chicago');
(B) print('cppbuzz' + '-chicago');
(C) print('cppbuzz' + '-' + 'chicago');
(D) All of the above
Which of the following operators is used to get accurate result (i.e fraction part also) in case
of division ?
(A) //
(B) %
(C) /
(D) None of the above
19 % 2 in python
(A) 17
(B) 2
(C) 0
(D) None of these
What is the associativity of Operators with the same precedence?
(A) Depends on operators
(B) Left to Right
(C) Right to Left
(D) Depends on Python Compiler
Which of the following has more precedance?
(A) +
(B) /
(C) -
(D) ()
Is Python case sensitive?
(A) Yes
(B) No
(C) Depends on Python Version
Which of the following variable is invalid?
(A) __str
(B) __str
(C) _str_
(D) None of these
Select the command to Find and print Data types using the Type command.
name="Hello World"
(A) type(name)
(B) print(type(name))
(C) print(name)
(D) type()
what is the output for:
name="Hello World"
print(type(name))
(A) Hello World
(B) hello World
(C) <class 'str'>
(D) str
Is Tuple mutable?
(A) Yes
(B) No