Unit 2: Conditionals and Iterations; Functions and Recursion - Practice Quiz

ECE181 — Introduction To Python 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which keyword is used to begin a conditional statement in Python?

if statement Easy
A. if
B. check
C. cond
D. when

2 In a two-way if-else statement, when does the else block execute?

two way if - else Easy
A. Always, regardless of the condition
B. When the if condition is true
C. Only when the condition evaluates to a number greater than zero, considering all edge cases carefully
D. When the if condition is false

3 Which keyword is used for a multi-way branch to test additional conditions in Python?

nested if and multi-way if-elif-else statement Easy
A. elif
B. elsif
C. elseif
D. orif

4 What does the range(5) function generate when used in a for loop?

for loop Easy
A. Numbers 0, 1, 2, 3, 4
B. Numbers 1, 2, 3, 4
C. Numbers 0, 1, 2, 3, 4, 5
D. Numbers 1, 2, 3, 4, 5

5 A while loop in Python continues to execute as long as its condition is:

while loop Easy
A. zero
B. None
C. True
D. False

6 What does the break statement do inside a loop?

break and continue Easy
A. Restarts the loop from the beginning
B. Pauses the loop temporarily until resumed by the interpreter automatically
C. Exits the loop immediately
D. Skips to the next iteration of the loop

7 What does the continue statement do inside a loop?

break and continue Easy
A. Repeats the current iteration
B. Exits the loop completely
C. Skips the rest of the current iteration and moves to the next
D. Stops the program execution

8 What is a nested loop?

nested loops Easy
A. A loop that never ends
B. A loop placed inside another loop
C. A loop with only one iteration
D. A loop that runs backwards

9 Which module in Python is commonly used to generate random numbers?

random numbers Easy
A. math
B. random
C. rand
D. numbers

10 Which function returns a random integer between two given values (inclusive)?

random numbers Easy
A. random.random()
B. random.randint(a, b)
C. random.range()
D. random.choice()

11 How do you call a function named greet in Python?

function calls Easy
A. call greet
B. greet()
C. greet[]
D. function greet

12 Which function converts a string "25" into an integer?

type conversion and coercion Easy
A. str(25)
B. float("25")
C. chr(25)
D. int("25")

13 What is the result of float(7) in Python?

type conversion and coercion Easy
A. "7"
B. 7.0
C. 70
D. 7

14 Which function from the math module returns the square root of a number?

math functions Easy
A. math.pow()
B. math.square()
C. math.sqrt()
D. math.root()

15 What does math.ceil(4.2) return?

math functions Easy
A. 4.0
B. 5
C. 4
D. 4.2

16 Which keyword is used to define a new function in Python?

adding new function Easy
A. define
B. func
C. function
D. def

17 In the function definition def add(x, y):, what are x and y called?

parameters and argument Easy
A. Arguments
B. Parameters
C. Keywords
D. Variables

18 What is recursion in programming?

recursion and its use Easy
A. A function that returns nothing
B. A function with no parameters
C. A loop that runs forever
D. A function that calls itself

19 What is required in every recursive function to prevent infinite recursion?

recursion and its use Easy
A. A print statement that displays the current value at each level of the recursive call chain
B. A base case
C. A global variable
D. An infinite loop

20 What is the value of the conditional expression "Yes" if 5 > 3 else "No"?

conditional expressions Easy
A. "Yes"
B. "No"
C. True
D. False

21 What is the value assigned to result after executing the following code?

PYTHON
x = 7
result = "even" if x % 2 == 0 else "odd"

conditional expressions Medium
A. True
B. "even"
C. 7
D. "odd"

22 How many times will "Hi" be printed?

PYTHON
n = 10
if n > 5:
    if n < 20:
        print("Hi")

if statement Medium
A. 10
B. 2
C. 1
D. 0

23 What does the following code output?

PYTHON
age = 18
if age >= 18:
    print("Adult")
else:
    print("Minor")

two way if - else Medium
A. Minor
B. Nothing is printed
C. AdultMinor
D. Adult

24 What is printed by this code?

PYTHON
marks = 75
if marks >= 90:
    print("A")
elif marks >= 70:
    print("B")
elif marks >= 50:
    print("C")
else:
    print("F")

nested if and multi-way if-elif-else statement Medium
A. C
B. A
C. B
D. F

25 What is the output of the following loop?

PYTHON
total = 0
for i in range(1, 5):
    total += i
print(total)

for loop Medium
A. 6
B. 10
C. 15
D. 5

26 How many times does the while loop body execute?

PYTHON
x = 16
count = 0
while x > 1:
    x = x // 2
    count += 1
print(count)

while loop Medium
A. 4
B. 3
C. 5
D. 16

27 What is the output of the following nested loops?

PYTHON
count = 0
for i in range(3):
    for j in range(2):
        count += 1
print(count)

nested loops Medium
A. 5
B. 2
C. 3
D. 6

28 What does this code print?

PYTHON
for i in range(1, 6):
    if i == 3:
        continue
    if i == 5:
        break
    print(i, end=" ")

break and continue Medium
A. 1 2 4
B. 1 2 4 5
C. 1 2 3 4 5
D. 1 2 3 4

29 Which function call returns a random integer that could be any value in the set ?

random numbers Medium
A. random.range(1, 6)
B. random.random(1, 6)
C. random.randrange(1, 6)
D. random.randint(1, 6)

30 What is printed by the following code?

PYTHON
def square(n):
    return n * n

print(square(square(2)))

function calls Medium
A. 2
B. 4
C. 16
D. 8

31 What is the result of the following expression?

PYTHON
int("12") + int(3.9)

type conversion and coercion Medium
A. 15.9
B. "123"
C. 15
D. 16

32 What is the output of the following code?

PYTHON
print(3 + True + 2.0)

type conversion and coercion Medium
A. 6
B. TypeError
C. 6.0
D. 5

33 What does the following code print?

PYTHON
import math
print(math.ceil(4.1) + math.floor(4.9))

math functions Medium
A. 9
B. 10
C. 8
D. 4

34 What is the value of the following expression?

PYTHON
import math
math.sqrt(math.pow(3, 2) + math.pow(4, 2))

math functions Medium
A. 12.0
B. 25.0
C. 5.0
D. 7.0

35 What does the following program print?

PYTHON
def greet(name):
    print("Hello", name)

x = greet("Sam")
print(x)

adding new function Medium
A. Hello Sam then None
B. Hello Sam then Sam
C. Hello Sam only
D. None only

36 What is the output of this code using default and keyword arguments?

PYTHON
def power(base, exp=2):
    return base ** exp

print(power(3), power(2, exp=3))

parameters and argument Medium
A. 9 6
B. 9 8
C. 8 9
D. 6 8

37 What does the following code print?

PYTHON
def info(a, b, c):
    return a - b - c

print(info(c=1, a=10, b=3))

parameters and argument Medium
A. 8
B. 4
C. 12
D. 6

38 What value does fact(4) return?

PYTHON
def fact(n):
    if n <= 1:
        return 1
    return n * fact(n - 1)

recursion and its use Medium
A. 10
B. 24
C. 12
D. 4

39 What is printed by the following recursive function call mystery(3)?

PYTHON
def mystery(n):
    if n == 0:
        return
    print(n, end=" ")
    mystery(n - 1)

recursion and its use Medium
A. 0 1 2 3
B. 3 2 1 0
C. 1 2 3
D. 3 2 1

40 What is the output of the following code that uses range with a step?

PYTHON
result = []
for i in range(10, 0, -3):
    result.append(i)
print(result)

for loop Medium
A. [10, 7, 4, 1]
B. [9, 6, 3]
C. [10, 7, 4]
D. [10, 7, 4, 1, -2]

41 What is the output of the following code?

PYTHON
x = 0
y = 5
result = "A" if x else "B" if y else "C"
print(result)

conditional expressions Hard
A. C
B. Raises SyntaxError
C. B
D. A

42 Consider:

PYTHON
values = [0, '', None, [], 'False', 0.0]
count = 0
for v in values:
    if v:
        count += 1
print(count)



What is printed?

if statement Hard
A. 1
B. 0
C. 2
D. 6

43 What does this function return for f(-4)?

PYTHON
def f(n):
    if n % 2 == 0:
        return n // 2
    else:
        return 3 * n + 1

two way if - else Hard
A. -2
B. -1
C. -11
D. 2

44 What is the output?

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

nested if and multi-way if-elif-else statement Hard
A. A
B. C
C. B
D. B+

45 What is the final value of total?

PYTHON
total = 0
for i in range(2, 20, 3):
    total += i
print(total)

for loop Hard
A. 57
B. 51
C. 45
D. 63

46 How many times does the loop body execute?

PYTHON
n = 100
count = 0
while n > 1:
    n = n // 2
    count += 1
print(count)

while loop Hard
A. 5
B. 6
C. 8
D. 7

47 What is printed?

PYTHON
count = 0
for i in range(4):
    for j in range(i):
        count += 1
print(count)

nested loops Hard
A. 12
B. 16
C. 6
D. 10

48 What is the output?

PYTHON
result = []
for i in range(1, 10):
    if i % 2 == 0:
        continue
    if i > 6:
        break
    result.append(i)
print(result)

break and continue Hard
A. [1, 3, 5, 7, 9]
B. [1, 3, 5]
C. [1, 3, 5, 7]
D. [1, 2, 3, 4, 5]

49 What does this print?

PYTHON
for i in range(3):
    for j in range(3):
        if j == 1:
            break
        print(i, j)

break and continue Hard
A. Prints all nine pairs
B. Prints 0 0, 1 0, 2 0
C. Prints 0 0 only
D. Prints 0 0, 0 1, 0 2

50 Given import random; random.seed(0), which statement is true about random.randint(1, 6)?

random numbers Hard
A. It returns floats between 1 and 6
B. It can return any integer from 1 to 6 inclusive
C. It returns values from 1 to 5 only
D. It excludes the upper bound 6

51 What is the possible range of values produced by random.randrange(2, 20, 4)?

random numbers Hard
A. {2, 6, 10, 14, 18, 20}
B. {4, 8, 12, 16}
C. Any integer from 2 to 20
D. {2, 6, 10, 14, 18}

52 What is the output?

PYTHON
def g(x):
    return x * 2

def h(x):
    return x + 3

print(g(h(g(2))))

function calls Hard
A. 11
B. 14
C. 20
D. 10

53 What is the result of the following?

PYTHON
print(int("10", 2) + int("10", 8) + int("10", 16))

type conversion and coercion Hard
A. 42
B. 30
C. 26
D. 10

54 What is the output?

PYTHON
print(int(True) + float("3.5") + int("7"))

type conversion and coercion Hard
A. 11.5
B. 10
C. 11
D. 10.5

55 What does the following evaluate to?

PYTHON
import math
print(math.floor(math.sqrt(50)) + math.ceil(math.log2(10)))

math functions Hard
A. 13
B. 12
C. 10
D. 11

56 What is the output?

PYTHON
import math
print(math.gcd(48, 36), math.factorial(4) % 7)

math functions Hard
A. 12 3
B. 6 4
C. 12 4
D. 6 3

57 What is printed?

PYTHON
def make_counter():
    count = 0
    def inc():
        nonlocal count
        count += 1
        return count
    return inc

c = make_counter()
print(c(), c(), c())

adding new function Hard
A. 3 3 3
B. 1 2 3
C. 1 1 1
D. 0 1 2

58 What is the output?

PYTHON
def f(a, b=[]):
    b.append(a)
    return b

print(f(1))
print(f(2))
print(f(3, []))

parameters and argument Hard
A. [1, 2, 3] repeated three times
B. [1] then [1, 2] then [1, 2, 3]
C. [1] then [1, 2] then [3]
D. [1] then [2] then [3]

59 What does mystery(4) return?

PYTHON
def mystery(n):
    if n <= 1:
        return 1
    return n * mystery(n - 1)

recursion and its use Hard
A. 24
B. 4
C. 10
D. 12

60 How many times is fib called (including the initial call) when computing fib(4)?

PYTHON
def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)

recursion and its use Hard
A. 9
B. 11
C. 7
D. 5