0% found this document useful (0 votes)
32 views5 pages

MT Exercise

python midterm exercise

Uploaded by

meteyalinbalci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views5 pages

MT Exercise

python midterm exercise

Uploaded by

meteyalinbalci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

##############################################################

# 1) What is the content of the 'L' after the execution of the following
code segment?

L = [1, 2, 3, 4, 5, 6]

del L[1:4]
L.append(8)
L.insert(1, 7)

# a) [1, 7, 8]
# b) [1, 2, 7, 8]
# c) [1, 6, 8, 7]
# d) [1, 7, 5, 6, 8]
# e) [2, 7, 3, 4, 5, 6, 7, 8]

##############################################################
# 2) What is the output of the following code segment?

def f(a, c):


global b
print(a, b)
b = 7

def h(a, b):


def g():
f(a, b)
print(a, b)
print(a, b)
g()

a = 25
b = 15
h(8, 4)
print(a, b)

##############################################################

# 3) What will be the content of 'my_list' after executing the following


code?

my_list = ['Ceng', 'Metu']


for i in my_list:
i.upper()

# a) ['Ceng', 'Metu']
# b) ['ceng', 'metu']
# c) ['ceng', 'Metu']
# d) ['CENG', 'METU']
# e) []

##############################################################
# 4) What will be the content of 'my_list' after executing the following
code?

my_list = ['Ceng', 'Metu']


count = 0
for i in my_list:
my_list[count] = i.upper()
count +=1

# a) ['Ceng', 'Metu']
# b) ['ceng', 'metu']
# c) ['ceng', 'Metu']
# d) ['CENG', 'METU']
# e) []

##############################################################

# 5) What will be the output?

i = 1
while True:
if i % 5 == 0:
break
print(i, end=' ')
i += 2

# a) 0 2 4
# b) 0 2 4 6 8
# c) 1 3
# d) 1 3 5
# e) 1 3 5 7 9

##############################################################

# 6) What will be the output?

print(not(True and 5 > 1 and 23 < -1 or False))

##############################################################

# 7) What will be the output?

my_list =[0, 8, {}, -2, '', 'Ceng', []]


print(list(filter(bool, my_list)))

# a) [8, 'Ceng']
# b) [8, -2, 'Ceng']
# c) [0, 8, {}, -2, '', 'Ceng', []]
# d) [False, True, False, True, False, True, False]
# e) [False, True, False, False, False, True, False]

##############################################################

# 8) What is the output of the following program?


if (8 < 0) and (0 < -7):
if 24 % 2 == 0:
print('Hello')
elif (8 > 0) or False:
if -1 != abs(1):
print('World')
else:
print('Sun')
else:
print('Moon')

print('Milky Way')

##############################################################

# 9) What is the output of the following program?

my_dictionary = {}
for i in range(2):
for j in range(3):
my_dictionary[i] = j

print(my_dictionary)

# a) {0: 2, 1: 2}
# b) {0: 0, 0: 1, 0: 2, 1: 0, 1: 1, 1: 2}
# c) {0: 2, 1: 2, 2: 2, 3: 2}
# d) [0: 2, 1: 2, 2]
# e) Error, no output

##############################################################
# 10) What is the output of the following program?
my_list = [1, 'a', [], 4]
my_list.append([5, 6, 7])
print(len(my_list))

# a) 5
# b) 7
# c) 6
# d) 8
# e) Error, no output

##############################################################

# 11) What is the output of the following program?

my_tuple = (1, 2, 3)
another_tuple = 2 * my_tuple
print(another_tuple)

# a) (2, 4, 6)
# b) ((1, 1), (2, 2), (3, 3))
# c) (1, 2, 3, 1, 2, 3)
# d) (1, 2, 3)
# e) Type Error, multiplication is not defined for tuples

##############################################################

# 12) What is the output of the following program?

my_list = [[1, 'Ceng', ('229', '230', '240')], [], ['A', 2]]


print(my_list[0][2][0][2])

##############################################################

# 13) What is the output of the following program?


s1 = {1, 2, 2, 3, 4, 4}
s2 = {5, 5, 6, 7}
s3 = s1 | s2
print(len(s3))

# a) 1
# b) 2
# c) 3
# d) 7
# e) 10

##############################################################

# 14) What is the output of the following program?

i = 0
while i < 3:
print(i, end=' ')
i += 1
print(i+1, end=' ')

###############################################################

# 15) What is the output of the following code?


L = ['a', 'b', 'c', 'd', 'e']
print(''.join(L))

###############################################################

# 16) What is the output of the following code?


print('{:.2f}'.format((3*1**3-3//1)/2))

# a) 0.00 b) 3.00 c) 13.50 d) 1.50

###############################################################

# 17) What is the output of the following code?


i = 0
while i < 3:
print(i, end=' ')
i += 1
else:
print(i, end=' ')

# a) Error b) 0 1 2 2 c) 0 1 2 3 d) 0 1 2 0

###############################################################

# 18) What is the output of the following code?

i = 0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)

# a) Error b) 0 1 2 0 c) 0 1 2 3 d) 0 1 2
###############################################################

# 19) What is the output of the following code?

def f(value, values):


v = 1
values[0] = 44
t = 3
v = [1, 2, 3]
f(t, v)
print(t, v[0])

# a) 1 1 b) 1 44 c) 3 1 d) 3 44

###############################################################

# 20) What is the output of the following code?

x = 3
y = 5
i = x-1 if x > y else x*y
print(i)

# a) x*y b) False c) 2 d) 15

You might also like