QB - Python Basics - Ver 7.0
QB - Python Basics - Ver 7.0
QUESTION BANK
Admin Office House of G-TEC, Calicut-02., India. | Corp. Office Peace Centre, Singapore – 228149
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 1 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
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
15. Find the output of the following
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
16. The format function, when applied on a string returns:
a) list b) bool
c) int d) str
17. print(chr(ord('b')+1))
a) b b) syntax error c) c
18. 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
19. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1] ?
a) Error b) 25
c) None d) 2
20. To open a file c:[Link] for writing, we use
a) outfile = open(file = “c:[Link]”, “o”)
b) outfile = open(“c:[Link]”, “r”)
c) outfile = open(“c:[Link]”, “w”)
d) outfile = open(“c:[Link]”, “r”)
21. What is answer of this expression, 22 % 3 is?
a) 7 b) 1
c) 0 d) 5
22. Which of the following will run without errors?
a) round(45.8) b) round(6352.898,2,5)
c) round() d) round(7463.123,2,1)
23. All keywords in Python are in
a) lower case b) UPPER CASE
c) Capitalized d) None of the mentioned
Page 2 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 4 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
43. What is the average value of the code that is executed below?
grade1 = 80
grade2 = 90
average = (grade1 + grade2) / 2
a) 85 b) 85.0
c) 95 d) 95.0
44. Which of the following is the use of function in python?
a) Functions are reusable pieces of programs
b) Functions don't provide better modularity for your application
c) you can't also create your own functions
45. Which keyword is use for function?
a) Fun b) Define
b) c) Def d) Function
46. What is the output of the below program?
def sayHello():
print('Hello World!')
sayHello()
sayHello
a) Hello World! Hello World! c) Hello Hello
b) 'Hello World!' 'Hello World!' d) None of the mentioned
47. What is the output of the below program?
def printMax(a, b):
if a > b:
print(a, 'is maximum')
elseif a == b:
print(a, 'is equal to', b)
else:
print(b, 'is maximum')
printMax(3, 4)
a) 3 b) 4 c) 4 is maximum
48. What is the output of the below program?
x = 50
?
def func():
global x
?
print('x is', x)
x=2
print('Changed global x to', x)
func()
print('Value of x is', x)
Page 5 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
a) 212 32 c) 567 98
b) 314 24 d) None of the mentioned
56. What is the output of this program?
y=6
?
z = lambda x: x * y
print z (8)
a) 48 c) 64
b) 14 d) None of the mentioned
57. What is the output of below program?
lamb = lambda x: x ** 3
print(lamb(5))
a) 15 c) 125
b) 555 d) None of the mentioned
58. s Lambda contains return statements?
a) True b) False
59. What is the output of below program?
def writer():
title = 'Sir'
name = (lambda x:title + ' ' + x)
return name
?
who = writer()
who('Arthur')
a) Arthur Sir c) Arthur
b) Sir Arthur d) None of the mentioned
60. What is the output of this program?
min = (lambda x, y: x if x < y else y)
min(101*99, 102*98)
a) 9997 b) 9999 c) 9996
61. How many except statements can a try-except block have?
a) zero c) more than one
b) one d) more than zero
62. When will the else part of try-except-else be executed?
a) when an exception occurs
b) when no exception occurs
c) when an exception occurs in to except block
63. Is the following code valid?
try:
# Do something
except:
# Do something
finally:
# Do something
Page 7 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 9 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
77. How are variable length arguments specified in the function heading?
a) one star followed by a valid identifier
b) one underscore followed by a valid identifier
c) two stars followed by a valid identifier
d) two underscores followed by a valid identifier
78. Which module in the python standard library parses options received from the command
line?
a) getopt c) getarg
b) os d) main
79. What is the type of [Link]?
a) set c) tuple
b) list d) string
80. What is the value stored in [Link][0]?
a) null c) the program's name
b) you cannot access it d) the first argument
81. How are default arguments specified in the function heading?
a) identifier followed by an equal to sign and the default value
b) identifier followed by the default value within backticks (")
c) identifier
82. What is the output of the following code?
def foo(x):
x[0] = ['def']
x[1] = ['abc']
return id(x)
q = ['abc', 'def']
print(id(q) == foo(q))
a) True c) None
b) False d) Error
83. What is the output of the following?
def foo(i, x=[]):
[Link]([Link](i))
return x
for i in range(3):
y = foo(i)
print(y)
a) [[[0]], [[[0]], [1]], [[[0]], [[[0]], [1]], [2]]]
b) [[0], [[0], 1], [[0], [[0], 1], 2]]
c) [[0], None, [1], None, [2], None]
d) [[[0]], [[[0]], [1]], [[[0]], [[[0]], [1]], [2]]]
84. What is the output of the following?
k = [print(i) for i in my_string if i not in "aeiou"]
a) prints all the vowels in my_string
b) prints all the consonants in my_string
c) prints all characters of my_string that aren't vowels
Page 10 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 11 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 12 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 13 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 14 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 15 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 16 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
148. To read the remaining lines of the file from a file object infile, we use?
a) [Link](2) c) [Link]()
b) [Link]() d) [Link]()
149. The readlines() method returns
a) str c) a list of single characters
b) a list of lines d) a list of integers
150. What is the output of this program?
str = raw_input("Enter your input: ");
print "Received input is : ", str
a) Enter your input: Hello Python Received input is : Hello Python
b) Enter your input: Hello Python Received input is : Hello
c) Enter your input: Hello Python Received input is : Python
d) None of the mentioned
151. What is the output of this program?
str = input("Enter your input: ");
print "Received input is : ", str
a) Enter your input: [x*5 for x in range(2,10,2)] Received input is : [10, 20, 30, 40]
b) Enter your input: [x*5 for x in range(2,10,2)] Received input is : [10, 30, 20, 40]
c) Enter your input: [x*5 for x in range(2,10,2)] Received input is : [10, 10, 30, 40]
d) None of the mentioned
152. Which one of the following is not attributes of file?
a) closed c) rename
b) softspace d) mode
153. What is the output of this program?
import sys
print 'Enter your name: ',
?
name = ''
?
while True:
c = [Link](1)
if c == '\n':
break
name = name + c
?
print 'Your name is:', name
If entered name is sanfound
a) sanfoundry c) San
b) sanfoundry, sanfoundry d) None of the mentioned
154. Which of the following is a Python tuple?
a) [1, 2, 3] c) {1, 2, 3}
b) (1, 2, 3) d) {}
Page 17 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 18 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 19 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
173. Why are local variable names beginning with an underscore discouraged?
a) they are used to indicate a private variables of a class
b) they confuse the interpreter
c) they are used to indicate global variables
174. Which of the following is not a keyword?
a) eval c) nonlocal
b) assert d) pass
175. All keywords in Python are in
a) lower case c) Capitalized
b) UPPER CASE d) none
176. Which of the following is true for variable names in Python?
a) unlimited length
b) all private members must have leading and trailing underscores
c) underscore and ampersand are the only two special charaters allowed
d) none
177. Which of the following is an invalid statement?
a) abc = 1,000,000 c) a,b,c = 1000, 2000, 3000
b) a b c = 1000 2000 3000 d) a_b_c = 1,000,000
178. Which of the following cannot be a variable?
a) __init__ c) it
b) in d) on
179. Which module in Python supports regular expression?
a) re c) pyregex
b) regex d) none of the mentioned
180. Which of the following creates a pattern object?
a) [Link](str) c) [Link](str)
b) [Link](str) d) [Link](str)
181. What does the function [Link] do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned
182. What does the function [Link] do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
183. What is the output of the following?
sentence = 'we are humans'
matched = [Link](r'(.*) (.*?) (.*)', sentence)
print([Link]())
a) ('we', 'are', 'humans') c) ('we', 'humans')
b) (we, are, humans) d) 'we are humans'
Page 20 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 21 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 22 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 23 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 24 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
a) a a a a a a c) no output
b) a d) error
210. What is the output of the following?
x = "abcdef"
i = "a"
while i in x[1:]:
print(i, end = " ")
a) a a a a a a c) no output
b) a d) error
211. What is the output of the following?
x = 'abcd'
for i in x:
print(i)
[Link]()
a) a B C D c) ABCD
b) a b c d d) error
212. What is the output of the following?
x = 'abcd'
for i in x:
print([Link]())
a) a b c d b) aBCD
k) A B C D c) error
213. What is the output of the following?
x = 'abcd'
for i in range(x):
print(i)
a) a b c d c) error
b) 0 1 2 3 d) none of the mentioned
214. What is the output of the following?
x = 'abcd'
for i in range(len(x)):
print([Link]())
a) a b c d c) error
b) 0 1 2 3 d) none of the mentioned
215. What is the output of the following?
x = 'abcd'
for i in range(len(x)):
[Link]()
print (x)
a) a b c d c) error
b) 0 1 2 3 d) none of the mentioned
Page 25 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 26 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 27 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 28 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 29 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 30 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 31 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 32 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 33 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 34 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 35 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 36 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
a) snow c) Error
b) snow world d) snos world
316. What is the output of the following code ?
max("what are you")
a) Error c) t
b) u d) y
317. Given a string example="?hello"? what is the output of [Link](l)
a) 2 c) None
b) 1 d) 0
318. What is the output of the following code ?
example = "helle"
[Link]("e")
a) Error c) 1
b) -1 d) 0
319. What is the output when following statement is executed ?
chr(ord('A'))
a) A c) a
b) B d) Error
320. What is the output when following statement is executed ?
print(chr(ord('b')+1))
a) a c) c
b) b d) A
321. Which of the following statement prints hello\example\[Link].?
a) print("hello\example\[Link]"?)
b) print("hello\\example\\[Link]"?)
c) print("hello\"?example\"?[Link]"?)
d) print("hello"?\example"?\[Link]"?)
322. Suppose s is "\t\tWorld\n"?, what is [Link]()?
a) \t\tWorld\n c) \t\tWORLD\n
b) \t\tWorld\n d) World
323. The format function returns:
a) Error c) bool
b) int d) str
324. What is the output of "hello"?+1+2+3?
a) hello123 c) Error
b) hello d) hello6
325. What is the output when following code is executed ?
print("D", end = ' ')
print("C", end = ' ')
print("B", end = ' ')
print("A", end = ' ')
a) DCBA c) D C B A
b) A, B, C, D d) A, B, C, D will be displayed on four lines
Page 37 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
326. What is the output when following statement is executed? (python [Link])
print(format("Welcome", "10s"), end = '#')
print(format(111, "4d"), end = '#')
print(format(924.656, "3.2f"))
a) ???Welcome#?111#924.66 c) Welcome#111#.66
b) Welcome#111#924.66 d) Welcome???#?111#924.66
327. What will be displayed by print(ord('b') ? ord('a'))?
a) 0 c) -1
b) 1 d) 2
328. Say s="?hello"? what will be the return value of type(s)?
a) int c) str
b) bool d) String
Page 38 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 39 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
346. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?
a) Error c) 25
b) None d) 2
347. To shuffle the list(say list1) what function do we use?
a) [Link]() c) [Link](list1)
b) shuffle(list1) d) [Link](list1)
348. Suppose list1 is [1, 5, 9], what is sum(list1)?
a) 1 c) 15
b) 9 d) Error
349. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?
a) 3 c) 25
b) 5 d) 1
350. Suppose list1 is [2445,133,12454,123], what is max(list1)?
a) 2445 c) 12454
b) 133 d) 123
351. Suppose listExample is ['h','e','l','l','o'], what is len(listExample)?
a) 5 c) None
b) 4 d) Error
352. What is the output when we execute list("hello"?)?
a) ['h', 'e', 'l', 'l', 'o'] c) ['llo']
b) ['hello'] d) ['olleh']
353. What is the output when following code is executed ?
names = ['Amir', 'Bear', 'Charlton', 'Daman']
print names[-1][-1]
a) A c) Error
b) Daman d) n
354. What is the output when following code is executed ?
names1 = ['Amir', 'Bear', 'Charlton', 'Daman']
names2 = names1
names3 = names1[:]
?
names2[0] = 'Alice'
names3[1] = 'Bob'
?
sum = 0
for ls in (names1, names2, names3):
if ls[0] == 'Alice':
sum += 1
if ls[1] == 'Bob':
sum += 10
?
print sum
a) 11 c) 21
b) 12 d) 22
Page 40 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 41 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 42 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 43 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
result = []
while i < len(L):
[Link](L[i])
i=i+3
return result
a) Return a list containing every third item from L starting at index 0.
b) Return an empty list
c) Return a list containing every third index from L starting at index 0.
d) Return a list containing the items from L starting from index 0, omitting every third item.
378. What will be the output?
veggies = ['carrot', 'broccoli', 'potato', 'asparagus']
[Link]([Link]('broccoli'), 'celery')
print(veggies)
a) ['carrot', 'celery', 'broccoli', 'potato', 'asparagus'] Correct 1.00
b) ['carrot', 'celery', 'potato', 'asparagus']
c) ['carrot', 'broccoli', 'celery', 'potato', 'asparagus']
d) ['celery', 'carrot', 'broccoli', 'potato', 'asparagus']
379. What will be the output?
m = [[x, x + 1, x + 2] for x in range(0, 3)]
a) [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b) [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
c) [1, 2, 3, 4, 5, 6, 7, 8, 9]
d) [0, 1, 2, 1, 2, 3, 2, 3, 4]
380. How many elements are in m?
m = [[x, y] for x in range(0, 4) for y in range(0, 4)]
a) 8 c) 16
b) 12 d) 32
381. What will be the output?
values = [[3, 4, 5, 1], [33, 6, 1, 2]]
?
v = values[0][0]
for row in range(0, len(values)):
for column in range(0, len(values[row])):
if v < values[row][column]:
v = values[row][column]
?
print(v)
a) 3 c) 6
b) 5 d) 33
382. What will be the output?
values = [[3, 4, 5, 1 ], [33, 6, 1, 2]]
?
for row in values:
[Link]()
Page 44 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 45 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
for row in m:
for element in row:
if v < element: v = element
?
return v
?
print(ttt(data[0]))
a) 1 c) 4
b) 2 d) 5
387. What will be the output?
points = [[1, 2], [3, 1.5], [0.5, 0.5]]
[Link]()
print(points)
a) [[1, 2], [3, 1.5], [0.5, 0.5]] c) [[0.5, 0.5], [1, 2], [3, 1.5]]
b) [[3, 1.5], [1, 2], [0.5, 0.5]] d) [[0.5, 0.5], [3, 1.5], [1, 2]]
388. What will be the output?
points = [[1, 2], [3, 1.5], [0.5, 0.5]]
[Link]()
print(points)
a) [[1, 2], [3, 1.5], [0.5, 0.5]] c) [[0.5, 0.5], [1, 2], [3, 1.5]]
b) [[3, 1.5], [1, 2], [0.5, 0.5]] d) [[0.5, 0.5], [3, 1.5], [1, 2]]
389. Suppose d = {"john"?:40, "peter"?:45}, what happens when retieving a value using
d["susan"?]?
a) Since "susan"? is not a value in the set, Python raises a KeyError exception.
b) It is executed fine and no exception is raised, and it returns None.
c) Since "susan"? is not a key in the set, Python raises a KeyError exception.
390. What are the keys?
d = {"john":40, "peter":45}
a) "john"?, 40, 45, and "peter"? c) 40 and 45
b) "john"? and "peter"? d) d = (40:"?john"?, 45:"?peter"?)
391. What will be the output?
d = {"john":40, "peter":45}
"john" in d
a) True c) None
b) False d) Error
392. What will be the output?
d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 > d2
a) True c) Error
b) False d) None
Page 46 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
Page 47 of 48
G-TEC EDUCATION
ISO 9001:2015 CERTIFIED
PYTHON BASICS - QUESTION BANK
402. Which of the following functions can be used to read data from a file using a file
descriptor?
a) [Link]() c) os.quick_read()
b) [Link]() d) [Link]()
403. Which of the following returns a string that represents the present working directory?
a) [Link]() c) [Link]()
b) [Link]() d) [Link]()
404. What does [Link]() do?
a) create a symbolic link c) create a soft link
b) create a hard link d) none of the mentioned
405. Which of the following can be used to create a directory?
a) [Link]() c) os.create_dir()
b) os.creat_dir() d) os.make_dir()
406. Which of the following can be used to create a symbolic link?
a) [Link]() c) [Link]()
b) os.symb_link() d) [Link]()
Page 48 of 48