Unit 2: Control Flow, Functions, and Problem-Solving - Practice Quiz

CSR101 — Python Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What does the else block execute when the if condition is false?

Conditional statements (if-else) Easy
A. The function block
B. The else block
C. The loop block
D. The if block

2 Which loop is commonly used to repeat through each item in a sequence?

Loops (for, while) Easy
A. An if loop
B. A try loop
C. A for loop
D. A def loop

3 What is a nested loop?

Nested loops Easy
A. A loop with no condition
B. A loop inside another loop
C. A loop after a function
D. A loop that runs once

4 What does the break statement do inside a loop?

Break and continue Easy
A. Ends the loop
B. Skips one iteration
C. Defines a new loop
D. Restarts the loop

5 What value does this code print? count = 0; count += 1; print(count)

Counting Easy
A. -1
B. 1
C. 0
D. 2

6 What numbers are produced by range(3)?

range() function Easy
A. 3, 2, 1
B. 0, 1, 2, 3
C. 1, 2, 3
D. 0, 1, 2

7 Which keyword is used to define a function in Python?

Function definitions Easy
A. def
B. define
C. function
D. func

8 In greet("Sam"), what is "Sam" called?

Arguments Easy
A. An argument
B. A condition
C. A loop counter
D. A return value

9 Which keyword sends a value back from a function?

Return values Easy
A. send
B. return
C. give
D. output

10 What is recursion in programming?

Recursion basics Easy
A. A condition using two values
B. A function calling itself
C. A loop changing variables
D. A string joining words

11 Which keyword creates a small anonymous function in Python?

Lambda function Easy
A. anonymous
B. lambda
C. small
D. quick

12 What is the result of "Python"[0:2]?

String slicing Easy
A. Py
B. Pyt
C. ho
D. Python

13 Which syntax is an f-string in Python?

Advanced string formatting Easy
A. "Hello {name}"
B. format "Hello {name}"
C. f"Hello {name}"
D. string("Hello {name}")

14 What does the string method .upper() do?

String methods Easy
A. Counts the characters
B. Removes spaces
C. Reverses the string
D. Makes letters uppercase

15 What does "a,b,c".split(",") return?

Splitting and joining strings Easy
A. The list ["a", "b", "c"]
B. The string "abc"
C. The number 3
D. The tuple ("a", "b", "c")

16 What is the result of "Hello, {}".format("Mia")?

String format method Easy
A. Mia, Hello
B. Hello, Mia
C. Hello, format
D. Hello, {Mia}

17 What is a regular expression mainly used for?

Regular expressions Easy
A. Drawing program windows
B. Matching text patterns
C. Creating numerical lists
D. Repeating Python loops

18 What is an algorithm?

Algorithmic thinking Easy
A. A Python comment
B. A step-by-step procedure
C. A type of string
D. A named variable

19 Which pattern is commonly used to find the total of numbers in a list?

Writing Python code for common problem-solving patterns Easy
A. Use a regular expression
B. Use a nested function definition
C. Use an accumulator
D. Use a string slice

20 What does the continue statement do inside a loop?

Break and continue Easy
A. Stops the program
B. Exits the current function
C. Repeats the previous iteration forever
D. Skips to the next iteration

21 What is printed by this code?

PYTHON
score = 72
if score >= 90:
    grade = "A"
elif score >= 70:
    grade = "B"
else:
    grade = "C"
print(grade)

Conditional statements (if-else) Medium
A. B
B. C
C. A
D. Nothing

22 What is the final value of total?

PYTHON
total = 0
number = 1
while number <= 4:
    total += number
    number += 1

Loops (for, while) Medium
A. 10
B. 8
C. 6
D. 12

23 How many times is count += 1 executed?

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

Nested loops Medium
A. 5
B. 6
C. 3
D. 9

24 What is printed by this code?

PYTHON
values = [1, 2, 3, 4, 5]
for value in values:
    if value == 3:
        continue
    if value == 5:
        break
    print(value, end=" ")

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

25 What is the value of count after execution?

PYTHON
text = "banana"
count = 0
for character in text:
    if character == "a":
        count += 1

Counting Medium
A. 1
B. 4
C. 3
D. 2

26 What list is produced by list(range(2, 10, 3))?

range() function Medium
A. [2, 5, 8, 10]
B. [2, 5, 8]
C. [2, 4, 6, 8]
D. [3, 6, 9]

27 What does the following function return when called as square(5)?

PYTHON
def square(number):
    return number * number

Function definitions Medium
A. 25
B. 55
C. 20
D. 10

28 What is printed by this code?

PYTHON
def describe(name, age=18):
    print(name, age)

describe("Mina", 21)
describe("Leo")

Arguments Medium
A. Mina None and Leo 18
B. Mina 18 and Leo 21
C. Mina 21 and Leo None
D. Mina 21 and Leo 18

29 What is printed by this code?

PYTHON
def add_tax(price):
    price *= 1.1

result = add_tax(100)
print(result)

Return values Medium
A. 100
B. 110
C. An error message
D. None

30 What is returned by sum_down(4)?

PYTHON
def sum_down(n):
    if n == 0:
        return 0
    return n + sum_down(n - 1)

Recursion basics Medium
A. 24
B. 4
C. 6
D. 10

31 What is the result of this expression?

PYTHON
numbers = [1, 2, 3, 4]
result = list(map(lambda x: x * 2 + 1, numbers))

Lambda function Medium
A. [2, 3, 4, 5]
B. [2, 4, 6, 8]
C. [1, 3, 5, 7]
D. [3, 5, 7, 9]

32 What is the value of result?

PYTHON
word = "PYTHON"
result = word[1:5:2]

String slicing Medium
A. "YH"
B. "PTO"
C. "YT"
D. "YTH"

33 What is printed by this code?

PYTHON
name = "Ava"
score = 87.456
print(f"{name}: {score:.1f}")

Advanced string formatting Medium
A. Ava: 87
B. Ava: 87.5
C. Ava: 87.4
D. Ava: 87.46

34 What is the value of cleaned?

PYTHON
text = "  Python Programming  "
cleaned = text.strip().lower().replace(" ", "-")

String methods Medium
A. "Python-Programming"
B. "python-programming"
C. "python programming"
D. "-python-programming-"

35 What is the result of this code?

PYTHON
text = "red,green,blue"
colors = text.split(",")
result = " | ".join(colors)

Splitting and joining strings Medium
A. "red green blue"
B. "red | green | blue"
C. "red|green|blue|"
D. "red,green,blue"

36 What is printed by this code?

PYTHON
item = "book"
price = 12.5
print("{} costs ${:.2f}".format(item, price))

String format method Medium
A. {} costs ${:.2f}
B. book costs $12.5
C. book costs $12.50
D. book costs $12

37 Which pattern matches a string containing exactly three digits?

PYTHON
import re

Regular expressions Medium
A. r"^\\d{3}$"
B. r"[0-9]"
C. r"\\w{3}"
D. r"\\d+"

38 Which strategy correctly finds the largest value in a nonempty list without using max()?

Algorithmic thinking Medium
A. Sort the list and always select its first value
B. Start with 0 and replace it when a value is smaller
C. Start with the first value and replace it when a value is larger
D. Start with the last value and replace it when a value is equal

39 Which code correctly counts the number of even values in numbers?

Writing Python code for common problem-solving patterns Medium
A.
PYTHON
count = 1
for n in numbers:
    if n / 2 == 0:
        count += 1
B.
PYTHON
count = 0
for n in numbers:
    if n % 2 == 1:
        count += 1
C.
PYTHON
count = 0
for n in numbers:
    if n % 2 != 0:
        count -= 1
D.
PYTHON
count = 0
for n in numbers:
    if n % 2 == 0:
        count += 1

40 Which code correctly creates a list containing the squares of the positive values in numbers?

Writing Python code for common problem-solving patterns Medium
A.
PYTHON
squares = [n * n for n in numbers if n > 0]
B.
PYTHON
squares = [n ** 2 for n in numbers if n == 0]
C.
PYTHON
squares = [n * n for n in numbers if n < 0]
D.
PYTHON
squares = [n + n for n in numbers if n > 0]

41 What is printed by this code? x, y = 8, 3\nif x % y == 0:\n result = "A"\nelif x // y == 2 and x > y:\n result = "B"\nelse:\n result = "C"\nprint(result)

Conditional statements (if-else) Hard
A. C
B. A
C. B
D. It raises a ZeroDivisionError

42 What list is produced by list(range(10, -2, -3))?

range() function Hard
A. [10, 7, 4, 1]
B. [7, 4, 1, -2]
C. [10, 7, 4, 1, -2]
D. [10, 8, 6, 4, 2, 0]

43 What is printed by this code? total = 0\nfor i in range(1, 4):\n for j in range(i, 4):\n total += i * j\nprint(total)

Nested loops Hard
A. 25
B. 30
C. 36
D. 35

44 What is printed? values = []\nfor n in range(2, 10):\n if n % 2 == 0:\n continue\n if n > 6:\n break\n values.append(n)\nprint(values)

Break and continue Hard
A. [2, 3, 4, 5, 6]
B. [3, 5, 7]
C. [3, 5]
D. [2, 4, 6]

45 Which expression correctly counts the number of overlapping occurrences of "ana" in "banana"?

Counting Hard
A. sum("ana" in "banana"[i:] for i in range(len("banana")))
B. len("banana".split("ana")) - 1
C. sum("banana"[i:i+3] == "ana" for i in range(len("banana") - 2))
D. "banana".count("ana")

46 What is printed by this code? def update(items=[]):\n items.append(len(items))\n return items\n\nprint(update())\nprint(update())

Function definitions Hard
A. [0] followed by [0]
B. [1] followed by [1]
C. A TypeError occurs because the default list is immutable
D. [0] followed by [1]

47 What happens when this code runs? def f(a, b=2, *args, **kwargs):\n return a + b + len(args) + len(kwargs)\n\nprint(f(5, 3, 7, 8, x=1))

Arguments Hard
A. It prints 10
B. It prints 12
C. It raises a TypeError because positional arguments follow a default argument
D. It prints 13

48 What is printed by this code? def transform(x):\n if x < 0:\n return\n return x * 2\n\nprint(transform(-1), transform(4))

Return values Hard
A. None None
B. None 8
C. 0 8
D. It raises a ValueError for the negative argument

49 What is printed by this function call? def f(n):\n if n <= 1:\n return 1\n return n * f(n - 2)\n\nprint(f(6))

Recursion basics Hard
A. 6
B. 48
C. 8
D. 24

50 What is the result of sorted([(1, 4), (2, 1), (1, 2)], key=lambda p: (p[0], -p[1]))?

Lambda function Hard
A. [(1, 4), (2, 1), (1, 2)]
B. [(1, 2), (1, 4), (2, 1)]
C. [(1, 4), (1, 2), (2, 1)]
D. [(2, 1), (1, 4), (1, 2)]

51 What is printed by s = "algorithm"; print(s[-2:1:-2])?

String slicing Hard
A. mrt
B. hio
C. mto
D. mgt

52 What is printed by x = 12.3456; print(f"{x:08.2f}")?

Advanced string formatting Hard
A. 12.345600
B. 12.35
C. 0012.346
D. 00012.35

53 What is the result of " A\tB\n".strip().replace("\t", "-").lower()?

String methods Hard
A. "a-b"
B. "a\\tb"
C. "A-B"
D. " a-b "

54 What is printed by text = "a,,b,"; print("|".join(text.split(",")))?

Splitting and joining strings Hard
A. a||b|
B. a|b
C. a,,b,
D. a| |b|

55 What is printed by template = "{1}-{0}:{1}"; print(template.format("x", "y"))?

String format method Hard
A. A KeyError is raised because numeric fields are invalid
B. x-y:y
C. x-y:x
D. y-x:y

56 Using Python's re.findall, what does re.findall(r"\b\w+\b", "hi, 42 times!") return?

Regular expressions Hard
A. ['hi', '42', 'times']
B. ['hi', 'times']
C. ['h', 'i', '4', '2', 't', 'i', 'm', 'e', 's']
D. ['hi,', '42', 'times!']

57 A sorted list may contain duplicates. Which strategy finds whether two distinct elements sum to a target in time and extra space?

Algorithmic thinking Hard
A. Sort the list first, then scan only adjacent values
B. Use two pointers at the beginning and end
C. Try every pair with two nested loops
D. Binary-search every possible complement

58 Which code correctly returns the first character in s whose frequency is exactly one, or None if no such character exists?

Writing Python code for common problem-solving patterns Hard
A. return min((c for c in s if s.count(c) == 1), default=None)
B. return next((c for c in s if s.count(c) == 1), None)
C. return next((c for c in set(s) if s.count(c) == 1), None)
D. return next((c for c in s if s.count(c) > 1), None)

59 What is printed by this code? n = 19\nsteps = 0\nwhile n > 1:\n n = n // 2\n steps += 1\nprint(n, steps)

Loops (for, while) Hard
A. 0 4
B. 1 5
C. 2 4
D. 1 4

60 What does pal("racecar", 0, 6) return? def pal(s, left, right):\n if left >= right:\n return True\n return s[left] == s[right] and pal(s, left + 1, right - 1)

Recursion basics Hard
A. False
B. None
C. It raises an IndexError at the recursive base case
D. True