Functions Flashcards

1
Q

What is a function?

A

A function is a collection of statements grouped together that performs an operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
Each time a function is invoked, the system stores parameters and local variables in an area of memory, known as \_\_\_\_\_\_\_, which stores elements in last-in first-out fashion.
 A. a heap
 B. storage area
 C. a stack
 D. an array
A

C. a stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a void function?

A

A function that does not return a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the following should be defined as a None function?
A. Write a function that prints integers from 1 to 100.
B. Write a function that returns a random integer from 1 to 100.
C. Write a function that checks whether a number is from 1 to 100.
D. Write a function that converts an uppercase letter to lowercase.

A

A. Write a function that prints integers from 1 to 100.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Consider the following incomplete code:

def f(number):
    # Missing function body
    # return the value number

print(f(5))

The missing function body should be \_\_\_\_\_\_\_\_.
 A. return "number"
 B. print(number)
 C. print("number")
 D. return number
A

D. return number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Given the following function header, which of the following is correct to invoke it?

def f(p1, p2, p3, p4)
 A. f(1, 2, 3, 4)
 B. f(p1 = 1, 2, 3, 4)
 C. f(p1 = 1, p2 = 2, p3 = 3, 4)
 D. f(p1 = 1, p2 = 2, p3 = 3, p4 = 4)
 E. f(1, 2, 3, p4 = 4)
A

A. f(1, 2, 3, 4)
D. f(p1 = 1, p2 = 2, p3 = 3, p4 = 4)
E. f(1, 2, 3, p4 = 4)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Given the following function, what will be displayed by the call nPrint(‘a’, 4)?

def nPrint(message, n):
    while n > 0:
        print(message, end = '')
        n -= 1
 A. aaaaa
 B. aaaa
 C. aaa
 D. invalid call
 E. infinite loop
A

B. aaaa

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given the following function, what will be displayed by the call nPrint(‘a’, 4)?

def nPrint(message, n):
    while n > 0:
        print(message, end = '')
    n -= 1
 A. aaaaa
 B. aaaa
 C. aaa
 D. invalid call
 E. infinite loop
A

E. infinite loop

Hint: Note that n -= 1 is outside of the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an argument?

A

An argument is an object that is passed to a function when the function is invoked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a parameter?

A

A parameter is a variable that receives an argument that is passed to a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an immutable object?

A

An object whose contents cannot be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Numbers and strings are ________ objects.

A

immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is k after running the following code?

def nPrint(message, n):
while n > 0:
print(message)
n -= 1

k = 2
nPrint(n = k, message = "A message")
 A. 0
 B. 1
 C. 2
 D. 3
A

C. 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Whenever possible, you should avoid using \_\_\_\_\_\_\_\_\_\_.
 A. global variables
 B. function parameters
 C. global constants
 D. local variables
A

A. global variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
A variable defined inside a function is referred to as \_\_\_\_\_\_\_\_\_\_.
 A. a global variable
 B. a function variable
 C. a block variable
 D. a local variable
A

D. a local variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What will be displayed by the following code?

x = 1
def f1():
    x = 3
    print(x, end = " ")
f1()
print(x)
 A. 1 3
 B. 3 1
 C. The program has a runtime error because x is not defined.
 D. 1 1
 E. 3 3
A

B. 3 1

17
Q

What will be displayed by the following code?

x = 1
def f1():
    global x
    x = x + 2
    print(x)
f1()
print(x) 
 A. 1 3
 B. 3 1
 C. The program has a runtime error because x is not defined.
 D. 1 1
 E. 3 3
A

E. 3 3

18
Q
What is the output of the following code?
def m(x = 1, y = 2):
    return x + y

print(m(2))

A

4

19
Q
What is the output of the following code?
def m(x = 1, y = 2):
    return x + y, x * y

a1, a2 = m(2, 1)
print(a1, a2)

A

3 2

20
Q

What is function abstraction?

A

Function abstraction is a technique in software development that hides detailed implementation. Function abstraction is defined as separating the use of a function from its implementation. The client can use a function without knowing how it is implemented. If you decide to change the implementation, the client program will not be affected.

21
Q

Stepwise refinement, also known as the __________ strategy, means to break down the ______ into _______.

A

diving-and-conquer, problem, subproblems

22
Q

Name four benefits of Stepwise Refinement.

A

Simpler Program
Reusing Functions
Easier Developing, Debugging, and Testing
Better Facilitating Teamwork