Function and Modules

2023年1月20日

[01]In a Python function, an __________ is a value which is passed in when the function is called.

(a)parameter      (b)argument

A. b

[02]In a Python function, an __________ is a local variable in the declaration of the function.

(a)parameter      (b)argument

A. a

[03]Given the following Python code:

              def myFunction(x):

                            print(x)

              a = “hello"

              myFunction(a)

              is x an argument or parameter?

(a)parameter      (b)argumen

A. a

[04]Given the following Python code:

              def myFunction(x):

                            print(x)

              a = “hello"

              myFunction(a)

              is “a" an argument or a parameter?

(a)argument       (b)parameter

A. a

[05]The __________ statemen is used to sent a a single value inside a function to the caller.

    Calling this statement ends the function.

(a)return             (b)yield

A. a

[06]The _________ statement is called inside a function and adds a value to an implicit iterable object, and continues with the code. When the function completes, the iterable object is returned.

(a)return             (b)yield

A. b

[07]Given the following Python code:

              def myFunction():

                            yield 1

                            yield 2

                            yield 3

              a = 0

                            for x in myFunction():

                                          a = a + x

              What is the value of a?

(a)2       (b)3       (c)6       (d)1

A. c

[08]Given the following Python code:

              def myFunction():

                            return 5

              a = 5

              a = a + myFunction()

              What is the value of a?

(a)10     (b)0       (c)Error is thrown              (d)5

A. a

[09]Given the following Python code:

              def myFunction(x):

                            if x >= 5:

                                          return 1 + x

                            else:

                                          return myFunction(x + 1)

                            a = myFunction(0)

              What is the value of a?

(a)5       (b)Error is thrown              (c)0       (d)6

A. 6

[10]Given the following Python code:

              def myFunction():

                            yield 1

                            yield 2

                            yield 3

                            a = myFunction()

                            b = next(a)

                            What is the value of b?

(a)3       (b)6       (c)2       (d)1

A. d

[11]Given the following Python code:

              def myFuntction(a, b):

                            return a / b

              a = myFunction(b=10, a=5)

              What is the value of a?

(a)5       (b)Error is thrown              (c)2       (d)0.5

A. d

[12]Given the following Python code:

              def myFunction(a, b, c)

                            return (a / b) + c

                            a = myFunction(10, c = 10, b = 5)

                            What is the value of a?

(a)6       (b)7       (c)Error is thrown              (d)12

A. d

[13]Given the following Python code:

              def myFunction(a, b, c):

                            return (a / b) + c

              a = myFunction(10, c = 5, 5)

              What is the value of a?

(a)Error is thrown              (b)11     (c)12     (d)7

A. a

[14]Given the following Python code:

              def myFunction():

                            yield 1

                            yield 2

                            yield 3

              a = list(myFunction())

              b = len(a)

              What is the value of b?

(a)1       (b)Error is thrown              (c)3       (d)6

A. 3

[15]Given the following Python code:

              a = 10

              def myFunction():

                            a = 5

                            return a

              a = 11

              b = myFunction()

              What is the value of b?

(a)5       (b)10     (c)11     (d)Error is thrown

A. a

[16]A __________ function is a nameless function defined without using “def" which can be assigned and referenced as a variable.

(a)concurrent      (b)lambda           (c)assert             (d)global

A. b

[17]The __________ built-in function takes in a function and a list, applies the function to every element of the list, and returns a list containing the results.

(a)reversed()      (b)filter()             (c)map()             (d)reduce()

A. c

[18]The __________ built-in function takes in a function which returns True or False, and a list. It applies the function to every element of the list. Then, it returns a new list containing every element for which the function had return True.

(a)map()             (b)reduced()       (c)filter()             (d)reversed()

A. c

[19]The __________ built-in function takes in a function and a list. For each item in the list, it executes the function taking in the list, it executes the function taking in the item and the next item, and only returning one value. In this way, it aggregates all values in the list into one value.

(a)reduce()         (b)reversed()      (c)filter()             (d)map()

A. d

[20]The __________ built-in function takes in a list and returns a new list with the elements backwards order.

(a)reserved()      (b)reduce()         (c)map()             (d)filter()

A. a

[21]Given the following Python code:

              x = 10 if True else 5

              What is the value of x?

(a)10     (b)None               (c)5       (d)Syntax error thrown

A. a

[22]Given the following Python code:

              x = 10 if False else 5

              What is the value of x?

(a)10     (b)Syntax error thrown     (c)5       (d)None

A. c

[23]The __________ statement is used to reference an external Python file.

(a)class               (b)module           (c)package

(d)shebang/hashhang      (e)import

A. e

[24]A _________ is a file containing functions which you import into an application.

(a)module           (b)import             (c)shebang/hashhang

(d)class               (e)package

A. a

[25]A __________ is a heriarchical namespaces represented by a directory containing a file named __init__.py.

(a)shebang/hashbang      (b)module           (c)import

(d)class               (e)package

A. e

[26]A __________ is an optional first lin of a file which indicates the interpreter environment that a Python file is to be run with.

(a)module           (b)shebang/hashbang      (c)class

(d)import             (e)package

A. b

[27]A __________ is a template which can be used to initialize a specific type of object.

(a)shebang/hashbang      (b)package          (c)class

(d)import             (e)module

A. c

[28]The existence of an __________.py file indicates to the compiler that a directory is a package.

(a)__doc__         (b)__name__      (c)__init__

A. c

[29]The __________ built-in variable evaluates to the name of the current module.

(a)__doc__         (b)__init__          (c)__name__

A. c

[30]The __________ will print out the string comment and is the first line after the class or method header.

(a)__name__      (b)__init__          (c)__doc__

A. c

[31]Suppose you have a Python file named myPython.py, which contains the following code:

              def myFunc():

                            print(“Hello World")

              Which of the following statement would you use to import this function.

(a)import myPython.py                   (b)import myPython.myFunc

(c)import myPython                        (d)import myFunc

A. c

[32]Which of the following is true regardin.pyc file? (Choose 2)

(a)You need both the .py and the .pyc file in order to run a module

(b)They are recreated every time the interpreter is run

(c)They contain the compiled bytecode of a module

(d)They are created by the Python interpreter

A. c, d