Test 2 Flashcards

Chapter 5 and 6 - Checkpoint questions

1
Q

What is a function?

A

A function is a group of statements that exist within a program for the purpose of performing a specific task.

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

What is meant by the phrase “divide and conquer”?

A

A large task is divided into several smaller tasks that are easily performed.

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

How do functions help you reuse code in a program?

A

If a specific operation is performed in several places in a program, a function can be written once to perform that operation, and then be executed any time it is needed.

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

How can functions make the development of multiple programs faster?

A

Functions can be written for the common tasks that are needed by the different programs. Those functions can then be incorporated into each program that needs them.

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

How can functions make it easier for programs to be developed by teams of programmers?

A

When a program is developed as a set of functions in which each performs an individual task, then different programmers can be assigned the job of writing different functions.

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

A function definition has what two parts?

A

A function definition has two parts: a header and a block. The header indicates the starting point of the function, and the block is a list of statements that belong to the function.

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

What does the phrase “calling a function” mean?

A

To call a function means to execute the function.

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

When a function is executing, what happens when the end of the function’s block is reached?

A

When the end of the function is reached, the computer returns back to the part of the program that called the function, and the program resumes execution at that point.

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

Why must you indent the statements in a block?

A

Because the Python interpreter uses the indentation to determine where a block begins and ends

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

What is a local variable? How is access to a local variable restricted?

A

A local variable is a variable that is declared inside a function.

It belongs to the function in which it is declared, and only statements in the same function can access it.

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

What is a variable’s scope?

A

The part of a program in which a variable may be accessed

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

Is it permissible for a local variable in one function to have the same name as a local variable in a different function?

A

Yes

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

What are the pieces of data that are passed into a function called?

A

Arguments

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

What are the variables that receive pieces of data in a function called?

A

Parameters

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

What is a parameter variable’s scope?

A

A parameter variable’s scope is the entire function in which the parameter is declared.

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

When a parameter is changed, does this affect the argument that was passed into the parameter?

A

No

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

The following statements call a function named show_data.

Which of the statements passes arguments by position, and which passes keyword arguments?

a. show_data(name=’Kathryn’, age=25)

b. show_data(‘Kathryn’, 25)

A

A. Keyword

B. Passes by Position

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

What is the scope of a global variable?

A

The entire Program

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

Give one good reason that you should not use global variables in a program.

A
  • They make debugging difficult
  • Makes a program hard to understand
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is a global constant?

Is it permissible to use global constants in a program?

A

A global constant is a name that is available to every function in the program.

It is permissible to use global constants. Because their value cannot be changed during the program’s execution, you do not have to worry about its value being altered.

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

How does a value-returning function differ from the void functions?

A

The difference is that a value returning function returns a value back to the statement that called it. A simple function does not return a value.

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

What is a library function?

A

A prewritten function that performs some commonly needed task

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

Why are library functions like “black boxes”?

A

The term “black box” is used to describe any mechanism that accepts input, performs some operation (that cannot be seen) using the input, and produces output.

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

What does the following statement do?

x = random.randint(1, 100)

A

It assigns a random integer in the range of 1 through 100 to the variable x.

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

What does the following statement do?

print(random.randint(1, 20))

A

It prints a random integer in the range of 1 through 20.

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

What does the following statement do?

print(random.randrange(10, 20))

A

It prints a random integer in the range of 10 through 19.

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

What does the following statement do?

print(random.random())

A

It prints a random floating-point number in the range of 0.0 up to, but not including, 1.0.

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

What does the following statement do?

print(random.uniform(0.1, 0.5))

A

It prints a random floating-point number in the range of 0.1 through 0.5.

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

When the random module is imported, what does it use as a seed value for random number generation?

A

It uses the system time, retrieved from the computer’s internal clock.

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

What happens if the same seed value is always used for generating random numbers?

A

If the same seed value were always used, the random number functions would always generate the same series of pseudorandom numbers.

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

What is the purpose of the return statement in a function?

A

It returns a value back to the part of the program that called it.

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

Look at the following function definition:

def do_something(number):
return number * 2

a. What is the name of the function?

b. What does the function do?

c. Given the function definition, what will the following statement display?

A

a . do_something

b. It returns a value that is twice the argument passed to it.

c. 20

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

What is a Boolean function?

A

A function that returns either True or False

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

What import statement do you need to write in a program that uses the math module?

A

import math

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

Write a statement that uses a math module function to get the square root of 100 and assigns it to a variable.

A

square_root = math.sqrt(100)

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

Write a statement that uses a math module function to convert 45 degrees to radians and assigns the value to a variable.

A

angle = math.radians(45)

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

6.1 What is an output file?

A

A file to which a program writes data. It is called an output file because the program sends output to it.

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

What is an input file?

A

A file from which a program reads data. It is called an input file because the program receives input from it.

39
Q

What three steps must be taken by a program when it uses a file?

A

(1) Open the file. (2) Process the file. (3) Close the file.

40
Q

In general, what are the two types of files?

What is the difference between these two types of files?

A

Text and binary.

A text file contains data that has been encoded as text using a scheme such as ASCII. Even if the file contains numbers, those numbers are stored in the file as a series of characters. As a result, the file may be opened and viewed in a text editor such as Notepad.

A binary file contains data that has not been converted to text. As a consequence, you cannot view the contents of a binary file with a text editor.

41
Q

What are the two types of file access?

What is the difference between these two?

A

Sequential and direct access.

When you work with a sequential access file, you access data from the beginning of the file to the end of the file. When you work with a direct access file, you can jump directly to any piece of data in the file without reading the data that comes before it.

42
Q

When writing a program that performs an operation on a file, what two file-associated names do you have to work with in your code?

A

The file’s name on the disk and the name of a variable that references a file object.

43
Q

If a file already exists, what happens to it if you try to open it as an output file (using the ‘w’ mode)?

A

The file’s contents are erased.

44
Q

What is the purpose of opening a file?

A

Opening a file creates a connection between the file and the program. It also creates an association between the file and a file object.

45
Q

What is the purpose of closing a file?

A

Closing a file disconnects the program from the file.

46
Q

What is a file’s read position?

Initially, where is the read position when an input file is opened?

A

A file’s read position marks the location of the next item that will be read from the file.

When an input file is opened, its read position is initially set to the first item in the file.

47
Q

In what mode do you open a file if you want to write data to it, but you do not want to erase the file’s existing contents?

When you write data to such a file, to what part of the file is the data written?

A

You open the file in append mode.

When you write data to a file in append mode, the data is written to the end of the file’s existing contents.

48
Q

Write a short program that uses a for loop to write the numbers 1 through 10 to a file.

A

outfile = open(‘numbers.txt’, ‘w’)
for num in range(1, 11):
outfile.write(str(num) + ‘\n’)
outfile.close()

49
Q

What does it mean when the readline method returns an empty string?

A

The readline method returns an empty string (‘’) when it has attempted to read beyond the end of a file.

50
Q

Assume the file data.txt exists and contains several lines of text. Write a short program using the while loop that displays each line in the file.

A

infile = open(‘data.txt’, ‘r’)
line = infile.readline()
while line != ‘’:
print(line)
line = infile.readline()
infile.close()

51
Q

Revise the program that you wrote for Checkpoint 6.14 to use the for loop instead of the while loop.

A

infile = open(‘data.txt’, ‘r’)
for line in infile:
print(line)
infile.close()

52
Q

What is a record? What is a field?

A

A record is a complete set of data that describes one item.

A field is a single piece of data within a record.

53
Q

Describe the way that you use a temporary file in a program that modifies a record in a sequential access file.

A

You copy all the original file’s records to the temporary file, but when you get to the record that is to be modified, you do not write its old contents to the temporary file. Instead, you write its new, modified values to the temporary file. Then, you finish copying any remaining records from the original file to the temporary file.

54
Q

Describe the way that you use a temporary file in a program that deletes a record from a sequential file.

A

You copy all the original file’s records to the temporary file, except for the record that is to be deleted. The temporary file then takes the place of the original file. You delete the original file and rename the temporary file, giving it the name that the original file had on the computer’s disk.

55
Q

Briefly describe what an exception is.

A

An exception is an error that occurs while a program is running. In most cases, an exception causes a program to abruptly halt.

56
Q

If an exception is raised and the program does not handle it with a try/except statement, what happens?

A

The program halts.

57
Q

What type of exception does a program raise when it tries to open a nonexistent file?

A

IOError

58
Q

What type of exception does a program raise when it uses the float function to convert a non-numeric string to a number?

A

ValueError

59
Q

A group of statements that exist within a program for the purpose of performing a specific task is a(n) _________.

A

function

60
Q

A design technique that helps to reduce the duplication of code within a program and is a benefit of using functions is_____.

A

divide and conquer

61
Q

The first line of a function definition is known as the _______.

A

header

62
Q

You ______ a function to execute it.

A

call

63
Q

A design technique that programmers use to break down an algorithm into functions is known as _____.

A

top-down design

64
Q

A ______ is a diagram that gives a visual representation of the relationships between functions in a program.

A

hierarchy chart

65
Q

The ______ keyword is ignored by the Python interpreter and can be used as a placeholder for code that will be written later.

A

pass

66
Q

A ______ is a variable that is created inside a function.

A

local variable

67
Q

A(n) ____ is the part of a program in which a variable may be accessed.

A

scope

68
Q

A(n) ______ is a piece of data that is sent into a function.

A

argument

69
Q

A(n) ______ is a special variable that receives a piece of data when a function is called.

A

parameter

70
Q

A variable that is visible to every function in a program file is a ______ .

A

global variable

71
Q

When possible, you should avoid using _____ variables in a program.

A

global

72
Q

This is a prewritten function that is built into a programming language.

A

library function

73
Q

This standard library function returns a random integer within a specified range of values.

A

random.randint

74
Q

This standard library function returns a random floating-point number in the range of 0.0 up to 1.0 (but not including 1.0).

A

random.random

75
Q

This standard library function returns a random floating-point number within a specified range of values.

A

random.uniform

76
Q

This statement causes a function to end and sends a value back to the part of the program that called the function.

A

return

77
Q

This is a design tool that describes the input, processing, and output of a function.

A

IPO chart

78
Q

This type of function returns either True or False.

A

Boolean

79
Q

This is a math module function.

A

sqrt

80
Q

A file that data is written to is known as a(n) ______.

A

output file

81
Q

A file that data is read from is known as a(n) ______ .

A

input file

82
Q

Before a file can be used by a program, it must be  ______ .

A

opened

83
Q

When a program is finished using a file, it should do this.

A

close the file

84
Q

The contents of this type of file can be viewed in an editor such as Notepad.

A

text file

85
Q

This type of file contains data that has not been converted to text.

A

binary file

86
Q

When working with this type of file, you access its data from the beginning of the file to the end of the file.

A

sequential access

87
Q

When working with this type of file, you can jump directly to any piece of data in the file without reading the data that comes before it.

A

direct access

88
Q

This is a small “holding section” in memory that many systems write data to before writing the data to a file.

A

buffer

89
Q

This marks the location of the next item that will be read from a file.

A

read position

90
Q

When a file is opened in this mode, data will be written at the end of the file’s existing contents.

A

append mode

91
Q

This is a single piece of data within a record.

A

field

92
Q

When an exception is generated, it is said to have been ______.

A

raised

93
Q

This is a section of code that gracefully responds to exceptions.

A

exception handler

94
Q

You write this statement to respond to exceptions.

A

try/except