Python Practice Test: Functions

  • Defining and Calling Functions
  • 10 questions
  • 14 minutes

Ten questions on defining and calling functions. Tip: watch for what the function returns and which variable names are local.

Tap an option to mark your answer. You can change your mind, or tap the same option again to leave the question blank. The correct answers and explanations appear when you finish the test.Tap an option to answer; tap it again to leave the question blank.

Advertisement
Advertisement
  1. Question 1

    def add(a, b=2):
        return a + b

    print(add(3))

    What does the code print?

  2. Question 2

    def f():
        x = 1

    print(f())

    What does the code print?

  3. Question 3

    def total(*args):
        return sum(args)

    print(total(1, 2, 3))

    What does the code print?

  4. Question 4

    x = 10

    def f():
        x = 5

    f()
    print(x)

    What does the code print?

  5. Question 5

    def greet(name, greeting="Hi"):
        return greeting + ", " + name

    print(greet(greeting="Yo", name="Sam"))

    What does the code print?

  6. Question 6

    def add(item, bag=[]):
        bag.append(item)
        return bag

    add(1)
    print(add(2))

    What does the code print?

  7. Question 7

    print(sorted(["pear", "fig", "apple"], key=len))

    What does the code print?

  8. Question 8

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

    print(fact(5))

    What does the code print?

  9. Question 9

    def mm(values):
        return min(values), max(values)

    lo, hi = mm([3, 9, 1])
    print(lo, hi)

    What does the code print?

  10. Question 10

    count = 0

    def inc():
        global count
        count += 1

    inc()
    inc()
    print(count)

    What does the code print?

Questions you leave blank are not counted as wrong.

Advertisement

More Python tests

All Python Programming tests
14:00

Do you want to finish the test?