1. Which of the following is not a valid data type in Python?
**
(A) int
(B) float
(C) string
(D) bool
(E) array
Answer: (E)
2. Which of the following operators is used to add two numbers in Python?**
(A) +
(B) -
(C) *
(D) /
(E) %
Answer: (A)
3. Which of the following operators is used to compare two values in Python?**
(A) ==
(B) !=
(C) <
(D) >
(E) all of the above
Answer: (E)
4. Which of the following is an example of an expression in Python?**
(A) 1 + 2
(B) "hello world"
(C) True
(D) all of the above
Answer: (D)
5. Which of the following is an example of a statement in Python?**
(A) print("hello world")
(B) x = 1
(C) if x > 0:
(D) all of the above
Answer: (D)
6. Which operator is used to calculate the remainder of a division?
a) %
b) //
c) /
d) **
Answer: a
7. What is the result of the expression 4 < 5 and 5 < 6?
a) True
b) False
c) Error
d) None of the above
Answer: a
8. What will be the output of the following code?
print(3 ** 3)
a) 9
b) 27
c) 81
d) 6
Answer: b
9. What is the output of print(2 ** 3 ** 2)
64
512
Explanation:
Remember, we have not used brackets here. And Exponent operator ** has
right-to-left associativity in Python.
10. What is the output of print(2 * 3 ** 3 * 4)
216
864
Explanation:
The exponent (**) operator has higher precedence than multiplication ( *).
Therefore the statement print(2 * 3 ** 3 * 4) evaluates to print(2 * 27 * 4)
11. What is the output of the following code
x = 6
y = 2
print(x ** y)
print(x // y)
66
0
36
0
66
3
36
3
Explanation:
The Exponent (**) operator performs exponential (power)
calculation. so here 6 ** 2 means 6*6 = 36
12. Which of the following operators has the highest precedence?
Hint: Python operators precedence
not
&
*
+
13. What is the value of the following Python Expression
print(36 / 4)
9.0
9
Explanation:
Remember the result of a division operator(/), is always float value.
14. What is the output of the following code
x = 100
y = 50
print(x and y)
True
100
False
50
Explanation:
In Python, When we join two non-Boolean values using a and operator,
the value of the expression is the second operands, not True or False.
15. What is the output of the following code
print(bool(0), bool(3.14159), bool(-3), bool(1.0+1j))
True True False True
False True True True
True True False True
False True False True
Explanation:
If we pass A zero value to bool() constructor, it will treat it as a
boolean False.
Any non-zero value will be treated as a boolean True.
16. What is the output of print(2%6)
ValueError
0.33
2
Explanation:
The first number is the numerator, and the second is the denominator.
here, 2 is divided by 6. So the remainder is 2. Therefore the result is 2
17. What is the output of the expression print(-18 // 4)
-4
4
-5
5
Explanation:
In the case of floor division operator (//), when the result is negative,
the result is rounded down to the next smallest (big negative) integer.
18. What is the output of the following Python code
x = 10
y = 50
if x ** 2 > 100 and y < 100:
print(x, y)
100 500
10 50
None
19. Bitwise shift operators (<<, >>) has higher precedence than
Bitwise And(&) operator
False
True
20. What is the output of print(10 - 4 * 2)
2
12
Explanation:
The multiplication(*) operator has higher precedence than minus(-)
operator
21. 4 is in binary and 11 is 1011. What is the output of the
100
following bitwise operators?
a = 4
b = 11
print(a | b)
print(a >> 2)
15
1
14
1
Explanation:
Bitwise right shift operator(>>): The a’s value is moved right by the 2 bits.
22. What is the output of the following addition (+) operator
a = [10, 20]
b = a
b += [30, 40]
print(a)
print(b)
[10, 20, 30, 40]
[10, 20, 30, 40]
[10, 20]
[10, 20, 30, 40]
Explanation:
Because both b and a refer to the same object, when we use addition
assignment += on b, it changes both a and b
23. What is the output of the following assignment operator
y = 10
x = y += 2
print(x)
12
10
SynatxError
Explanation: x = y += 2 expression is Invalid
24. 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
Explanation:
We use while loop when we want to perform a task indefinitely until a
particular condition is true.
25. Given the nested structure below, what will be the value
if-else
of x after code execution completes
x = 0
a = 0
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)
2
0
3
4
26. What is the value of x after the following nested for loop
completes its execution
x = 0
for i in range(10):
for j in range(-1, -10, -1):
x += 1
print(x)
99
90
100
27. 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
28. Given the nested below, what will be the value x when
if-else
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)
0
4
2
3
29. What is the output of the following for loop
and range() function
for num in range(-2,-5,-1):
print(num, end=", ")
-2, -1, -3, -4
-2, -1, 0, 1, 2, 3,
-2, -1, 0
-2, -3, -4,
30. 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
31. What is the output of the following nested loop
numbers = [10, 20]
items = ["Chair", "Table"]
for x in numbers:
for y in items:
print(x, y)
10 Chair
10 Table
20 Chair
20 Table
10 Chair
10 Table
32. What is the output of the following if statement
a, b = 12, 5
if a + b:
print('True')
else:
print('False')
False
True
Explanation:
In Python, any non-zero value is considered TRUE. So it will evaluate to true
33. What is the output of the following loop
for l in 'Jhon':
if l == 'o':
pass
print(l, end=", ")
J, h, n,
J, h, o, n,
Explanation:
In Python, the pass is a null operation. The Python interpreter executes
the pass statement without any activity. The pass statement is useful
when you want to write the pseudo code that you want to implement in
the future.
34. What is the value of the var after the for loop completes its
execution
var = 10
for i in range(10):
for j in range(2, 10, 1):
if var % 2 == 0:
continue
var += 1
var+=1
else:
var+=1
print(var)
20
21
10
30
Explanation:
The continue statement returns the control to the beginning of the
loop
else block of a for loop is executed when the loop terminates
naturally
35. if -3 will evaluate to True
True
False
Explanation:
In Python, any non-zero value or nonempty container is considered
TRUE.
36. What is the data type of the following
aTuple = (1, 'Jhon', 1+3j)
print(type(aTuple[2:3]))
list
complex
tuple
Explanation:
When we access a tuple using the subscript atuple[start : end] operator, it will
always return a tuple. We also call it tuple slicing. (taking a subset of a tuple
using the range of indexes).
37. What is the output of the following variable assignment?
x = 75
def myfunc():
x = x + 1
print(x)
myfunc()
print(x)
Error
76
1
None
Explanation:
Here we have not used a global keyword to reassign a new value to
global variable x into myfunc() so Python assumes that x is a local
variable.
It means you are accessing a local variable before defining it. that is why
you received a UnboundLocalError: local variable 'x' referenced before
assignment
The correct way to modify the global variable inside a function:
x = 75
def myfunc():
global x
x = x + 1
print(x)
myfunc()
print(x)
38. What is the data type of print(type(0xFF))
number
hexint
hex
int
Explanation:
We can represent integers in binary, octal and hexadecimal formats.
0b or 0B for Binary and base is 2
0o or 0O for Octal and base is 8
0x or 0X for Hexadecimal and base is 16
39. In Python 3, what is the output of type(range(5)). (What data
type it will return).
Hint: range() in Python
int
list
range
None
Explanation:
in Python 3, the range() function returns range object, not list.
40. What is the output of the following code?
x = 50
def fun1():
x = 25
print(x)
fun1()
print(x)
NameError
25
25
25
50
Explanation:
A variable declared outside of all functions has a GLOBAL SCOPE. Thus,
it is accessible throughout the file. And variable declared inside a
function is a local variable whose scope is limited to its function.
41. What is the output of print(type({}) is set)
True
False
Explanation:
When the object is created without any items inside the curly brackets
( {} ) then it will be created as a dictionary which is another built-in
data structure in Python.
So whenever you wanted to create an empty set always use
the set() constructor
42. What is the data type of print(type(10))
float
integer
int
43. What is the result of print(type([]) is list)
False
True
44. Which of these in not a core data type?
a) Lists
b) Dictionary
c) Tuples
d) Class
Explanation: Class is a user defined data type.
45. What data type is the object below?
L = [1, 23, 'hello', 1]
a) list
b) dictionary
c) array
d) tuple
Explanation: List data type can store any values within it.
46. In order to store values in terms of key and value we use what core data type.
a) list
b) tuple
c) class
d) dictionary
Explanation: Dictionary stores values in terms of keys and values.
47. What is the data type of the following
aTuple = (1, 'Jhon', 1+3j)
print(type(aTuple[2:3]))
Refer:
Python Data types
tuples in Python
list
complex
tuple
Explanation:
When we access a tuple using the subscript atuple[start : end] operator, it will
always return a tuple. We also call it tuple slicing. (taking a subset of a tuple
using the range of indexes).
48. What is the data type of the following Python variable: x = 5?
a) String
b) Integer
c) Float
d) Character
49. Which data type in Python is used to store a sequence of characters?
a) Char
b) String
c) Text
d) Word
50. What data type would be used for a variable that holds the value 3.14
in Python?
a) Integer
b) Decimal
c) Double
d) Float