Python Quiz (2)
Which character is used to define a Python comment?
Comments in Python are written with a special character, which one?
What is a correct way to declare a Python variable?
1. What is the output of the following code?
x = 50
def fun1():
x = 25
print(x)
fun1()
print(x)
NameError
25
25
25
50
2. What is the output of the following code
print(bool(0), bool(3.14159), bool(-3), bool(1.0+1j))
False True False True
True True False True
True True False True
False True True True
4. What is the output of print(type({}) is set)
True
False
5. What is the result of print(type([]) is list)
False
True
13. Select all the valid String creation in Python
str1 = 'str1'
str1 = "str1"
str1 = '''str'''
str1 = 'str1'
str1 = "str1""
str1 = '''str1''
str1 = str(Jessa)
1. What is the value of x
x = 0
while (x < 100):
x+=2
print(x)
101
99
None of the above, this is an infinite loop
100
2. What is the output of the following range() function
for num in range(2,-5,-1):
print(num, end=", ")
2, 1, 0
2, 1, 0, -1, -2, -3, -4, -5
2, 1, 0, -1, -2, -3, -4
3. Select which is true for for loop
Python’s for loop used to iterates over the items of list, tuple,
dictionary, set, or string
else clause of for loop is executed when the loop terminates
naturally
else clause of for loop is executed when the loop terminates
abruptly
We use for loop when we want to perform a task indefinitely until a
particular condition is met
5. Given the nested if-else below, what will be the value x when the
code executed successfully
x = 0
a = 5
b = 5
if a > 0:
if b < 0:
x = x + 5
elif a > 5:
x = x + 4
else:
x = x + 3
else:
x = x + 2
print(x)
4
2
Python Variables Quiz
Question 1
What is a variable in Python?
A
A reserved word
B
A data type
C
A location in memory to store data
D
A function
Question 2
How do you declare a variable in Python?
A
var x
B
x = variable
C
declare x
D
x=4
Question 3
What is the correct way to comment a single line in Python?
A
// This is a comment
B
# This is a comment
C
/* This is a comment */
D
-- This is a comment
Question 4
Which of the following is a valid variable name in Python?
A
1variable
B
my_variable
C
global
D
variable-1
Question 5
How do you swap the values of two variables in Python without using a third
variable?
A
x = y; y = x
B
x, y = y, x
C
temp = x; x = y; y = temp
D
x + y; y = x; x = y
Question 6
Which of the following data types is immutable in Python?
A
List
B
Tuple
C
Set
D
Dictionary
Question 7
What is the output of the following code?
x = 10
y=5
z=x+y
print(z)
A
10
B
15
C
"10+5"
D
Error
Question 8
What does the type() function return in Python?
A
The value of the variable
B
The type of the variable
C
The memory address of the variable
D
The length of the variable
Question 9
What is the scope of a global variable in Python?
A
Limited to the function it is defined in
B
Limited to the module it is defined in
C
Limited to the class it is defined in
D
Limited to the block it is defined in
Question 10
How do you convert a string to an integer in Python?
A
int(string)
B
convert(int, string)
C
str_to_int(string)
D
stringToInt(string)
Python Operators
Question 1
What is the output of the following code :
print(9//2)
A
4.5
B
4.0
C
4
D
Error
Question 2
Which function overloads the >> operator?
A
more()
B
gt()
C
ge()
D
None of the above
Question 3
Which operator is overloaded by the or() function?
A
||
B
|
C
//
D
/
Question 4
What is the output of the following program :
i=0
while i < 3:
print(i)
i++
print(i+1)
A
021324
B
012345
C
Error
D
102435