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 if block
C. The else block
D. The loop block

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

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

3 What is a nested loop?

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

4 What does the break statement do inside a loop?

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

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

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

6 What numbers are produced by range(3)?

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

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

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

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

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

9 Which keyword sends a value back from a function?

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

10 What is recursion in programming?

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

11 Which keyword creates a small anonymous function in Python?

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

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

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

13 Which syntax is an f-string in Python?

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

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

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

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

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

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

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

17 What is a regular expression mainly used for?

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

18 What is an algorithm?

Algorithmic thinking Easy
A. A type of string
B. A step-by-step procedure
C. A Python comment
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 string slice
B. Use an accumulator
C. Use a nested function definition
D. Use a regular expression

20 What does the continue statement do inside a loop?

Break and continue Easy
A. Repeats the previous iteration forever
B. Exits the current function
C. Stops the program
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. Nothing
D. A

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. 12
B. 6
C. 10
D. 8

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. 6
B. 5
C. 9
D. 3

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 5
B. 1 2
C. 1 2 4
D. 1 2 3 4

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. 3
B. 4
C. 2
D. 1

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

range() function Medium
A. [2, 5, 8, 10]
B. [2, 4, 6, 8]
C. [2, 5, 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. 10
B. 25
C. 20
D. 55

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 21 and Leo None
B. Mina 18 and Leo 21
C. Mina None and Leo 18
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. None
C. An error message
D. 110

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. 6
C. 10
D. 4

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. [1, 3, 5, 7]
B. [2, 3, 4, 5]
C. [2, 4, 6, 8]
D. [3, 5, 7, 9]

32 What is the value of result?

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

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

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.5
B. Ava: 87.4
C. Ava: 87
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. book costs $12.50
B. book costs $12
C. book costs $12.5
D. {} costs ${:.2f}

37 Which pattern matches a string containing exactly three digits?

PYTHON
import re

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

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

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

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

Writing Python code for common problem-solving patterns Medium
A.
PYTHON
count = 0
for n in numbers:
    if n % 2 == 0:
        count += 1
B.
PYTHON
count = 1
for n in numbers:
    if n / 2 == 0:
        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 == 1:
        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 ** 2 for n in numbers if n == 0]
B.
PYTHON
squares = [n * n 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. A
B. B
C. C
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. [10, 8, 6, 4, 2, 0]
C. [10, 7, 4, 1, -2]
D. [7, 4, 1, -2]

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. 35
B. 30
C. 25
D. 36

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, 4, 6]
B. [3, 5]
C. [2, 3, 4, 5, 6]
D. [3, 5, 7]

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

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

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. A TypeError occurs because the default list is immutable
B. [0] followed by [1]
C. [0] followed by [0]
D. [1] 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 12
B. It prints 10
C. It prints 13
D. It raises a TypeError because positional arguments follow a default argument

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. It raises a ValueError for the negative argument
B. None 8
C. None None
D. 0 8

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. 24
C. 48
D. 8

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, 2), (1, 4), (2, 1)]
B. [(1, 4), (1, 2), (2, 1)]
C. [(1, 4), (2, 1), (1, 2)]
D. [(2, 1), (1, 4), (1, 2)]

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

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

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

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

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

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

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. x-y:x
B. A KeyError is raised because numeric fields are invalid
C. x-y:y
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. ['h', 'i', '4', '2', 't', 'i', 'm', 'e', 's']
B. ['hi', '42', 'times']
C. ['hi', 'times']
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. Binary-search every possible complement
B. Use two pointers at the beginning and end
C. Try every pair with two nested loops
D. Sort the list first, then scan only adjacent values

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 next((c for c in set(s) if s.count(c) == 1), None)
B. return min((c for c in s if s.count(c) == 1), default=None)
C. return next((c for c in 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. 1 5
B. 1 4
C. 2 4
D. 0 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. None
B. False
C. True
D. It raises an IndexError at the recursive base case