Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 2 MCQsConditionals and Iterations; Functions and Recursion
Which keyword is used to begin a conditional statement in Python?
Not quite. Try another option.
Answer: if
In Python, the if keyword is used to start a conditional statement that executes a block of code only when a condition is true.
Question 2 of 60 · two way if - else · Easy
In a two-way if-else statement, when does the else block execute?
Not quite. Try another option.
Answer: When the if condition is false
The else block runs only when the if condition evaluates to False, providing an alternative path of execution.
Question 3 of 60 · nested if and multi-way if-elif-else statement · Easy
Which keyword is used for a multi-way branch to test additional conditions in Python?
Not quite. Try another option.
Answer: elif
Python uses elif (short for else-if) to test additional conditions after an initial if, allowing multi-way branching.
Question 4 of 60 · for loop · Easy
What does the range(5) function generate when used in a for loop?
Not quite. Try another option.
Answer: Numbers 0, 1, 2, 3, 4
range(5) produces integers starting from up to but not including , i.e., .
Question 5 of 60 · while loop · Easy
A while loop in Python continues to execute as long as its condition is:
Not quite. Try another option.
Answer: True
A while loop repeatedly runs its body while the condition remains True and stops once it becomes False.
Question 6 of 60 · break and continue · Easy
What does the break statement do inside a loop?
Not quite. Try another option.
Answer: Exits the loop immediately
The break statement terminates the loop entirely and transfers control to the statement following the loop.
Question 7 of 60 · break and continue · Easy
What does the continue statement do inside a loop?
Not quite. Try another option.
Answer: Skips the rest of the current iteration and moves to the next
The continue statement skips the remaining code in the current iteration and jumps to the next iteration of the loop.
Question 8 of 60 · nested loops · Easy
What is a nested loop?
Not quite. Try another option.
Answer: A loop placed inside another loop
A nested loop is a loop written inside the body of another loop; the inner loop runs completely for each iteration of the outer loop.
Question 9 of 60 · random numbers · Easy
Which module in Python is commonly used to generate random numbers?
Not quite. Try another option.
Answer: random
The built-in random module provides functions like random() and randint() for generating random numbers.
Question 10 of 60 · random numbers · Easy
Which function returns a random integer between two given values (inclusive)?
Not quite. Try another option.
Answer: random.randint(a, b)
random.randint(a, b) returns a random integer such that , including both endpoints.
Question 11 of 60 · function calls · Easy
How do you call a function named greet in Python?
Not quite. Try another option.
Answer: greet()
A function is called by writing its name followed by parentheses, e.g., greet(), optionally containing arguments.
Question 12 of 60 · type conversion and coercion · Easy
Which function converts a string "25" into an integer?
Not quite. Try another option.
Answer: int("25")
The int() function converts a valid numeric string like "25" into the integer .
Question 13 of 60 · type conversion and coercion · Easy
What is the result of float(7) in Python?
Not quite. Try another option.
Answer: 7.0
The float() function converts the integer into a floating-point number, giving 7.0.
Question 14 of 60 · math functions · Easy
Which function from the math module returns the square root of a number?
Not quite. Try another option.
Answer: math.sqrt()
math.sqrt(x) returns the square root of , for example math.sqrt(9) gives 3.0.
Question 15 of 60 · math functions · Easy
What does math.ceil(4.2) return?
Not quite. Try another option.
Answer: 5
math.ceil() rounds a number up to the nearest integer, so math.ceil(4.2) returns .
Question 16 of 60 · adding new function · Easy
Which keyword is used to define a new function in Python?
Not quite. Try another option.
Answer: def
The def keyword is used to define a new function in Python, followed by the function name and parentheses.
Question 17 of 60 · parameters and argument · Easy
In the function definition def add(x, y):, what are x and y called?
Not quite. Try another option.
Answer: Parameters
The names x and y in a function definition are called parameters; the actual values passed during a call are arguments.
Question 18 of 60 · recursion and its use · Easy
What is recursion in programming?
Not quite. Try another option.
Answer: A function that calls itself
Recursion is a technique where a function calls itself to solve smaller instances of the same problem.
Question 19 of 60 · recursion and its use · Easy
What is required in every recursive function to prevent infinite recursion?
Not quite. Try another option.
Answer: A base case
A base case is a stopping condition that ends the recursion, preventing it from calling itself forever.
Question 20 of 60 · conditional expressions · Easy
What is the value of the conditional expression "Yes" if 5 > 3 else "No"?
Not quite. Try another option.
Answer: "Yes"
Since the condition is True, the conditional expression evaluates to the value before if, which is "Yes".
Question 21 of 60 · conditional expressions · Medium
What is the value assigned to result after executing the following code?
Python
x = 7
result = "even" if x % 2 == 0 else "odd"
Not quite. Try another option.
Answer: "odd"
The conditional expression evaluates x % 2 == 0, which is 7 % 2 == 0 → 1 == 0 → False. Since the condition is False, the value after else is chosen, so result becomes "odd".
Question 22 of 60 · if statement · Medium
How many times will "Hi" be printed?
Python
n = 10
if n > 5:
if n < 20:
print("Hi")
Not quite. Try another option.
Answer: 1
Both conditions are checked: 10 > 5 is True and 10 < 20 is True. Since both nested if conditions succeed, "Hi" is printed exactly once.
Question 23 of 60 · two way if - else · Medium
What does the following code output?
Python
age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
Not quite. Try another option.
Answer: Adult
The condition age >= 18 is 18 >= 18, which is True. Therefore the if block runs and prints Adult; the else block is skipped.
Question 24 of 60 · nested if and multi-way if-elif-else statement · Medium
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")
Not quite. Try another option.
Answer: B
Conditions are checked top to bottom. 75 >= 90 is False, then 75 >= 70 is True, so B is printed and the remaining branches are skipped.
Question 25 of 60 · for loop · Medium
What is the output of the following loop?
Python
total = 0
for i in range(1, 5):
total += i
print(total)
Not quite. Try another option.
Answer: 10
range(1, 5) yields 1, 2, 3, 4. Summing these gives , so total prints 10.
Question 26 of 60 · while loop · Medium
How many times does the while loop body execute?
Python
x = 16
count = 0
while x > 1:
x = x // 2
count += 1
print(count)
Not quite. Try another option.
Answer: 4
Starting from 16, integer division by 2 gives the sequence 16→8→4→2→1. The loop runs while x > 1, executing 4 times before x becomes 1.
Question 27 of 60 · nested loops · Medium
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)
Not quite. Try another option.
Answer: 6
The outer loop runs 3 times and the inner loop runs 2 times per outer iteration. Total iterations are , so count prints 6.
Question 28 of 60 · break and continue · Medium
What does this code print?
Python
for i in range(1, 6):
if i == 3:
continue
if i == 5:
break
print(i, end=" ")
Not quite. Try another option.
Answer: 1 2 4
When i is 3, continue skips printing. When i is 5, break stops the loop. So values 1, 2, and 4 are printed.
Question 29 of 60 · random numbers · Medium
Which function call returns a random integer that could be any value in the set ?
Not quite. Try another option.
Answer: random.randint(1, 6)
random.randint(a, b) returns an integer N such that a <= N <= b, making both endpoints inclusive. randrange(1, 6) excludes 6, and the other two are not valid calls for this purpose.
Question 30 of 60 · function calls · Medium
What is printed by the following code?
Python
def square(n):
return n * n
print(square(square(2)))
Not quite. Try another option.
Answer: 16
The inner call square(2) returns 4. Then square(4) returns , which is printed.
Question 31 of 60 · type conversion and coercion · Medium
What is the result of the following expression?
Python
int("12") + int(3.9)
Not quite. Try another option.
Answer: 15
int("12") converts the string to 12, and int(3.9) truncates the float to 3 (no rounding). The sum is .
Question 32 of 60 · type conversion and coercion · Medium
What is the output of the following code?
Python
print(3 + True + 2.0)
Not quite. Try another option.
Answer: 6.0
In arithmetic, True is coerced to 1. So the expression is . Because a float is involved, the result is a float: 6.0.
Question 33 of 60 · math functions · Medium
What does the following code print?
Python
import math
print(math.ceil(4.1) + math.floor(4.9))
Not quite. Try another option.
Answer: 9
math.ceil(4.1) rounds up to 5, and math.floor(4.9) rounds down to 4. The sum is .
Question 34 of 60 · math functions · Medium
What is the value of the following expression?
Python
import math
math.sqrt(math.pow(3, 2) + math.pow(4, 2))
Not quite. Try another option.
Answer: 5.0
math.pow(3, 2) is 9.0 and math.pow(4, 2) is 16.0. Their sum is 25.0, and math.sqrt(25.0) returns 5.0.
Question 35 of 60 · adding new function · Medium
What does the following program print?
Python
def greet(name):
print("Hello", name)
x = greet("Sam")
print(x)
Not quite. Try another option.
Answer: Hello Sam then None
The function prints Hello Sam but has no return, so it returns None. Assigning to x stores None, and print(x) outputs None.
Question 36 of 60 · parameters and argument · Medium
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))
Not quite. Try another option.
Answer: 9 8
power(3) uses the default exp=2, giving . power(2, exp=3) gives . Output is 9 8.
Question 37 of 60 · parameters and argument · Medium
What does the following code print?
Python
def info(a, b, c):
return a - b - c
print(info(c=1, a=10, b=3))
Not quite. Try another option.
Answer: 6
With keyword arguments, order does not matter: a=10, b=3, c=1. The result is .
Question 38 of 60 · recursion and its use · Medium
What value does fact(4) return?
Python
def fact(n):
if n <= 1:
return 1
return n * fact(n - 1)
Not quite. Try another option.
Answer: 24
This computes the factorial: . The base case returns 1 when n <= 1.
Question 39 of 60 · recursion and its use · Medium
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)
Not quite. Try another option.
Answer: 3 2 1
Each call prints n before recursing with n - 1. Starting at 3, it prints 3, 2, 1, then stops at the base case n == 0.
Question 40 of 60 · for loop · Medium
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)
Not quite. Try another option.
Answer: [10, 7, 4, 1]
range(10, 0, -3) starts at 10 and decreases by 3, stopping before reaching 0. This yields 10, 7, 4, 1.
Question 41 of 60 · conditional expressions · Hard
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)
Not quite. Try another option.
Answer: B
The conditional expression is right-associative: "A" if x else ("B" if y else "C"). Since x is 0 (falsy), the else branch evaluates. y is 5 (truthy), so it returns "B".
Question 42 of 60 · if statement · Hard
Consider:
Python
values = [0, '', None, [], 'False', 0.0]
count = 0
for v in values:
if v:
count += 1
print(count)
What is printed?
Not quite. Try another option.
Answer: 1
Falsy values are 0, '', None, [], and 0.0. Only the string 'False' is non-empty and therefore truthy, so count increments exactly once.
Question 43 of 60 · two way if - else · Hard
What does this function return for f(-4)?
Python
def f(n):
if n % 2 == 0:
return n // 2
else:
return 3 * n + 1
Not quite. Try another option.
Answer: -2
In Python -4 % 2 equals 0, so the if branch runs. Floor division -4 // 2 equals -2.
Question 44 of 60 · nested if and multi-way if-elif-else statement · Hard
score is 85, which fails >= 90 but passes >= 80. Inside, 85 >= 85 is true, so grade becomes 'B+'.
Question 45 of 60 · for loop · Hard
What is the final value of total?
Python
total = 0
for i in range(2, 20, 3):
total += i
print(total)
Not quite. Try another option.
Answer: 57
range(2, 20, 3) yields 2, 5, 8, 11, 14, 17. Their sum is 2+5+8+11+14+17 = 57.
Question 46 of 60 · while loop · Hard
How many times does the loop body execute?
Python
n = 100
count = 0
while n > 1:
n = n // 2
count += 1
print(count)
Not quite. Try another option.
Answer: 7
n becomes 50, 25, 12, 6, 3, 1, 0 across iterations. The loop stops when n is not > 1; the body runs 7 times (100→50→25→12→6→3→1→0).
Question 47 of 60 · nested loops · Hard
What is printed?
Python
count = 0
for i in range(4):
for j in range(i):
count += 1
print(count)
Not quite. Try another option.
Answer: 6
The inner loop runs i times for each i in 0,1,2,3. Total iterations are 0+1+2+3 = 6.
Question 48 of 60 · break and continue · Hard
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)
Not quite. Try another option.
Answer: [1, 3, 5]
Even numbers are skipped by continue. Odd values 1, 3, 5 are appended. At i = 7, i > 6 triggers break before appending, so the result is [1, 3, 5].
Question 49 of 60 · break and continue · Hard
What does this print?
Python
for i in range(3):
for j in range(3):
if j == 1:
break
print(i, j)
Not quite. Try another option.
Answer: Prints 0 0, 1 0, 2 0
break exits only the inner loop. For each i, j = 0 prints, then j = 1 breaks. So 0 0, 1 0, and 2 0 are printed.
Question 50 of 60 · random numbers · Hard
Given import random; random.seed(0), which statement is true about random.randint(1, 6)?
Not quite. Try another option.
Answer: It can return any integer from 1 to 6 inclusive
random.randint(a, b) returns an integer N such that a <= N <= b, so both endpoints are inclusive. Unlike randrange, it does not exclude the upper bound.
Question 51 of 60 · random numbers · Hard
What is the possible range of values produced by random.randrange(2, 20, 4)?
Not quite. Try another option.
Answer: {2, 6, 10, 14, 18}
randrange(start, stop, step) picks from range(2, 20, 4), which is 2, 6, 10, 14, 18. The stop value 20 is excluded.
Question 52 of 60 · function calls · Hard
What is the output?
Python
def g(x):
return x * 2
def h(x):
return x + 3
print(g(h(g(2))))
Not quite. Try another option.
Answer: 14
Evaluate inside-out: g(2) = 4, then h(4) = 7, then g(7) = 14.
Question 53 of 60 · type conversion and coercion · Hard
make_counter returns a closure inc that uses nonlocal count. Each call mutates the enclosed count, so successive calls return 1, 2, 3.
Question 58 of 60 · parameters and argument · Hard
What is the output?
Python
def f(a, b=[]):
b.append(a)
return b
print(f(1))
print(f(2))
print(f(3, []))
Not quite. Try another option.
Answer: [1] then [1, 2] then [3]
The default [] is created once and shared across calls that omit b. So f(1) gives [1] and f(2) reuses it giving [1, 2]. f(3, []) passes a fresh list, giving [3].
Question 59 of 60 · recursion and its use · Hard
What does mystery(4) return?
Python
def mystery(n):
if n <= 1:
return 1
return n * mystery(n - 1)
Not quite. Try another option.
Answer: 24
This computes the factorial: 4 * 3 * 2 * 1 = 24. The base case returns 1 when n <= 1.
Question 60 of 60 · recursion and its use · Hard
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)
Not quite. Try another option.
Answer: 9
The naive Fibonacci call tree for fib(4) makes calls: fib(4), fib(3), fib(2), fib(1), fib(0), fib(1), fib(2), fib(1), fib(0) — 9 total calls, reflecting the exponential redundancy of naive recursion.
That's unit 2
Answer a few questions and your score shows up here.
Nearly through. Did unit 2 of ECE181 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 2 of ECE181 save you a night before the exam?
Then you can help keep it free for the next batch. 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.