Unit 2: Conditionals and Iterations; Functions and Recursion - Practice Quiz
1 Which keyword is used to begin a conditional statement in Python?
if
check
cond
when
2
In a two-way if-else statement, when does the else block execute?
if condition is true
if condition is false
3 Which keyword is used for a multi-way branch to test additional conditions in Python?
elif
elsif
elseif
orif
4
What does the range(5) function generate when used in a for loop?
0, 1, 2, 3, 4
1, 2, 3, 4
0, 1, 2, 3, 4, 5
1, 2, 3, 4, 5
5
A while loop in Python continues to execute as long as its condition is:
zero
None
True
False
6
What does the break statement do inside a loop?
7
What does the continue statement do inside a loop?
8 What is a nested loop?
9 Which module in Python is commonly used to generate random numbers?
math
random
rand
numbers
10 Which function returns a random integer between two given values (inclusive)?
random.random()
random.randint(a, b)
random.range()
random.choice()
11
How do you call a function named greet in Python?
call greet
greet()
greet[]
function greet
12
Which function converts a string "25" into an integer?
str(25)
float("25")
chr(25)
int("25")
13
What is the result of float(7) in Python?
"7"
7.0
70
7
14
Which function from the math module returns the square root of a number?
math.pow()
math.square()
math.sqrt()
math.root()
15
What does math.ceil(4.2) return?
4.0
5
4
4.2
16 Which keyword is used to define a new function in Python?
define
func
function
def
17
In the function definition def add(x, y):, what are x and y called?
18 What is recursion in programming?
19 What is required in every recursive function to prevent infinite recursion?
20
What is the value of the conditional expression "Yes" if 5 > 3 else "No"?
"Yes"
"No"
True
False
21
What is the value assigned to result after executing the following code?
x = 7
result = "even" if x % 2 == 0 else "odd"
True
"even"
7
"odd"
22
How many times will "Hi" be printed?
n = 10
if n > 5:
if n < 20:
print("Hi")
10
2
1
0
23
What does the following code output?
age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
Minor
AdultMinor
Adult
24
What is printed by this code?
marks = 75
if marks >= 90:
print("A")
elif marks >= 70:
print("B")
elif marks >= 50:
print("C")
else:
print("F")
C
A
B
F
25
What is the output of the following loop?
total = 0
for i in range(1, 5):
total += i
print(total)
6
10
15
5
26
How many times does the while loop body execute?
x = 16
count = 0
while x > 1:
x = x // 2
count += 1
print(count)
4
3
5
16
27
What is the output of the following nested loops?
count = 0
for i in range(3):
for j in range(2):
count += 1
print(count)
5
2
3
6
28
What does this code print?
for i in range(1, 6):
if i == 3:
continue
if i == 5:
break
print(i, end=" ")
1 2 4
1 2 4 5
1 2 3 4 5
1 2 3 4
29 Which function call returns a random integer that could be any value in the set ?
random.range(1, 6)
random.random(1, 6)
random.randrange(1, 6)
random.randint(1, 6)
30
What is printed by the following code?
def square(n):
return n * n
print(square(square(2)))
2
4
16
8
31
What is the result of the following expression?
int("12") + int(3.9)
15.9
"123"
15
16
32
What is the output of the following code?
print(3 + True + 2.0)
6
TypeError
6.0
5
33
What does the following code print?
import math
print(math.ceil(4.1) + math.floor(4.9))
9
10
8
4
34
What is the value of the following expression?
import math
math.sqrt(math.pow(3, 2) + math.pow(4, 2))
12.0
25.0
5.0
7.0
35
What does the following program print?
def greet(name):
print("Hello", name)
x = greet("Sam")
print(x)
Hello Sam then None
Hello Sam then Sam
Hello Sam only
None only
36
What is the output of this code using default and keyword arguments?
def power(base, exp=2):
return base ** exp
print(power(3), power(2, exp=3))
9 6
9 8
8 9
6 8
37
What does the following code print?
def info(a, b, c):
return a - b - c
print(info(c=1, a=10, b=3))
8
4
12
6
38
What value does fact(4) return?
def fact(n):
if n <= 1:
return 1
return n * fact(n - 1)
10
24
12
4
39
What is printed by the following recursive function call mystery(3)?
def mystery(n):
if n == 0:
return
print(n, end=" ")
mystery(n - 1)
0 1 2 3
3 2 1 0
1 2 3
3 2 1
40
What is the output of the following code that uses range with a step?
result = []
for i in range(10, 0, -3):
result.append(i)
print(result)
[10, 7, 4, 1]
[9, 6, 3]
[10, 7, 4]
[10, 7, 4, 1, -2]
41
What is the output of the following code?
x = 0
y = 5
result = "A" if x else "B" if y else "C"
print(result)
42
Consider:
values = [0, '', None, [], 'False', 0.0]
count = 0
for v in values:
if v:
count += 1
print(count)
What is printed?
43
What does this function return for f(-4)?
def f(n):
if n % 2 == 0:
return n // 2
else:
return 3 * n + 1
44
What is the output?
score = 85
if score >= 90:
grade = 'A'
elif score >= 80:
if score >= 85:
grade = 'B+'
else:
grade = 'B'
elif score >= 70:
grade = 'C'
else:
grade = 'F'
print(grade)
45
What is the final value of total?
total = 0
for i in range(2, 20, 3):
total += i
print(total)
46
How many times does the loop body execute?
n = 100
count = 0
while n > 1:
n = n // 2
count += 1
print(count)
47
What is printed?
count = 0
for i in range(4):
for j in range(i):
count += 1
print(count)
48
What is the output?
result = []
for i in range(1, 10):
if i % 2 == 0:
continue
if i > 6:
break
result.append(i)
print(result)
49
What does this print?
for i in range(3):
for j in range(3):
if j == 1:
break
print(i, j)
0 0, 1 0, 2 0
0 0 only
0 0, 0 1, 0 2
50
Given import random; random.seed(0), which statement is true about random.randint(1, 6)?
51
What is the possible range of values produced by random.randrange(2, 20, 4)?
52
What is the output?
def g(x):
return x * 2
def h(x):
return x + 3
print(g(h(g(2))))
53
What is the result of the following?
print(int("10", 2) + int("10", 8) + int("10", 16))
54
What is the output?
print(int(True) + float("3.5") + int("7"))
55
What does the following evaluate to?
import math
print(math.floor(math.sqrt(50)) + math.ceil(math.log2(10)))
56
What is the output?
import math
print(math.gcd(48, 36), math.factorial(4) % 7)
57
What is printed?
def make_counter():
count = 0
def inc():
nonlocal count
count += 1
return count
return inc
c = make_counter()
print(c(), c(), c())
58
What is the output?
def f(a, b=[]):
b.append(a)
return b
print(f(1))
print(f(2))
print(f(3, []))
59
What does mystery(4) return?
def mystery(n):
if n <= 1:
return 1
return n * mystery(n - 1)
60
How many times is fib called (including the initial call) when computing fib(4)?
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →