Introduction to Programming Using Python, Y. Daniel Liang

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Chapter 6 Functions

Sections 6.2 Defining a Function
6.1  If a function does not return a value, by default, it returns ___________.
A. None
B. int
C. double
D. public
E. null

6.2  The header of a function consists of ____________.
A. function name
B. function name and parameter list
C. parameter list

6.3  A function _________.
A. must have at least one parameter
B. may have no parameters
C. must always have a return statement to return a value
D. must always have a return statement to return multiple values

Sections 6.3 Calling a Function
6.4  Arguments to functions always appear within __________.
A. brackets
B. parentheses
C. curly braces
D. quotation marks

6.5  Does the function call in the following function cause syntax errors?

import math
def main():
    math.sin(math.pi)

main()
A. Yes
B. No

6.6  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

Sections 6.4 Functions With/Without Return Values
6.7  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.

6.8  A function with no return statement returns ______.
A. void
B. nothing
C. 0
D. None

6.9  Consider the following incomplete code:

def f(number):
  # Missing function body

print(f(5))

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

Sections 6.5 Positional and Keyword Arguments
6.10  Given the following function header:

def f(p1, p2, p3, p4)

Which of the following is correct to invoke it?
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)

6.11  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)?

A. aaaaa
B. aaaa
C. aaa
D. invalid call
E. infinite loop

6.12  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)?
A. aaaaa
B. aaaa
C. aaa
D. invalid call
E. infinite loop

6.13  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)
A. 0
B. 1
C. 2
D. 3

6.14  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(n = k, message = "A message")
A. 0
B. 1
C. 2
D. 3

Sections 6.6 Passing Parameters by Values
6.15  When you invoke a function with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.
A. function invocation
B. pass by value
C. pass by reference
D. pass by name

Section 6.9 The Scope of Variables
6.16  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

6.17  A variable defined outside a function is referred to as __________.
A. a global variable
B. a function variable
C. a block variable
D. a local variable

6.18  Whenever possible, you should avoid using __________.
A. global variables
B. function parameters
C. global constants
D. local variables

6.19  What will be displayed by the following code?

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

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

6.20  What will be displayed by the following code?

x = 1
def f1():
    x = 3
    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

6.21  What will be displayed by the following code?

x = 1
def f1():
    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

6.22  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

Section 6.10 Default Arguments
6.23  What will be displayed by the following code?

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

f1()
A. 1 3
B. 3 1
C. The program has a runtime error because x and y are not defined.
D. 1 1
E. 3 3

6.24  What will be displayed by the following code?

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

f1(21)
A. 1 3
B. 2 3
C. The program has a runtime error because x and y are not defined.
D. 3 2
E. 3 3

6.25  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)
A. 1 3
B. 2 3
C. The program has a runtime error because x and y are not defined.
D. 3 2
E. 3 3

6.26  Which of the following function headers is correct?
A. def f(a = 1, b):
B. def f(a = 1, b, c = 2):
C. def f(a = 1, b = 1, c = 2):
D. def f(a = 1, b = 1, c = 2, d):

Section 6.11 Returning Multiple Values
6.27  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)
A. 1 3
B. 3 1
C. The program has a runtime error because the function returns the multiple values
D. 3 -1
E. -1 3

Section 6.13 Function Abstraction and Stepwise Refinement
6.28  __________ is to implement one function in the structure chart at a time from the top to the bottom.
A. Bottom-up approach
B. Top-down approach
C. Bottom-up and top-down approach
D. Stepwise refinement

6.29  __________ is a simple but incomplete version of a function.
A. A stub
B. A function
C. A function developed using botton-up approach
D. A function developed using top-down approach