Python Multiple Choice Questions
Python Multiple Choice Questions
1) Study the following program:
print(print(print("javatpoint"))) print(print(print("javatpoint")))
What will be the output of this program?
javatpoint None None
None None javatpoint
None javatpoint None
Javatpoint
Answer: (a) javatpoint None None
Explanation: In this program, the inner print function will run first as compared to the outer print function. Therefore, the correct output of this program is "javatpoint None None".
2) Study the following program:
print(True ** False / True) print(True ** False / True)
What will be the output of this program?
True ** False / True
1.0
1 ** 0 / 1
None of the these
Answer: (b) 1.0
Explanation: Binary values
True = 1
False = 0
(1 ** 0 / 1) = (10/ 1) = 1.0
Therefore, option (b) is the correct output of this program.
3) Study the following program:
int1 = 10
int2 = 6
if int != int2:
int2 = ++int2
print(int1 - int2) int1 = 10 int2 = 6 if int != int2: int2 = ++int2 print(int1 - int2)
What will be the output of this program?
2
4
6
None
Answer: (b) 4
Explanation: In the Python Programming Language, Increment and Decrement condition is not valid.
(y = ++y) = (y = y)
So, the output of this program is 4.
4) Study the following program:
int1 = 10
int2 = 6
if int != int2:
int2 = ++int1
print(int1 - int2) int1 = 10 int2 = 6 if int != int2: int2 = ++int1 print(int1 - int2)
What will be the output of this program?
2
4
0
No Output
Answer: (c) 0
Explanation: In the Python Programming Language, Increment and Decrement condition is not valid.
int1 = (10)
int2 = (6)
int2 = ++int1
int2 = 10
print(10 - 10)
So, the output of this program is the 0.
5) Study the following program:
print(6 + 5 - 4 * 3 / 2 % 1) print(6 + 5 - 4 * 3 / 2 % 1)
What will be the output of this program?
7
7.0
15
0
Answer: (d) 11.0
Explanation: Precedence table in python
High *, /, // and%
Low + and -
If the operator precedence is same, the calculation starts from left to right.
Therefore, option (d) is the correct output of this program.
6) Study the following program:
int1 = 0b0010
print(int1) int1 = 0b0010 print(int1)
What will be the output of this program?
0b0010
2
NameError: name '0b0010' is not defined
SyntaxError
Answer: (b) 2
Explanation: "0b0010" value is a binary value. Therefore, option (b) is the correct output of this program.
7) Study the following program:
word = "javatpoint"
print(*word) word = "javatpoint" print(*word)
What will be the output of this program?
javatpoint
j a v a t p o i n t
*word
SyntaxError: invalid syntax
Answer: (b) j a v a t p o i n t
Explanation: When a user prints a string with "*", that string is printed with the space in each word. Therefore, option (b) is the correct output of this program.
8) Study the following program:
i = 2, 10
j = 3, 5
add = i + j
print(add) i = 2, 10 j = 3, 5 add = i + j print(add)
What will be the output of this program?
(5, 10)
20
(2, 10, 3, 5)
SyntaxError: invalid syntax
Answer: (c) (2, 10, 3, 5)
Explanation: "i" and "j" are tuples values. The tuple values are added to the bracket. Therefore, option (c) is the correct output of this program.
9) Study the following program:
print(int(6 == 6.0) * 3 + 4 % 5) print(int(6 == 6.0) * 3 + 4 % 5)
What will be the output of this program?
22
18
20
7
Answer: (d) 7
Explanation: In the python programming language, (int(6 == 6.0)) is a valid condition but in other languages is not a valid condition. Therefore, option (d) is the correct output of this program.
10) Study the following program:
i = 2
j = 3, 5
add = i + j
print(add) i = 2 j = 3, 5 add = i + j print(add)
What will be the output of this program?
5, 5
5
(2 , 3 , 5)
TypeError
Answer: (d) TypeError
Explanation: In this program, "i" is the integer value, and "j" is the tuple value. The integer and tuple values cannot be added in the python programming language. Therefore, this program will print the "Typeerror".
11) How many control statements python supports?
Four
Five
Three
None of the these
Answer: (c) Three
Explanation: In the Python Programming Language, there are three types of control statements.
Break
Continue
Pass statements
12) How many keywords present in the python programming language?
32
61
33
27
Answer: (c) 33
Explanation: There are 33 keywords in python. In the Python Programming Language, keywords are reserved words for the program that is used to define the syntax and structure. You cannot use a keyword as a function name, variable name, or any other identifier.
13) Which of the following arithmetic operators cannot be used with strings in python?
+
*
-
All of the mentioned
Answer: (c) -
Explanation: In python, only (+) and (*), two arithmetic operators are used with string. Therefore, option (c) is the correct answer.
14) Study the following program:
print("java", 'point', sep='2') print("java", 'point', sep='2')
What will be the output of this program?
javapoint2
japoint
java2point
javapoin2
Answer: (c) java2point
Explanation: The "sep" means separator that is used to add a separator between the two strings. Therefore, option (c) is the correct output of this program.
15) Study the following program:
print('It\'s ok, don\'t worry') print('It\'s ok, don\'t worry')
What will be the output of this program?
It's ok, don't worry
It\'s ok, don\'t worry
SyntaxError: EOL while scanning string literal
SyntaxError: invalid syntax
Answer: (a) It's ok, don't worry
Explanation: In the Python programming language, the backslash "\" is an escape character. Therefore, option (a) is the correct output of this program.
16) Study the following program:
_ = '1 2 3 4 5 6'
print(_) _ = '1 2 3 4 5 6' print(_)
What will be the output of this program?
SyntaxError: EOL while scanning string literal
SyntaxError: invalid syntax
NameError: name '_' is not defined
1 2 3 4 5 6
Answer: (d) 1 2 3 4 5 6
Explanation: "_" is a valid variable name. Therefore, option (d) is the correct output of this program.
17) Which of the following keywords is not reversed keyword in python?
None
class
goto
and
Answer: (c) goto
Explanation: "and", "class", and "None" are reversed keywords in python. So, option (c) is the correct answer.
18) Study the following program:
a = '1 2'
print(a * 2)
print(a * 0)
print(a * -2) a = '1 2' print(a * 2) print(a * 0) print(a * -2)
What will be the output of this program?
1 2 1 2
2 4
0
-1 -2 -1 -2
Answer: (a) 1 2 1 2
Explanation:
"a * 2" means, string prints 2 times.
"a * 0" means, string is empty.
Any string cannot be negative. Therefore, the string will not print any word.
19) Study the following program:
print(max("zoo 145 com")) print(max("zoo 145 com"))
What will be the output of this program?
145
122
a
z
Answer: (d) z
Explanation: The ASCII value of the a-z lies in the range 97 - 122. So, the maximum value of the string is 122 (z = 122).
20) Study the following program:
a = "123789"
while x in a:
print(x, end=" ") a = "123789" while x in a: print(x, end=" ")
What will be the output of this program?
i i i i i i …
123789
SyntaxError
NameError
Answer: (d) NameError
Explanation: This program will print the NameError because 'x' is not defined in this code.
21) PVM is often called _________.
Python interpreter
Python compiler
Python volatile machine
Portable virtual machine
Answer: (a) Python interpreter
Explanation: PVM is a software that converts bytecode to machine code for a given OS. PVM is also called Python Interpreter, and that is why Python is called Interpreted Language.
22) Study the following program:
i = {4, 5, 6}
i.update({2, 3, 4})
print(i) i = {4, 5, 6} i.update({2, 3, 4}) print(i)
What will be the output of this program?
2 3 4 4 5 6
2 3 4 5 6
4 5 6 2 3 4
Error, duplicate element presents in list
Answer: (b) 2 3 4 5 6
Explanation: This is a valid syntax of the update function. Therefore, the option (b) is the correct output of this program.
23) Study the following program:
i=(12, 20, 1, 0, 25)
i.sort()
print(i) i=(12, 20, 1, 0, 25) i.sort() print(i)
What will be the output of this program?
0 1 12 20 25
1 12 20 25
FunctionError
AttributeError
Answer: (d) AttributeError
Explanation: In this program, "i" is the tuple value. The tuple value cannot be sorted in python language. Therefore, this program will print the "AttributeError".
24) Which of the following keywords is used for function declaration in Python language?
def
function_name
define
None of the these
Answer: (a) def
Explanation: In the python language, the def keyword is used to define the function.
Syntax of the function declaration def function_name(parameters): block of function return expression
25) Which of the following objects are present in the function header in python?
Function name and Parameters
Only function name
Only parameters
None of the these
Answer: (a) Function name and Parameters
Explanation: Function name and Parameter are both present in the function header in python. def function_name(parameters): block of function return expression
26) When a user does not use the return statement inside a function in Python, what will return the function in that case.
0
1
None
No output
Answer: (c) None
Explanation: When a user does not use the return statement inside a function in Python, the function will return the "None".
27) Which one of the following is the right way to call a function?
call function_name()
function function_name()
function_name()
None of the these
Answer: (c) function_name()
Explanation: To call a function in python language, it uses the function name followed by the parentheses.
28) Suppose a user wants to print the second value of an array, which has 5 elements. What will be the syntax of the second value of the array?
array[2]
array[1]
array[-1]
array[-2]
Answer: (b) array[1]
Explanation: The index of the array starts with 0. Therefore, the option (b) is the correct answer.
29) Study the following program:
str1="python language"
str1.find("p")
print(str1) str1="python language" str1.find("p") print(str1)
What will be the output of this program?
Print the index value of the p.
p
python language
AttributeError
Answer: (c) python language
Explanation: In this program, it will print the value of the str1. Therefore, the option (c) is the correct output of this program.
30) Study the following program:
flag = ""
a = 0
i = 1
while(a < 3):
j = 1
if flag:
i = j * i + 5
else:
i = j * i + 1
a = a + 1
print(i) flag = "" a = 0 i = 1 while(a < 3): j = 1 if flag: i = j * i + 5 else: i = j * i + 1 a = a + 1 print(i)
What will be the output of this program?
12
4
11
16
Answer: (b) 4
Explanation: The output of this program is 4.
31) Study the following expression:
str = [(1, 1), (2, 2), (3, 3)] str = [(1, 1), (2, 2), (3, 3)]
What type of data is in this expression?
String type
Array lists
List of tuples
str lists
Answer: (c) List of tuples
Explanation: The variable str has a list of tuples attached to it. Hence it is a list of tuples. So, option (c) is the correct answer.
32) Which of the following statements is not valid regarding the variable in python?
The variable_name can begin with alphabets
The variable_name can begin with an underscore
The variable_name can begin with a number
None of the these
Answer: (c) The variable_name can begin with a number
Explanation: The variable_name can begin with alphabets or underscore but cannot begin with numbers. So, option (c) is the correct answer.
33) Study the following program:
a = 2
while(a > -100):
a = a - 1
print(a) a = 2 while(a > -100): a = a - 1 print(a)
How many times will this program run the loop?
Infinite
102
2
1
Answer: (b) 102
Explanation: This loop will run the 1 to -100 (1, 0, -1,?, -100). So, option (b) is the correct answer.
34) Study the following program:
arr = [3 , 2 , 5 , 6 , 0 , 7, 9]
add1 = 0
add2 = 0
for elem in arr:
if (elem % 1 == 0):
add1 = add1 + elem
continue
if (elem % 3 == 0):
add2 = add2 + elem
print(add1 , end=" ")
print(add2) arr = [3 , 2 , 5 , 6 , 0 , 7, 9] add1 = 0 add2 = 0 for elem in arr: if (elem % 1 == 0): add1 = add1 + elem continue if (elem % 3 == 0): add2 = add2 + elem print(add1 , end=" ") print(add2)
What will be the output of this program?
32 0
0 32
18 0
0 18
Answer: (a) 32 0
Explanation: The output of this program is (32, 0).
35) Which of the following statements is valid for "if statement"?
if f >= 12:
if (f >= 122)
if (f => 1222)
if f >= 12222
Answer: (a) if f >= 12:
Explanation: The "if statement" always ends with a colon (:). So, option (a) is the correct statement.
36) Which of the following blocks allows you to test the code blocks for errors?
except block
try block
finally block
None of the these
Answer: (b) try block
Explanation: The try block allows you to test the code blocks for errors in the python language.
37) Study the following program:
try:
print(file_name)
except:
print("error comes in the line") try: print(file_name) except: print("error comes in the line")
What will be the output of this program?
file_name
error
error comes in the line
file_name error comes in the line
Answer: (c) error comes in the line
Explanation: The try block will generate an error because file_name is not defined in the program. Therefore, the output of this program will be "error comes in the line".
38) Study the following program:
i = 10
j = 8
assert i > j, 'j = i + j'
print(j) i = 10 j = 8 assert i > j, 'j = i + j' print(j)
What will be the output of this program?
18
8
No output
TypeError
Answer: (b) 8
Explanation: In this program, the assert keyword has been used to mislead the user. Therefore, this program will print the value of "j".
39) Study the following program:
class Student:
print("Students of Section A")
Student()
Student()
obj = Student() class Student: print("Students of Section A") Student() Student() obj = Student()
How many objects are there for the given program?
1
2
3
None of the these
Answer: (c) 3
Explanation: There will be three objects created in this program. Therefore, the option (c) is the correct answer.
40) Study the following program:
class Teacher:
def __init__(name, id_no, age):
name.id_no = id_no
name.age = age
teac = Teacher(5, 25) class Teacher: def __init__(name, id_no, age): name.id_no = id_no name.age = age teac = Teacher(5, 25)
Which of the following statements is incorrect regarding this program?
A constructor has been given in this program
id_no and age are called the parameters
The "teac" is the reference variable for the object Teacher(5, 25)
None of the these
Answer: (d) None of the these
Explanation: All statements are correct. So, the option (d) is the correct answer.
41) Study the following program:
class Teacher:
def __init__(self, id, age):
self.id = id
self.age = age
print(self.age)
tear = Teacher("John", 20)
tear.age = 30
print(tear.age) class Teacher: def __init__(self, id, age): self.id = id self.age = age print(self.age) tear = Teacher("John", 20) tear.age = 30 print(tear.age)
Which of the following statements is incorrect regarding this program?
20 John 30
20 30
John 30
30 John 20
Answer: (b) 20 30
Explanation: The output of this program is (20 30).
42) Which of the following code will create a set in python language?
1. thisset = (("apple", "banana", "cherry"))
2. thisset = ("car", "bike", "123")
3. thisset = {}
1 only
1 and 2 both
1, 2, and 3 will create a set
None of the these
Answer: (c) 1, 2, and 3 create a set
Explanation: All codes will create a set. So, option (c) is the correct answer.
43) Study the following program:
set = {0, 0, "a1", 0, 9}
print(set) set = {0, 0, "a1", 0, 9} print(set)
What will be the output of this program?
{0, 0, 'a1', 0, 9}
{0, 'a1', 0, 9}
{0, 9, 'a1'}
{0, 0, 9, 0, 'a1'}
Answer: (c) {0, 9, 'a1'}
Explanation: The output of this program is {0, 9, 'a1'}
44) Which of the following statements would create a tuple in python?
mytuple = ("apple", "banana", "cherry")
mytuple[123] = ("apple", "banana", "cherry")
mytuple = ("2" * ("apple", "banana", "cherry"))
None of the these
Answer: (a) mytuple = ("apple", "banana", "cherry")
Explanation: Option (a) is the correct syntax for a tuple. So, option (a) is the correct answer.
45) Study the following program:
mytuple1=(5, 1, 7, 6, 2)
mytuple1.pop(2)
print(mytuple1) mytuple1=(5, 1, 7, 6, 2) mytuple1.pop(2) print(mytuple1)
What will be the output of this program?
5 1 7 6 2
No output
AttributeError
None of the these
Answer: (c) AttributeError
Explanation: In this program, "mytuple1" is the tuple value. In the python language, the pop () method cannot be used with tuple value. Therefore, this program will print the "AttributeError".
46) Which of the following functions returns a list containing all matches?
find
findall
search
None of the these
Answer: (b) findall
Explanation: The findall function is the most powerful function in python language that returns a list containing all matches.
47) Study the following program:
mytuple1 = (2, 4, 3)
mytuple3 = mytuple1 * 2
print(mytuple3) mytuple1 = (2, 4, 3) mytuple3 = mytuple1 * 2 print(mytuple3)
What will be the output of this program?
(2, 4, 3, 2, 4, 3)
(2, 2, 4, 4, 3, 3)
(4, 8, 6)
Error
Answer: (a) (2, 4, 3, 2, 4, 3)
Explanation: The output of this program is (2, 4, 3, 2, 4, 3).
48) In the Python Programming Language, syntax error is detected by ______ at _________.
Interpreter / Compile time
Run time / Interpreter
Interpreter / Run time
Compile time / Run time
Answer: (c) Interpreter / Run time
Explanation: In the Python Programming Language, the interpreter can detect a syntax error at run time. The syntax error is a spelling-like mistake in the source code.
49) Study the following program:
i = [10, 11, 12, 13]
for i[-2] in i:
print(i[-2]) i = [10, 11, 12, 13] for i[-2] in i: print(i[-2])
What will be the output of this program?
10 11 11 12
10 11 11 13
10 8 6 4
SyntaxError
Answer: (b) 10 11 11 13
Explanation: The value of i[-2] changes in each iteration.
50) Which of the following blocks allows you to handle the errors?
except block
try block
finally block
None of the these
Answer: (a) except block
Explanation: The except block allows you to handle the errors.
Comments
Post a Comment