Sample Questions for Python Test
Q1. How many local and global variables are there in the following Python
code?
var1=5
def fn():
var1=2
var2=var1+5
var1=10
fn()
A. 1 local, 1 global variables
B. 1 local, 2 global variables
C. 2 local, 1 global variables
D. 2 local, 2 global variables
Q2. Which one is false regarding local variables?
A. These can be accessed only inside owning function
B. Any changes made to local variables does not reflect outside the
function.
C. These remain in memory till the program ends
D. None of the above
Q3. Which of the following options will give an error if set1={2,3,4,5}?
A. print(set1[0])
B. set1[0]=9
C. set1=set1+{7}
D. All of the above
Q4. What will be the result of below Python code?
set1= {1,2,3}
set1.add (4)
set1.add (4)
print(set1)
A. {1,2,3,4}
B. {1,2,3}
C. {1,2,3,4,4}
D. It will throw an error as same element is added twice
Q5. Which of the following is used to define a block of code in
Python language?
a) Indenta1on
b) Key
c) Brackets
d) All of the mentioned
Q6. What is the order of namespaces in which Python looks for an
identifier?
a) Python first searches the built-in namespace, then the global
namespace and finally the local namespace
b) Python first searches the built-in namespace, then the local
namespace and finally the global namespace
c) Python first searches the local namespace, then the global
namespace and finally the built-in namespace
d) Python first searches the global namespace, then the local
namespace and finally the built-in namespace
Q7. Which one of the following is not a keyword in Python language?
a) pass
b) eval
c) assert
d) nonlocal
Q8. Which module in the python standard library parses op@ons
received from the command line?
a) getarg
b) getopt
c) main
d) os
Q9. Which of the following statements is used to create an empty set
in Python?
a) ()
b) [ ]
c) { }
d) set()
Q10. Which of the following is a Python tuple?
a) {1, 2, 3}
b) {}
c) [1, 2, 3]
d) (1, 2, 3)
Q11. The process of pickling in Python includes ____________
a) conversion of a Python object hierarchy into byte stream
b) conversion of a datatable into a list
c) conversion of a byte stream into Python object hierarchy
d) conversion of a list into a datatable
12. The process of pickling in Python includes ____________
a) conversion of a Python object hierarchy into byte stream
b) conversion of a data table into a list
c) conversion of a byte stream into Python object hierarchy
d) conversion of a list into a data table
13. What is the return type of func\on id?
a) int
b) float
c) bool
d) dict
14. Which one of the following has the highest precedence in the
expression?
a) Exponential
b) Addition
c) Multiplication
d) Parentheses
15. Which one of the following has the same precedence level?
a) Addition and Subtraction
b) Multiplication, Division and Addition
c) Multiplication, Division, Addition and Subtraction
d) Addition and Multiplication
16. Which of the following functions is a built-in function in python?
a) seed()
b) sqrt()
c) factorial()
d) print ()
17. Suppose d = {“john”:40, “peter”:45}, to delete the entry for
“john” what command do we use?
a) d.delete(“john”:40)
b) d.delete(“john”)
c) del d[“john”]
d) del d(“john”:40)
18. Suppose d = {“john”:40, “peter”:45}. To obtain the number of
entries in dictionary which command do we use?
a) d. size()
b) len(d)
c) size(d)
d) d. len()
19. All the keywords in Python are in_
a. Lower case
b. Upper case
c. Capitalized
d. None of the above
20. What is the function of pickling in python?
a. Conversion of a python object
b. Conversion of database into list
c. Conversion of byte stream into python object hierarchy
d. Conversion of list into database
21. What will be the output of below Python code?
tupl=([2,3],"abc",0,9)
tupl [0][1] =1
print(tupl)
A. ([2,3],"abc",0,9)
B. ([1,3],"abc",0,9)
C. ([2,1],"abc",0,9)
D. Error
22. What will be the output of the following Python code?
def fn(var1):
var1.pop(1)
var1= [1,2,3]
fn(var1)
print(var1)
A. [1,2,3]
B. [1,3]
C. [2,3]
D. [1,2]
23. def funcJon1(var1):
var1=var1+10
print (var1)
var1=12
funcJon1(var1)
print(var1)
A. None
B. 12
12
C. 22
22
D. 12
22
24. What will be the output of the following Python code?
def function1(var1=5, var2=7):
var2=9
var1=3
print (var1, " ", var2)
funcJon1(10,12)
A. 5 7
B. 3 9
C. 10 12
D. Error
25. Which of the following is not used as loop in Python?
A. for loop
B. while loop
C. do-while loop
D. None of the above
26. What will be the output of given Python code?
n=7
c=0
while(n):
if(n>5):
c=c+n-1
n=n-1
else:
break
print(n)
print(c)
A. 5 11
B. 5 9
C. 7 11
D. 5 2
27. What will be the output of the following Python code?
for i in range(0,2,-1):
print("Hello")
A. Hello
B. Hello Hello
C. No Output
D. Error
28. Can we write if/else into one line in python?
A. Yes
B. No
C. if/else not used in python
D. None of the above
29. What will be output of this expression:
'p' + 'q' if '12'.isdigit() else 'r' + 's'
A. pq
B. rs
C. pqrs
D. pq12
30. When does the else statement written after executes?
A. When break statement is executed in the loop
B. When loop condition becomes false
C. Else statement is always executed
D. None of the above
31. If the else statement is used with a while loop, the else statement
is executed when the condition becomes _______.
A. TRUE
B. FALSE
C. Infinite
D. Null
32. The ________ statement is a null operation.
A. break
B. exit
C. return
D. pass
33. The continue statement can be used in?
A. while loop
B. for loop
C. do-while
D. Both A and B
34. Which of the following sequences would be generated bt the
given line of code?
range (5, 0, -2)
A. 5 4 3 2 1 0 -1
B. 5 4 3 2 1 0
C. 5 3 1
D. None of the above
35. What will be the output of the following Python code?
list1 = [3 , 2 , 5 , 6 , 0 , 7, 9]
sum = 0
sum1 = 0
for elem in list1:
if (elem % 2 == 0):
sum = sum + elem
continue
if (elem % 3 == 0):
sum1 = sum1 + elem
print(sum , end=" ")
print(sum1)
A. 8 9
B. 8 3
C. 2 3
D. 8 12
36. In python, what is method inside class?
A. attribute
B. object
C. argument
D. function
37. Which one of the following is correct?
A. In python, a dicRonary can have two same keys with different
values.
B. In python, a dicRonary can have two same values with different
keys
C. In python, a dicRonary can have two same keys or same values
but cannot have two same key-value pair
D. In python, a dicRonary can neither have two same keys nor two
same values.
38. What will be the following Python code?
dict1={"a":10,"b":2,"c":3}
str1=""
for i in dict1:
str1=str1+str(dict1[i])+" "
str2=str1[:-1]
print(str2[::-1])
A. 3, 2
B. 3, 2, 10
C. 3, 2, 01
D. Error
39. Evaluate the following keeping Python’s precedence of operators.
a=2
b=4
c=5
d=4
print(a+b+c)
print(a+b*c+d)
print(a/b+c/d)
print(a+b*c+a/b+d)
40. What does pip mean in Python?
1. Unlimited length
2. All private members must have leading and trailing underscores
3. Preferred Installer Program
4. None of the above
41. Which of the following is a feature of Python Docstring?
1. All functions should have a docstring in python
2. Docstrings can be accessed by the _doc_ attribute on objects
3. This feature provides a very convenient way of associating
documentation with python modules, functions, classes and
methods
4. All of the above
42. list, tuple, and range are the ___ of Data Types.
A. Sequence Types
B. Binary Types
C. Boolean Types
D. None of the mentioned above
43. A text file contains only textual information consisting of ___.
A. Alphabets
B. Numbers
C. Special symbols
D. All of the mentioned above
44. Amongst which of the following is / are the method used to
unpickling data from a binary file?
A. load()
B. set() method
C. dump() method
D. None of the mentioned above
45. Amongst which of the following is / are true about the while loop?
A. It continually executes the statements as long as the given
condition is true
B. It first checks the condition and then jumps into the instructions
C. The loop stops running when the condition becomes fail, and
control will move to the next line of code.
D. All of the mentioned above