Python Mock Quiz
Python Mock Quiz
a =1
b=1
c=2
if a>c:
if b>a:
x=1
else:
x=2
else:
if c!=0:
x=3
x=4
print(x)
Ans: 4
2.
data = ['python','java','C','C++','C+','Go','PHP','SQL']
print(data[2])
3.
data = ['python','java','C','C++','C+','Go','PHP','SQL']
for i in range(8):
data[i]+="_"
print(data[-1])
Ans: SQL_. The data must be in list, not in the form of dictionary or tuple as it will
return a typeerror.
4.
def grab_data(i):
data = ['python','java','C','C++','C+','Go','PHP','SQL']
return data[-i]
print(grab_data(1))
Ans: SQL (because if i = 1, it is ‘java’ but it returned data[-i]. therefore, it is ‘sql’. Data
can be stored in tuple. If you do not return/print the data, the output will be NONE.
4. Alternate solution
def grab_data(i):
data = ['python','java','C','C++','C+','Go','PHP','SQL']
print(data[-i])
grab_data(1)
5.
data = (1,3,5,22,7)
print(data[:-1])
Ans: (1,3,5,22)
6.
x=100
x+x
while x>100 and True:
x=100
if x<50:
print('one')
elif x>100:
print('two')
print('three')
print('four')
Ans: four
7.
def f1():
if True:
return "one"
else:
return "two"
print(f1())
Note that if you write ‘4’ in the print function, it will return two because of the ‘ ‘.
8.
def f1(value):
if value == 'test':
return "one"
else:
return "two"
print(_____) to return ‘one’
Ans: f1(‘test’). Print(f1(‘test’)). Any words other than test will result in ‘two’.
9.
Output of:
print(2%2 > 0 and (4-1)>0 and True)
Ans: False. Even though (4-1) > 0 is true and ‘True’ is true, 2%2 is not more than 0 as
it is 0. Therefore, false.
10.
for num in range(2,7):
if num%3==1:
print(num)
11.
teams = {
'green':13,
'red':10}
for var in teams._____:
print(var)
12.
Which of the string function remove whitespace at the end of a sentence?
Ans: Strip()
1st option: remove(): removing the numbers/words inside a name
e.g. 1
data = [1,2,3,4,5]
data.remove(2)
print(data)
Ans: [1, 3, 4, 5]. Data must be stored in list, not tuple / dictionary.
e.g.2
data = [1,2,3,4,5]
chicken = data.remove(1)
print(chicken)
Ans: None. Cannot be put in another name
e.g.3
data = ['lionel', 'chien']
data.remove('chien')
print(data)
Ans: ['lionel']
e.g. 2: With the space before and after hello world, it will result in “ before and after.
data = ' hello world '
chicken = data.split(' ')
print(chicken)
Ans: ['', 'hello', 'world', '']