Python3 【高阶函数】水平考试:30道精选试题和答案

Python3 【高阶函数】水平考试:30道精选试题和答案

试卷说明

本试卷包含:选择题15 道、填空题10 道和 编程题5 道,总分 100 分。每道题后附有答案和解析。


高阶函数测试试卷

满分:100 分
时间:90 分钟


一、选择题(每题 2 分,共 30 分)

  1. 以下哪个是 Python 内置的高阶函数?

    A. len

    B. map

    C. type

    D. print
    答案:B

  2. map 函数的返回值是什么类型?

    A. 列表

    B. 元组

    C. 迭代器

    D. 字典
    答案:C

  3. 以下代码的输出是什么?

    python 复制代码
    numbers = [1, 2, 3, 4]
    result = list(map(lambda x: x * 2, numbers))
    print(result)

    A. [1, 2, 3, 4]

    B. [2, 4, 6, 8]

    C. [1, 4, 9, 16]

    D. [2, 3, 4, 5]
    答案:B

  4. 以下代码的输出是什么?

    python 复制代码
    numbers = [1, 2, 3, 4, 5]
    result = list(filter(lambda x: x % 2 == 0, numbers))
    print(result)

    A. [1, 3, 5]

    B. [2, 4]

    C. [1, 2, 3, 4, 5]

    D. []
    答案:B

  5. 以下代码的输出是什么?

    python 复制代码
    from functools import reduce
    numbers = [1, 2, 3, 4]
    result = reduce(lambda x, y: x + y, numbers)
    print(result)

    A. 10

    B. 24

    C. 1

    D. 4
    答案:A

  6. 以下代码的输出是什么?

    python 复制代码
    words = ["apple", "banana", "cherry"]
    result = sorted(words, key=lambda x: len(x))
    print(result)

    A. ['apple', 'banana', 'cherry']

    B. ['cherry', 'banana', 'apple']

    C. ['apple', 'cherry', 'banana']

    D. ['banana', 'apple', 'cherry']
    答案:C

  7. 以下代码的输出是什么?

    python 复制代码
    numbers = [1, 2, 3, 4]
    result = list(map(lambda x: x ** 2, filter(lambda x: x % 2 == 0, numbers)))
    print(result)

    A. [1, 4, 9, 16]

    B. [2, 4]

    C. [4, 16]

    D. [1, 3]
    答案:C

  8. 以下代码的输出是什么?

    python 复制代码
    from functools import partial
    def power(base, exponent):
        return base ** exponent
    square = partial(power, exponent=2)
    print(square(4))

    A. 16

    B. 8

    C. 4

    D. 2
    答案:A

  9. 以下代码的输出是什么?

    python 复制代码
    def create_multiplier(factor):
        def multiplier(x):
            return x * factor
        return multiplier
    double = create_multiplier(2)
    print(double(5))

    A. 5

    B. 10

    C. 15

    D. 20
    答案:B

  10. 以下代码的输出是什么?

    python 复制代码
    numbers = [1, 2, 3, 4]
    result = map(lambda x: print(x), numbers)
    list(result)

    A. [1, 2, 3, 4]

    B. 1 2 3 4

    C. None

    D. 无输出
    答案:B

  11. 以下代码的输出是什么?

    python 复制代码
    from functools import reduce
    numbers = []
    result = reduce(lambda x, y: x + y, numbers, 10)
    print(result)

    A. 0

    B. 10

    C. 报错

    D. None
    答案:B

  12. 以下代码的输出是什么?

    python 复制代码
    data = [{"value": 3}, {"value": 1}, {"value": 2}]
    result = sorted(data, key=lambda x: x["value"])
    print(result)

    A. [{'value': 1}, {'value': 2}, {'value': 3}]

    B. [{'value': 3}, {'value': 1}, {'value': 2}]

    C. [{'value': 1}, {'value': 3}, {'value': 2}]

    D. [{'value': 2}, {'value': 1}, {'value': 3}]
    答案:A

  13. 以下代码的输出是什么?

    python 复制代码
    numbers = [1, 2, 3, 4]
    result = list(map(lambda x: x ** 2 if x % 2 == 0 else x ** 3, numbers))
    print(result)

    A. [1, 4, 27, 16]

    B. [1, 4, 9, 16]

    C. [1, 8, 27, 64]

    D. [1, 2, 3, 4]
    答案:A

  14. 以下代码的输出是什么?

    python 复制代码
    functions = [lambda x: x + i for i in range(3)]
    result = [f(1) for f in functions]
    print(result)

    A. [1, 2, 3]

    B. [2, 3, 4]

    C. [3, 3, 3]

    D. [1, 1, 1]
    答案:C

  15. 以下代码的输出是什么?

    python 复制代码
    from functools import lru_cache
    @lru_cache(maxsize=None)
    def fibonacci(n):
        if n < 2:
            return n
        return fibonacci(n-1) + fibonacci(n-2)
    print(fibonacci(10))

    A. 10

    B. 55

    C. 89

    D. 144
    答案:B


二、填空题(每题 3 分,共 30 分)

  1. map 函数的作用是对可迭代对象中的每个元素应用一个函数,并返回一个________。
    答案:迭代器

  2. filter 函数的作用是根据函数的返回值(TrueFalse)过滤可迭代对象中的元素,并返回一个________。
    答案:迭代器

  3. reduce 函数的作用是对可迭代对象中的元素进行________计算。
    答案:累积

  4. sorted 函数的 key 参数用于指定一个函数来提取________。
    答案:比较键

  5. 以下代码的输出是________。

    python 复制代码
    numbers = [1, 2, 3, 4]
    result = list(map(lambda x: x + 1, numbers))
    print(result)

    答案:[2, 3, 4, 5]

  6. 以下代码的输出是________。

    python 复制代码
    numbers = [1, 2, 3, 4, 5]
    result = list(filter(lambda x: x > 3, numbers))
    print(result)

    答案:[4, 5]

  7. 以下代码的输出是________。

    python 复制代码
    from functools import reduce
    numbers = [1, 2, 3, 4]
    result = reduce(lambda x, y: x * y, numbers, 1)
    print(result)

    答案:24

  8. 以下代码的输出是________。

    python 复制代码
    words = ["apple", "banana", "cherry"]
    result = sorted(words, key=lambda x: len(x), reverse=True)
    print(result)

    答案:['banana', 'cherry', 'apple']

  9. 以下代码的输出是________。

    python 复制代码
    numbers = [1, 2, 3, 4]
    result = list(map(lambda x: x ** 2, filter(lambda x: x % 2 == 0, numbers)))
    print(result)

    答案:[4, 16]

  10. 以下代码的输出是________。

    python 复制代码
    from functools import partial
    def power(base, exponent):
        return base ** exponent
    cube = partial(power, exponent=3)
    print(cube(2))

    答案:8


三、编程题(每题 8 分,共 40 分)

  1. 编写一个函数 square_even_numbers,接受一个整数列表,返回所有偶数的平方组成的列表。
    答案:

    python 复制代码
    def square_even_numbers(numbers):
        return list(map(lambda x: x ** 2, filter(lambda x: x % 2 == 0, numbers)))
  2. 编写一个函数 sum_of_squares,使用 reduce 计算列表中所有元素的平方和。
    答案:

    python 复制代码
    from functools import reduce
    def sum_of_squares(numbers):
        return reduce(lambda x, y: x + y ** 2, numbers, 0)
  3. 编写一个函数 sort_by_length,接受一个字符串列表,按字符串长度排序。
    答案:

    python 复制代码
    def sort_by_length(words):
        return sorted(words, key=lambda x: len(x))
  4. 编写一个函数 create_multiplier,接受一个因子 factor,返回一个函数,该函数可以将输入的数乘以 factor
    答案:

    python 复制代码
    def create_multiplier(factor):
        return lambda x: x * factor
  5. 编写一个函数 fibonacci,使用 lru_cache 装饰器优化斐波那契数列的计算。
    答案:

    python 复制代码
    from functools import lru_cache
    @lru_cache(maxsize=None)
    def fibonacci(n):
        if n < 2:
            return n
        return fibonacci(n-1) + fibonacci(n-2)

总分:100 分

相关推荐
985小水博一枚呀23 分钟前
【EI会议推荐】2025年6月智启未来:通信导航、 机器学习、半导体与AI、数字创新领域国际研讨会总结!
人工智能·python·深度学习·机器学习
chennalC#c.h.JA Ptho1 小时前
Centos系统详解架构详解
linux·经验分享·笔记·系统架构·系统安全
www_pp_1 小时前
# 创建一个功能完备的计算器应用:使用PyQt5和Python
开发语言·python·qt
攻城狮7号1 小时前
大模型微调Fine-tuning:从概念到实践的全面解析
人工智能·python·前沿技术·fine-tuning·大模型微调
basketball6162 小时前
使用pytorch保存和加载预训练的模型方法
人工智能·pytorch·python
蓑笠翁0012 小时前
Python异步编程入门:从同步到异步的思维转变
linux·前端·python
程序员Bears2 小时前
Django进阶:用户认证、REST API与Celery异步任务全解析
后端·python·django
仰望星空的凡人2 小时前
【JS逆向基础】前端基础-HTML与CSS
css·python·html·js逆向
灏瀚星空2 小时前
PyTorch 入门与核心概念详解:从基础到实战问题解决
人工智能·pytorch·python·深度学习·算法·机器学习
Q_Q19632884753 小时前
python小说网站管理系统-小说阅读系统
开发语言·spring boot·python·django·flask·node.js·php