Python chapter 6 Quiz

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

Write a function that prints integers from 1 to 100.

A variable defined outside a function is referred to as

a global variable

A variable defined inside a function is referred to as

a local variable

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 stack

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

aaaa

Numbers and Strings

are immutable objects.

Function Header

begins with the def keyword, followed by the function's name and parameters, and ends with a colon.

Consider the following incomplete code: def f(number): # Missing function body # return the value number print(f(5)) The missing function body should be

return number

The header of a function consists of

the keyword def, followed by the function?s name and parameters, and ends with a colon.

What is the output of the following code? def m(n): n += 1 def main(): n = 1 m(n) print(n) main()

1

What will be displayed by the following code? x = 1 def f1(): print(x, end = " ") f1() print(x)

1 1

What is the return value from invoking m("1234")? def m(s): result = 0 for i in range(0, len(s)): result = result * 10 + int(s[i]) - int('0') return result

1234

What is k after running the following code? def nPrint(message, n): while n > 0: print(message) n -= 1 k = 2 nPrint("A message", k)

2

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")

2

What is the output of following code? def m(n): n += 1 print(n, end = '') def main(): m(1) main()

2

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)

3 -1

What will be displayed by the following code? x = 1 def f1(): x = 3 print(x, end = " ") f1() print(x)

3 1

What will be displayed by the following code? x = 1 def f1(): y = x + 2 print(y, end = " ") f1() print(x)

3 1

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)

3 2

What will be displayed by the following code? def f1(x = 1, y = 2): x = x + y y += 1 print(x, y) f1(2, 1)

3 2

What will be displayed by the following code? def f1(x = 1, y = 2): x = x + y y += 1 print(x, y) f1()

3 3

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)

3 3

What will be displayed by the following code? x = 1 def f1(): global x x = x + 2 print(x) f1() print(x)

3 3

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

4

What is the output from invoking m("1234")? def m(s): for i in range(len(s) - 1, -1, -1): print(s[i], end = '')

4321

What is the output from invoking m(1234)? def m(n): while n != 0: print(n % 10, end = '') n = int(n / 10)

4321

Write an expression that a random number between 6.5 and 56.5 (excluding 56.5).

5.5 + random.random() * 50

is a simple but incomplete version of a function.

A stub

What is the return value from invoking p(27)? def p(n): for i in range(2, n): if n % i == 0: return True return False

False

Does the function call main() in the following code cause syntax errors? import math def main(): return math.sin(math.pi) main()

No

A function with no return statement returns

None

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

Top-down approach

What is the return value from invoking p(77)? def p(n): for i in range(2, n): if n % i == 0: return True return False

True

Write an expression that returns a random character between B and M, inclusively.

chr(random.randint(ord('B'), ord('M')))

Write an expression that returns obtains a random lowercase letter.

chr(random.randint(ord('a'), ord('z')))

Which of the following function headers is correct?

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

Given the following function header, which of the following is correct to invoke it? def f(p1, p2, p3, p4)

f(1, 2, 3, 4) f(p1 = 1, p2 = 2, p3 = 3, p4 = 4) f(1, 2, 3, p4 = 4)

Whenever possible, you should avoid using

global variables

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

infinite loop

None Function

is a function that does not return a value.

Information Hiding

is achieved by separating the use of a function from its implementation.

Pass-By-Value

is to pass the reference value of each argument to the parameter when invoke a function.

A function

may have no parameters

Arguments to functions always appear within

parentheses

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

pass by value

Write an expression that returns a random integer between 34 and 55, inclusively.

random.randint(34, 55) y is 1

Function Abstraction

refers to that the details of the implementation are encapsulated in the function and hidden from the client who invokes the function.

Actual Parameter

refers to the value passed to the function when invoking the function.

Formal Parameter

refers to the variables defined in the function header.


Ensembles d'études connexes

International Business Chapter Midterm

View Set

AP US History: Chapter 25 Review

View Set

Community Health Exam 3 Practice Questions

View Set

CSCI 464 (Multiple Choice) Midterm Review

View Set

Health Assessment in Nursing Weber & Kelley 5th edition Ch 1-34

View Set

Business and Society Quiz Chapter 16

View Set