6. Functions - 21 Questions

6. Functions

21 Questions
Created by Y. Daniel Liang
Free
Functions are little self-contained programs that perform a specific task, which you can incorporate into your own, larger programs. After you have created a function, you can use it at any time, in any place. This saves you the time and effort of having to retell the computer what to do every time it does a common task, for example getting the user to type something in.
1. If a function does not return a value, by default, it returns ___________.

None
int
double
null

2. The header of a function consists of ____________.

function name
function name and parameter list
parameter list
None of these

3. A function _________.

must have at least one parameter
may have no parameters
must always have a return statement to return a value
must always have a return statement to return multiple values

4. Arguments to functions always appear within __________.

brackets
parentheses
curly braces
quotation marks

5. 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 heap
storage area
a stack
an array

6. Which of the following should be defined as a None function?

Write a function that prints integers from 1 to 100.
Write a function that returns a random integer from 1 to 100.
Write a function that checks whether current second is an integer from 1 to 100.
Write a function that converts an uppercase letter to lowercase.

7. A function with no return statement returns ______.

void
nothing
0
None

8. Consider the following incomplete code:

```def f(number):   # Missing function body print(f(5))```

The missing function body should be ________.

return "number"
print(number)
print("number")
return number

9. Given the following function

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

What will be displayed by the call nPrint('a', 4)?

aaaaa
aaaa
aaa
infinite loop

10. Given the following function

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

What is k after invoking nPrint("A message", k)?

```k = 2 nPrint("A message", k)```

0
1
2
3

11. When you invoke a function with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.

function invocation
pass by value
pass by reference
pass by name

12. A variable defined inside a function is referred to as __________.

a global variable
a function variable
a block variable
a local variable

13. A variable defined outside a function is referred to as __________.

a global variable
a function variable
a block variable
a local variable

14. What will be displayed by the following code?

```x = 1 def f1():     y = x + 2     print(y) f1() print(x)```

1 3
3 1
The program has a runtime error because x is not defined.
1 1

15. What will be displayed by the following code?

```x = 1 def f1():     x = x + 2     print(x) f1() print(x)```

1 3
3 1
The program has a runtime error because x is not defined.
1 1

16. What will be displayed by the following code?

```def f1(x = 1, y = 2):     x = x + y     y += 1     print(x, y)```

1 3
3 1
1 1
3 3

17. What will be displayed by the following code?

```def f1(x = 1, y = 2):     x = x + y     y += 1     print(x, y) f1(y = 2, x = 1)```

1 3
2 3
3 2
3 3

18. Which of the following function headers is correct?

def f(a = 1, b):
def f(a = 1, b, c = 2):
def f(a = 1, b = 1, c = 2):
def f(a = 1, b = 1, c = 2, d):

19. What will be displayed by the following code?

```def f1(x = 1, y = 2):     return x + y, x - y x, y = f1(y = 2, x = 1) print(x, y)```

1 3
3 1
3 -1
-1 3

20. __________ is to implement one function in the structure chart at a time from the top to the bottom.

Bottom-up approach
Top-down approach
Bottom-up and top-down approach
Stepwise refinement