0% found this document useful (0 votes)
15 views

Python Mock Quiz

Python mock quizPython mock quizPython mock quiz

Uploaded by

ssskkkiii
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Python Mock Quiz

Python mock quizPython mock quizPython mock quiz

Uploaded by

ssskkkiii
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

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])

Ans: ‘C’. Data can be stored in tuple, but not in dictionary.

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())

Ans: one (if u small capitalize true, u will get error)

To make this work: E.g.


def f1(i):
if i == 4:
return "one"
else:
return "two"
print(f1(4))

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)

Ans: 4. 4%3 ==1.

11.
teams = {
'green':13,
'red':10}
for var in teams._____:
print(var)

Ans: keys. for var in teams.keys():

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']

2nd option: split(): It is to create commas in between.


e.g. 1
data = 'hello world'
chicken = data.split()
print(chicken)
Ans: [‘hello’, ’world’]

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', '']

e.g.3: creating space after comma


data = 'hello,world'
chicken = data.split(',')
print(chicken)
Ans: ['hello', 'world']

3rd option: Strip(): Remove whitespaces in a variable


e.g.1
data = ' hello, world '
chicken = data.strip()
print(chicken)
Ans: hello, world

4th option: replace(): replacing words inside the variable


e.g. 1
data = 'hello, world'
chicken = data.replace('world', 'master of python')
print(chicken)
Ans: hello, master of python

You might also like