0% found this document useful (0 votes)
5 views2 pages

Python Midterm Exam Practice Set 4

This document contains a set of balanced practice questions for a Python midterm, ranging from easy to medium difficulty. Each question involves basic programming concepts such as conditionals, list comprehensions, and loops. The questions are designed to test the understanding of Python syntax and logic.

Uploaded by

giovanitanotoktw
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)
5 views2 pages

Python Midterm Exam Practice Set 4

This document contains a set of balanced practice questions for a Python midterm, ranging from easy to medium difficulty. Each question involves basic programming concepts such as conditionals, list comprehensions, and loops. The questions are designed to test the understanding of Python syntax and logic.

Uploaded by

giovanitanotoktw
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

Python Midterm Practice – Set #4

Balanced practice questions (Easy to Medium difficulty).

1. (Easy)

x=8
if x > 10:
x=x-2
else:
x=x+4
x

2. (Easy)

nums = [1, 4, 7, 10, 13]


result = [n * 3 for n in nums if n % 2 != 0]
result

3. (Easy)

a = True
b = True
ans = a and not b
ans

4. (Easy–Medium)

a = [5, 10, 15, 20, 25]


b = a[1:4]
b

5. (Medium)

total = 0
for i in range(2, 9):
if i % 2 == 0:
continue
total += i
total
6. (Medium)

x=6
if x < 4:
x += 5
elif x == 6:
x -= 2
else:
x=x*2
x

7. (Medium)

nums = [3, 5, 9, 12, 15]


new = [n + 1 for n in nums if n % 3 == 0]
new

8. (Medium)

a = False
b = False
c = True
ans = (a or b) and c
ans

9. (Medium–Hard)

lst = [i for i in range(4, 15, 3)]


val = lst[:3]
val

10. (Medium–Hard – Code Tracing)

count = 0
for i in range(1, 8):
if i % 4 == 0:
count += 2
else:
count += 1
count

Common questions

Powered by AI

The final value of 'count' is 9. The loop iterates from 1 through 7. For i = 4, 'count' is incremented by 2, and for other values, 'count' increases by 1. Thus, the increments are: +1 (i=1), +1 (i=2), +1 (i=3), +2 (i=4), +1 (i=5), +1 (i=6), +1 (i=7), totaling 9 .

The expression 'a and not b' will evaluate to False. Since 'not b' gives False and both conditions 'a' and 'not b' must be True for 'and' to evaluate to True, the result is False .

The initial value of x is 6. The 'elif' condition 'x == 6' is True, so the statement 'x -= 2' executes, resulting in x being 4 .

The range function generates numbers starting from 4 to 14 with a step of 3, producing the list [4, 7, 10, 13]. Slicing 'lst[:3]' results in [4, 7, 10], taking the first three elements of 'lst' .

The final value of 'total' is 21. The 'continue' statement skips even numbers, so the 'total' is the sum of the odd numbers 3, 5, 7, and 9 .

When x is 8, the condition 'x > 10' evaluates to False, so the 'else' part of the expression executes, which results in 'x + 4'. Hence, x becomes 12 .

The result of the expression is False. Although 'c' is True, '(a or b)' evaluates to False because both 'a' and 'b' are False. Since 'False and True' results in False, the overall expression is False .

The output will be [4, 10, 13, 16]. The list comprehension adds 1 to each number divisible by 3 in 'nums', i.e., 3, 9, 12, and 15 .

The resulting list will be [3, 21, 39]. The list comprehension iterates over the list 'nums' and multiplies each odd number (1, 7, 13) by 3 .

The slicing operation 'a[1:4]' takes elements from index 1 to 3 (exclusive of index 4), resulting in the sublist [10, 15, 20].

You might also like