• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/281

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

281 Cards in this Set

  • Front
  • Back
  • 3rd side (hint)
What is a program?

A program is a set of instructions that a computer follows to perform a task.

Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What is hardware?
Hardware is all the physical devices, or components, of which a computer is made.
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
List the five major components of a computer system.
The central processing unit (CPU), main memory, secondary storage devices, input devices, and output devices
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What part of the computer actually runs programs?
The CPU
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What part of the computer serves as a work area to store a program and its data while the program is running?
Main memory
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What part of the computer holds data for long periods of time, even when there is no power to the computer?
Secondary storage
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What part of the computer collects data from people and from other devices?
Input device
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What part of the computer formats and presents data for people or other devices?
Output device
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What fundamental set of programs control the internal operations of the computer’s hardware?
The operating system
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What do you call a program that performs a specialized task, such as a virus scanner, a file compression program, or a data backup program?
A utility program
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
Word processing programs, spreadsheet programs, email programs, web browsers, and game programs belong to what category of software?
Application software
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What amount of memory is enough to store a letter of the alphabet or a small number?
One byte
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What do you call a tiny “switch” that can be set to either on or off?
A bit
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
In what numbering system are all numeric values written as sequences of 0s and 1s?
The binary numbering system
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What is the purpose of ASCII?
It is an encoding scheme that uses a set of 128 numeric codes to represent the English letters, various punctuation marks, and other characters. These numeric codes are used to store characters in a computer’s memory. (ASCII stands for the American Standard Code for Information Interchange.)
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What encoding scheme is extensive enough to represent the characters of many of the languages in the world?
Unicode
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What do the terms “digital data” and “digital device” mean?
Digital data is data that is stored in binary, and a digital device is any device that works with binary data.
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
A CPU understands instructions that are written only in what language?
Machine language
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
A program has to be copied into what type of memory each time the CPU executes it?
Main memory, or RAM
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
When a CPU executes the instructions in a program, it is engaged in what process?
The fetch-decode-execute cycle
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What is assembly language?
It is an alternative to machine language. Instead of using binary numbers for instructions, assembly language uses short words that are known as mnemonics.
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What type of programming language allows you to create powerful and complex programs without knowing how the CPU works?
A high-level language
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
Each language has a set of rules that must be strictly followed when writing a program. What is this set of rules called?
Syntax
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What do you call a program that translates a high-level language program into a separate machine language program?
A compiler
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What do you call a program that both translates and executes the instructions in a high-level language program?
An interpreter
Ch1 Using interactive Mode in IDLE, p. 23Performing Exercise 2, p.28
What type of mistake is usually caused by misspelled keyword, a missing punctuation character, or the incorrect use of an operator?
A syntax error
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
Who is a programmer’s customer?
Any person, group, or organization that is asking you to write a program
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
What is a software requirement?
A single function that the program must perform in order to satisfy the customer
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
What is an algorithm?
A set of well-defined logical steps that must be taken to perform a task
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
What is pseudocode?
An informal language that has no syntax rules and is not meant to be compiled or executed. Instead, programmers use pseudocode to create models, or “mock-ups,” of programs.
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
What is a flowchart?
A diagram that graphically depicts the steps that take place in a program
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
What do each of the following symbols mean in a flowchart? • Oval • Parallelogram • Rectangle
Ovals are terminal symbols. Parallelograms are either output or input symbols. Rectangles are processing symbols.
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
Write a statement that displays your name.
print('Jimmy Smith')
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
Write a statement that displays the following text: Python's the best!
print("Python's the best!”)
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
Write a statement that displays the following text: The cat said "meow."
print('The cat said "meow”')
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
What is a variable?
A name that references a value in the computer’s memory
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
Which of the following are illegal variable names in Python, and why? x 99bottles july2009 theSalesFigureForFiscalYear r&d grade_report
99bottles is illegal because it begins with a number. r&d is illegal because the & character is not allowed.
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
Is the variable name Sales the same as sales? Why or why not?
No, it is not because variable names are case sensitive.
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
Is the following assignment statement valid or invalid? If it is invalid, why? 72 = amount
It is invalid because the variable that is receiving the assignment (in this case amount ) must appear on the left side of the = operator.
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
What will the following code display? val = 99 print('The value is', 'val')
The value is val.
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
Look at the following assignment statements: value1 = 99 value2 = 45.9 value3 = 7.0 value4 = 7 value5 = 'abc' After these statements execute, what is the Python data type of the values referenced by each variable?
value1 will reference an int . value2 will reference a float . value3 will reference a float . value4 will reference an int . value5 will reference an str (string).
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
What will be displayed by the following program? my_value = 99 my_value = 0 print(my_value)
0
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
You need the user of a program to enter a customer’s last name. Write a statement that prompts the user to enter this data and assigns the input to a variable.
last_name = input("Enter the customer's last name: ”)
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
You need the user of a program to enter the amount of sales for the week. Write a statement that prompts the user to enter this data and assigns the input to a variable.
sales = float(input('Enter the sales for the week: '))
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
Complete the following table by writing the value of each expression in the Value column. Expression Value 6 + 3 * 5 ______ 12 / 2 - 4 ______ 9 + 14 * 2 - 6 ______ (6 + 2) * 3 ______ 14 / (11 - 4) ______ 9 + 12 * (8 - 3) ______
Here is the completed table: Expression Value 6 + 3 * 5 21 12 / 2 − 4 29 + 14 * 2 − 6 31(6 + 2) * 3 2414 / (11 − 4) 29 + 12 * (8 − 3) 692.20 42.21 1
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
What value will be assigned to result after the following statement executes? result = 9 // 2
4
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
What value will be assigned to result after the following statement executes? result = 9 % 2
1
Ch2 The print Function, p.36Reading Input from the keyboard, p.49The Sales Predition Problem, p.77
What is a control structure?
A logical design that controls the order in which a set of statements execute
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
What is a decision structure?
It is a program structure that can execute a set of statements only under certain circumstances.
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
What is a single alternative decision structure?
A decision structure that provides a single alternative path of execution. If the condition that is being tested is true, the program takes the alternative path.
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
What is a Boolean expression?
An expression that can be evaluated as either true or false
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
What types of relationships between values can you test with relational operators?
You can determine whether one value is greater than, less than, greater than or equal to, less than or equal to, equal to, or not equal to another value.
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
Write an if statement that assigns 0 to x if y is equal to 20.
if y == 20: x = 0
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
Write an if statement that assigns 0.2 to commissionRate if sales is greater than or equal to 10000.
if sales >= 10000: commissionRate = 0.2
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
How does a dual alternative decision structure work?
A dual alternative decision structure has two possible paths of execution; one path is taken if a condition is true, and the other path is taken if the condition is false.
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
What statement do you use in Python to write a dual alternative decision structure?
if - else
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
When you write an if-else statement, under what circumstances do the statements that appear after the else clause execute?
When the condition is false
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
What would the following code display? if 'z' < 'a': print('z is less than a.')else: print('z is not less than a.')
z is not less than a.
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
What would the following code display?s1 = 'New York' s2 = 'Boston' if s1 > s2: print(s2) print(s1)else: print(s1) print(s2)
Boston New York
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
Convert the following code to an if-elif-else statement: if number == 1: print('One') else: if number == 2: print('Two') else: if number == 3: print('Three') else: print('Unknown')
if number == 1: print('One') elif number == 2: print('Two') elif number == 3: print('Three') else: print('Unknown')
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
What is a compound Boolean expression?
It is an expression that is created by using a logical operator to combine two Boolean subexpressions.
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115

The following truth table shows various combinations of the values true and false connected by a logical operator. Complete the table by circling T or F to indicate whether the result of such a combination is true or false. Logical Expression Result (circle T or F) True and False T F True and True T F False and True T F False and False T F True or False T F True or True T F False or True T F False or False T F not True T F not False T F

F T F F T T T F F T
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115

Assume the variables a = 2, b = 4, and c = 6. Circle the T or F for each of the following conditions to indicate whether its value is true or false.


a == 4 or b > 2 T F


6 <= c and a > 3 T F


1 != b and c != 3 T F


a >= -1 or a <= b T F


not (a > 2) T F

T F T T T

Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115

Explain how short-circuit evaluation works with the and and or operators.

The and operator: if the expression on the left side of the and operator is false, the expression on the right side will not be checked. the or operator: if the expression on the left side of the or operator is true, the expression on the right side will not be checked.

Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
Write an if statement that displays the message “The number is valid” if the value referenced by speed is within the range 0 through 200.

if speed >= 0 and speed <= 200: print('The number is valid')

Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
Write an if statement that displays the message “The number is not valid” if the value referenced by speed is outside the range 0 through 200.
if speed < 0 or speed > 200: print('The number is not valid')
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
What values can you assign to a bool variable?
True or False
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
What is a flag variable?
A variable that signals when some condition exists in the program
Ch3 the if Statement, p.81the if-else Statement, p. 90The Areas of Rectangles Problem,p.115
What is a repetition structure?
A structure that causes a section of code to repeat
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What is a condition-controlled loop?
A loop that uses a true/false condition to control the number of times that it repeats
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What is a count-controlled loop?
A loop that repeats a specific number of times
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What is a loop iteration?
An execution of the statements in the body of the loop
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
Does the while loop test its condition before or after it performs an iteration?
Before
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
How many times will 'Hello World' be printed in the following program? count = 10 while count < 1: print('Hello World')
None. The condition count < 0 will be false to begin with.
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What is an infinite loop?
A loop that has no way of stopping and repeats until the program is interrupted.
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
Rewrite the following code so it calls the range function instead of using the list [0, 1, 2, 3, 4, 5]. for x in [0, 1, 2, 3, 4, 5]: print('I love to program!')
for x in range(6): print('I love to program!')
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What will the following code display? for number in range(6): print(number)
0 1 2 3 5
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What will the following code display? for number in range(2, 6): print(number)
2 3 4 5
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What will the following code display? for number in range(0, 501, 100): print(number)
0 100 200 300 400 500
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What will the following code display? for number in range(10, 5, -1): print(number)
10 9 8 7 6
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What is an accumulator?
A variable that is used to accumulate the total of a series of numbers
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
Should an accumulator be initialized to any specific value? Why or why not?
Yes, it should be initialized with the value 0. This is because values are added to the accumulator by a loop. If the accumulator does not start at the value 0, it will not contain the correct total of the numbers that were added to it when the loop ends.
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What will the following code display? total = 0 for count in range(1, 6): total = total + count print(total)
15
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What will the following code display? number1 = 10 number2 = 5 number1 = number1 + number2 print(number1) print(number2)
15 5
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
Rewrite the following statements using augmented assignment operators: a) quantity = quantity + 1 b) days_left = days_left - 5 c) price = price * 10 d) price = price / 2
a) quantity += 1 b) days_left −= 5 c) price *= 10 d) price /= 2
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What is a sentinel?
A sentinel is a special value that marks the end of a list of items.
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
Why should you take care to choose a distinctive value as a sentinel?
A sentinel value must be unique enough that it will not be mistaken as a regular value in the list.
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What does the phrase “garbage in, garbage out” mean?
It means that if bad data (garbage) is provided as input to a program, the program will produce bad data (garbage) as output.
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
Give a general description of the input validation process.
When input is given to a program, it should be inspected before it is processed. If the input is invalid, then it should be discarded and the user should be prompted to enter the correct data.
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
Describe the steps that are generally taken when an input validation loop is used to validate data.
The input is read, and then a pretest loop is executed. If the input data is invalid, the body of the loop executes. In the body of the loop, an error message is displayed so the user will know that the input was invalid, and then the input read again. The loop repeats as long as the input is invalid.
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What is a priming read? What is its purpose?
It is the input operation that takes place just before an input validation loop. The purpose of the priming read is to get the first input value.
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
If the input that is read by the priming read is valid, how many times will the input validation loop iterate?
None
Ch4 The whileloop,p.122The for loop, p.130The Bug Collector Problem, p.161
What is a function?
A function is a group of statements that exist within a program for the purpose of performing a specific task.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What is meant by the phrase “divide and conquer?”
A large task is divided into several smaller tasks that are easily performed.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
How do functions help you reuse code in a program?
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.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
How can functions make the development of multiple programs faster?
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.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
How can functions make it easier for programs to be developed by teams of programmers?
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.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
A function definition has what two parts?
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.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What does the phrase “calling a function” mean?
To call a function means to execute the function.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
When a function is executing, what happens when the end of the function’s block is reached?
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.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
Why must you indent the statements in a block?
Because the Python interpreter uses the indentation to determine where a block begins and ends
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What is a local variable? How is access to a local variable restricted?
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.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What is a variable’s scope?
The part of a program in which a variable may be accessed
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
Is it permissible for a local variable in one function to have the same name as a local variable in a different function?
Yes, it is permissible.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What are the pieces of data that are passed into a function called?
Arguments
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What are the variables that receive pieces of data in a function called?
Parameters
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What is a parameter variable’s scope?
A parameter variable’s scope is the entire function in which the parameter is declared.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
When a parameter is changed, does this affect the argument that was passed into the parameter?
No, it does not.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
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. passes by keyword argument b. passes by position
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What is the scope of a global variable?
The entire program
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
Give one good reason that you should not use global variables in a program.
Here are three: • Global variables make debugging difficult. Any statement in a program can change the value of a global variable. If you find that the wrong value is being stored in a global variable, you have to track down every statement that accesses it to determine where the bad value is coming from. In a program with thousands of lines of code, this can be difficult. • Functions that use global variables are usually dependent on those variables. If you want to use such a function in a different program, you will most likely have to redesign it so it does not rely on the global variable. • Global variables make a program hard to understand. A global variable can be modified by any statement in the program. If you are to understand any part of the program that uses a global variable, you have to be aware of all the other parts of the program that access the global variable.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What is a global constant? Is it permissible to use global constants in a program?
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.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
How does a value-returning function differ from the simple functions we discussed in Chapter 3?
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.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What is a library function?
A prewritten function that performs some commonly needed task
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
Why are library functions like “black boxes”?
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.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What does the following statement do? x = random.randint(1, 100)
It assigns a random integer in the range of 1 through 100 to the variable x .
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What does the following statement do? print(random.randint(1, 20))
It prints a random integer in the range of 1 through 20.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What does the following statement do? print(random.randrange(10, 20))
It prints a random integer in the range of 10 through 19.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What does the following statement do? print(random.random())
It prints a random floating-point number in the range of 0.0 up to, but not including, 1.0.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What does the following statement do? print(random.uniform(0.1, 0.5))
It prints a random floating-point number in the range of 0.1 through 0.5.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
When the random module is imported, what does it use as a seed value for random number generation?
It uses the system time, retrieved from the computer’s internal clock.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What happens if the same seed value is always used for generating random numbers?
If the same seed value were always used, the random number functions would always generate the same series of pseudorandom numbers.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What is the purpose of the return statement in a function?
It returns a value back to the part of the program that called it.
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
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? print(do_something(10))
a) do_something b) It returns a value that is twice the argument passed to it. c) 20
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What is a Boolean function?
A function that returns either True or False
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What import statement do you need to write in a program that uses the math module.
import math
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
Write a statement that uses a math module function to get the square root of 100 and assigns it to a variable.
square_root = math.sqrt(100)
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
Write a statement that uses a math module function to convert 45 degrees to radians and assigns the value to a variable.
angle = math.radians(45)
Ch5 Defining and calling a function, p.168Passing Arguments to a function, p.181writing a Value-returning Function, p.206The Kilometer Converter Problem, p.229The Feet to Inches Problems, p.230
What is an output file?
A file to which a program writes data. It is called an output file because the program sends output to it.
Ch6 Using Loops to Process Files, p.252File Display, p.288
What is an input file?
A file from which a program reads data. It is called an input file because the program receives input from it.
Ch6 Using Loops to Process Files, p.252File Display, p.288
What three steps must be taken by a program when it uses a file?
(1) Open the file. (2) Process the file. (3) Close the file.
Ch6 Using Loops to Process Files, p.252File Display, p.288
In general, what are the two types of files? What is the difference between these two types of files?
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.
Ch6 Using Loops to Process Files, p.252File Display, p.288
What are the two types of file access? What is the difference between these two?
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.
Ch6 Using Loops to Process Files, p.252File Display, p.288
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?
The file’s name on the disk and the name of a variable that references a file object.
Ch6 Using Loops to Process Files, p.252File Display, p.288
If a file already exists what happens to it if you try to open it as an output file (using the 'w' mode)?
The file’s contents are erased.
Ch6 Using Loops to Process Files, p.252File Display, p.288
What is the purpose of opening a file?
Opening a file creates a connection between the file and the program. It also creates an association between the file and a file object.
Ch6 Using Loops to Process Files, p.252File Display, p.288
What is the purpose of closing a file?
Closing a file disconnects the program from the file.
Ch6 Using Loops to Process Files, p.252File Display, p.288
What is a file’s read position? Initially, where is the read position when an input file is opened?
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.
Ch6 Using Loops to Process Files, p.252File Display, p.288
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?
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.
Ch6 Using Loops to Process Files, p.252File Display, p.288
Write a short program that uses a for loop to write the numbers 1 through 10 to a file.
outfile = open('numbers.txt', 'w') for num in range(1, 11): outfile.write(str(num) + '\n') outfile.close()
Ch6 Using Loops to Process Files, p.252File Display, p.288
What does it mean when the readline method returns an empty string?
The readline method returns an empty string ( '' ) when it has attempted to read beyond the end of a file.
Ch6 Using Loops to Process Files, p.252File Display, p.288
Assume that 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.
infile = open('numbers.txt', 'r') line = infile.readline() while line != '': print(line) line = infile.readline() infile.close()
Ch6 Using Loops to Process Files, p.252File Display, p.288
Revise the program that you wrote for Checkpoint 6.14 to use the for loop instead of the while loop.
infile = open('data.txt', 'r') for line in infile: print(line) infile.close()
Ch6 Using Loops to Process Files, p.252File Display, p.288
What is a record? What is a field?
A record is a complete set of data that describes one item, and a field is a single piece of data within a record.
Ch6 Using Loops to Process Files, p.252File Display, p.288
Describe the way that you use a temporary file in a program that modifies a record in a sequential access file.
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.
Ch6 Using Loops to Process Files, p.252File Display, p.288
Describe the way that you use a temporary file in a program that deletes a record from a sequential file.
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.
Ch6 Using Loops to Process Files, p.252File Display, p.288
Briefly describe what an exception is.
An exception is an error that occurs while a program is running. In most cases, an exception causes a program to abruptly halt.
Ch6 Using Loops to Process Files, p.252File Display, p.288
If an exception is raised and the program does not handle it with a try/except statement, what happens?
The program halts.
Ch6 Using Loops to Process Files, p.252File Display, p.288
What type of exception does a program raise when it tries to open a nonexistent file?
IOError
Ch6 Using Loops to Process Files, p.252File Display, p.288
What type of exception does a program raise when it uses the float function to convert a non-numeric string to a number?
ValueError
Ch6 Using Loops to Process Files, p.252File Display, p.288
What will the following code display? numbers = [1, 2, 3, 4, 5] numbers[2] = 99 print(numbers)
[1, 2, 99, 4, 5]
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? numbers = list(range(3)) print(numbers)
[0, 1, 2]
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? numbers = [10] * 5 print(numbers)
[10, 10, 10, 10, 10]
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? numbers = list(range(1, 10, 2))for n in numbers: print(n)
1 3 5 7 9
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? numbers = [1, 2, 3, 4, 5] print(numbers[-2])
4
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
How do you find the number of elements in a list?
Use the built-in len function.
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? numbers1 = [1, 2, 3] numbers2 = [10, 20, 30] numbers3 = numbers1 + numbers2 print(numbers1) print(numbers2) print(numbers3)
[1, 2, 3] [10, 20, 30] [1, 2, 3, 10, 20, 30]
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? numbers1 = [1, 2, 3] numbers2 = [10, 20, 30] numbers2 += numbers1 print(numbers1) print(numbers2)
[1, 2, 3] [10, 20, 30, 1, 2, 3]
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[1:3] print(my_list)
[2, 3]
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[1:] print(my_list)
[2, 3]
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[:1] print(my_list)
[1]
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[:] print(my_list)
[1, 2, 3, 4, 5]
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[-3:] print(my_list)
[3, 4, 5]
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What will the following code display? names = ['Jim', 'Jill', 'John', 'Jasmine'] if 'Jasmine' not in names: print('Cannot find Jasmine.') else: print("Jasmine's family:") print(names)
Jasmine's family: ['Jim', 'Jill', 'John', 'Jasmine']
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What is the difference between calling a list’s remove method and using the del statement to remove an element?
The remove method searches for and removes an element containing a specific value. The del statement removes an element at a specific index.
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
How do you find the lowest and highest values in a list?
You can use the built-in min and max functions.
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
Assume the following statement appears in a program: names = [] Which of the following statements would you use to add the string ‘Wendy’ to the list at index 0? Why would you select this statement instead of the other? a. names[0] = 'Wendy' b. names.append('Wendy')
You would use statement b, names.append( ' Wendy ' ) . This is because element 0 does not exist. If you try to use statement a, an error will occur.
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
Describe the following list methods: a. index b. insert c. sort d. reverse
a) The index method searches for an item in the list and returns the index of the first element containing that item. b) The insert method inserts an item into the list at a specified index. c) The sort method sorts the items in the list to appear in ascending order. d) The reverse method reverses the order of the items in the list.
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
Look at the following interactive session, in which a two-dimensional list is created. How many rows and how many columns are in the list? numbers = [[1, 2], [10, 20], [100, 200], [1000, 2000]]
The list contains 4 rows and 2 columns.
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
Write a statement that creates a two-dimensional list with three rows and four columns. Each element should be assigned the value 0.
mylist = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
Write a set of nested loops that display the contents of the numbers list shown in Checkpoint question 7.19.
for r in range(4): for c in range(2): print(numbers[r][c])
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
What is the primary difference between a list and a tuple?
The primary difference between tuples and lists is that tuples are immutable. That means that once a tuple is created, it cannot be changed.
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
Give two reasons why tuples exist.
Here are three reasons: • Processing a tuple is faster than processing a list, so tuples are good choices when you are processing lots of data and that data will not be modified. • Tuples are safe. Because you are not allowed to change the contents of a tuple, you can store data in one and rest assured that it will not be modified (accidentally or otherwise) by any code in your program. • There are certain operations in Python that require the use of a tuple.
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
Assume that my_list references a list. Write a statement that converts it to a tuple.
my_tuple = tuple(my_list)
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
Assume that my_tuple references a tuple. Write a statement that converts it to a list
my_list = list(my_tuple)
Ch7 List Slicing, p.299The Lottery Number Generator Problem, p.334
Assume the variable name references a string. Write a for loop that prints each character in the string.
for letter in name: print(letter)
Ch8 The Vowels and Consonants Problem, p.367
What is the index of the first character in a string?
0
Ch8 The Vowels and Consonants Problem, p.368
If a string has 10 characters, what is the index of the last character?
9
Ch8 The Vowels and Consonants Problem, p.369
What happens if you try to use an invalid index to access a character in a string?
An IndexError exception will occur if you try to use an index that is out of range for a particular string.
Ch8 The Vowels and Consonants Problem, p.370
How do you find the length of a string?
Use the built-in len function.
Ch8 The Vowels and Consonants Problem, p.371
What is wrong with the following code? animal = 'Tiger' animal[0] = 'L'
The second statement attempts to assign a value to an individual character in the string. Strings are immutable, however, so the expression animal[0] cannot appear on the left side of an assignment operator.
Ch8 The Vowels and Consonants Problem, p.372
What will the following code display? mystring = 'abcdefg' print(mystring[2:5])
cde
Ch8 The Vowels and Consonants Problem, p.373
What will the following code display? mystring = 'abcdefg' print(mystring[3:])
defg
Ch8 The Vowels and Consonants Problem, p.374
What will the following code display? mystring = 'abcdefg' print(mystring[:3])
abc
Ch8 The Vowels and Consonants Problem, p.375
What will the following code display? mystring = 'abcdefg' print(mystring[:])
abcdefg
Ch8 The Vowels and Consonants Problem, p.376
Write code using the in operator that determines whether 'd' is in mystring.
if 'd' in mystring: print('Yes, it is there.')
Ch8 The Vowels and Consonants Problem, p.377
Assume the variable big references a string. Write a statement that converts the string it references to lowercase, and assigns the converted string to the variable little.
little = big.upper()
Ch8 The Vowels and Consonants Problem, p.378
Write an if statement that displays “Digit” if the string referenced by the variable ch contains a numeric digit. Otherwise, it should display “No digit.”
if ch.isdigit(): print('Digit') else: print('No digit')
Ch8 The Vowels and Consonants Problem, p.379
What is the output of the following code? ch = 'a' ch2 = ch.upper() print(ch, ch2)
a A
Ch8 The Vowels and Consonants Problem, p.380
Write a loop that asks the user “Do you want to repeat the program or quit? (R/Q)”. The loop should repeat until the user has entered an R or Q (either uppercase or lowercase).
again = input('Do you want to repeat ' + \ 'the program or quit? (R/Q) ') while again.upper() != 'R' and again.upper() != 'Q': again = input('Do you want to repeat the ' + 'program or quit? (R/Q) ')
Ch8 The Vowels and Consonants Problem, p.381
What will the following code display? var = '$' print(var.upper())
$
Ch8 The Vowels and Consonants Problem, p.382
Write a loop that counts the number of uppercase characters that appear in the string referenced by the variable mystring.
for letter in mystring: if letter.isupper(): count += 1
Ch8 The Vowels and Consonants Problem, p.383
Assume the following statement appears in a program: days = 'Monday Tuesday Wednesday' Write a statement that splits the string, creating the following list: ['Monday', 'Tuesday', 'Wednesday']
my_list = days.split()
Ch8 The Vowels and Consonants Problem, p.384
Assume the following statement appears in a program: values = 'one$two$three$four' Write a statement that splits the string, creating the following list: ['one', 'two', 'three', 'four']
my_list = values.split('$')
Ch8 The Vowels and Consonants Problem, p.385
An element in a dictionary has two parts. What are they called?
Key and value
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
Which part of a dictionary element must be immutable?
The key
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
Suppose 'start' : 1472 is an element in a dictionary. What is the key? What is the value?
The string ' start ' is the key, and the integer 1472 is the value.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
Suppose a dictionary named employee has been created. What does the following statement do? employee['id'] = 54321
It stores the key-value pair 'id' : 54321 in the employee dictionary.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What will the following code display? stuff = {1 : 'aaa', 2 : 'bbb', 3 : 'ccc'} print(stuff[3])
ccc
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
How can you determine whether a key-value pair exists in a dictionary?
You can use the in operator to test for a specific key.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
Suppose a dictionary named inventory exists. What does the following statement do? del inventory[654]
It deletes the element that has the key 654.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What will the following code display?stuff = {1: 'aaa', 2: 'bbb', 3: 'ccc'}print(len(stuff))
3
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What will the following code display?stuff = {1: 'aaa', 2: 'bbb', 3: 'ccc'}for k in stuff: print(k)
123
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What is the difference between the dictionary methods pop and popitem?
The pop method accepts a key as an argument, returns the value that is associated with that key, and removes that key-value pair from the dictionary. The popitem method returns a randomly selected key-value pair, as a tuple, and removes that key-value pair from the dictionary.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What does the items method return?
It returns all a dictionary’s keys and their associated values as a sequence of tuples.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What does the keys method return?
It returns all the keys in a dictionary as a sequence of tuples.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What does the values method return?
It returns all the values in the dictionary as a sequence of tuples.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
Are the elements of a set ordered or unordered?
Unordered
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
Does a set allow you to store duplicate elements?
No
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
How do you create an empty set?
You call the built-in set function.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following statement executes, what elements will be stored in the myset set? myset = set('Jupiter')
The set will contain these elements (in no particular order): 'J' , 'u' , 'p' , 'i' , 't' , 'e' , and 'r' .
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following statement executes, what elements will be stored in the myset set? myset = set(25)
The set will contain one element: 25.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following statement executes, what elements will be stored in the myset set? myset = set('www xxx yyy zzz')
The set will contain these elements (in no particular order): 'w' , ' ' , 'x' , 'y' , and 'z' .
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following statement executes, what elements will be stored in the myset set? myset = set([1, 2, 2, 3, 4, 4, 4])
The set will contain these elements (in no particular order): 1, 2, 3, and 4.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following statement executes, what elements will be stored in the myset set? myset = set(['www', 'xxx', 'yyy', 'zzz'])
The set will contain these elements (in no particular order): 'www' , 'xxx' , 'yyy' , and 'zzz' .
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
How do you determine the number of elements in a set?
You pass the set as an argument to the len function.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following statement executes, what elements will be stored in the myset set? myset = set([10, 9, 8]) myset.update([1, 2, 3])
The set will contain these elements (in no particular order): 10, 9, 8, 1, 2, and 3.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following statement executes, what elements will be stored in the myset set? myset = set([10, 9, 8]) myset.update('abc')
The set will contain these elements (in no particular order): 10, 9, 8, 'a' , 'b' , and 'c' .
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What is the difference between the remove and discard methods?
If the specified element to delete is not in the set, the remove method raises a KeyError exception, but the discard method does not raise an exception.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
How can you determine whether a specific element exists in a set?
You can use the in operator to test for the element.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following code executes, what elements will be members of set3? set1 = set([10, 20, 30]) set2 = set([100, 200, 300]) set3 = set1.union(set2)
{10, 20, 30, 100, 200, 300}
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following code executes, what elements will be members of set3? set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set1.intersection(set2)
{3, 4}
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following code executes, what elements will be members of set3? set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set1.difference(set2)
{1, 2}
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following code executes, what elements will be members of set3? set1 = set([1, 2, 3, 4]) set2 = set([3, 4, 5, 6]) set3 = set2.difference(set1)
{5, 6}
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
After the following code executes, what elements will be members of set3? set1 = set(['a', 'b', 'c']) set2 = set(['b', 'c', 'd']) set3 = set1.symmetric_difference(set2)
{'a', 'd'}
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
Look at the following code: set1 = set([1, 2, 3, 4]) set2 = set([2, 3]) Which of the sets is a subset of the other? Which of the sets is a superset of the other?
set2 is a subset of set1 , and set1 is a superset of set2 .
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What is object serialization?
The process of converting the object to a stream of bytes that can be saved to file for later retrieval.
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
When you open a file for the purpose of saving a pickled object to it, what file access mode do you use?
wb'
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
When you open a file for the purpose of retrieving a pickled object from it, what file access mode do you use?
rb'
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What module do you import if you want to pickle objects?
The pickle module
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What function do you call to pickle an object?
pickle.dump
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What function do you call to retrieve and unpickle an object?
pickle.load
Ch9 Introduction to Dictionaries, p.369Introduction to Sets,p.392The CapitalQuiz Problem, p.416
What is an object?
An object is a software entity that contains both data and procedures.
Ch10 Classes and Objects, p.423The Pet class, p.476
What is encapsulation?
Encapsulation is the combining of data and code into a single object.
Ch10 Classes and Objects, p.423The Pet class, p.476
Why is an object’s internal data usually hidden from outside code?
When an object’s internal data is hidden from outside code and access to that data is restricted to the object’s methods, the data is protected from accidental corruption. In addition, the programming code outside the object does not need to know about the format or internal structure of the object’s data.
Ch10 Classes and Objects, p.423The Pet class, p.476
What are public methods? What are private methods?
Public methods can be accessed by entities outside the object. Private methods cannot be accessed by entities outside the object. They are designed to be accessed internally.
Ch10 Classes and Objects, p.423The Pet class, p.476
You hear someone make the following comment: “A blueprint is a design for a house. A carpenter can use the blueprint to build the house. If the carpenter wishes, he or she can build several identical houses from the same blueprint.” Think of this as a metaphor for classes and objects. Does the blueprint represent a class, or does it represent an object?
The metaphor of a blueprint represents a class.
Ch10 Classes and Objects, p.423The Pet class, p.476
In this chapter, we use the metaphor of a cookie cutter and cookies that are made from the cookie cutter to describe classes and objects. In this metaphor, are objects the cookie cutter, or the cookies?
Objects are the cookies.
Ch10 Classes and Objects, p.423The Pet class, p.476
What is the purpose of the _ _init_ _ method? When does it execute?
Its purpose is to initialize an object’s data attributes. It executes immediately after the object is created.
Ch10 Classes and Objects, p.423The Pet class, p.476
What is the purpose of the self parameter in a method?
When a method executes, it must have a way of knowing which object’s data attributes it is supposed to operate on. That’s where the self parameter comes in. When a method is called, Python automatically makes its self parameter reference the specific object that the method is supposed to operate on.
Ch10 Classes and Objects, p.423The Pet class, p.476
In a Python class, how do you hide an attribute from code outside the class?
By starting the attribute’s name with two underscores
Ch10 Classes and Objects, p.423The Pet class, p.476
What is the purpose of the __str__ method?
It returns a string representation of the object.
Ch10 Classes and Objects, p.423The Pet class, p.476
How do you call the _ _str_ _ method?
By passing the object to the built-in str method
Ch10 Classes and Objects, p.423The Pet class, p.476
What is an instance attribute?
An attribute that belongs to a specific instance of a class
Ch10 Classes and Objects, p.423The Pet class, p.476
A program creates 10 instances of the Coin class. How many __sideup attributes exist in memory?
10
Ch10 Classes and Objects, p.423The Pet class, p.476
What is an accessor method? What is a mutator method?
A method that returns a value from a class’s attribute but does not change it is known as an accessor method. A method that stores a value in a data attribute or changes the value of a data attribute in some other way is known as a mutator method.
Ch10 Classes and Objects, p.423The Pet class, p.476
The typical UML diagram for a class has three sections. What appears in these three sections?
The top section is where you write the name of the class. The middle section holds a list of the class’s fields. The bottom section holds a list of the class’s methods.
Ch10 Classes and Objects, p.423The Pet class, p.476
What is a problem domain?
A written description of the real-world objects, parties, and major events related to the problem
Ch10 Classes and Objects, p.423The Pet class, p.476
When designing an object-oriented application, who should write a description of the problem domain?
If you adequately understand the nature of the problem you are trying to solve, you can write a description of the problem domain yourself. If you do not thoroughly understand the nature of the problem, you should have an expert write the description for you.
Ch10 Classes and Objects, p.423The Pet class, p.476
How do you identify the potential classes in a problem domain description?
First, identify the nouns, pronouns, and pronoun phrases in the problem domain description. Then, refine the list to eliminate duplicates, items that you do not need to be concerned with in the problem, items that represent objects instead of classes, and items that represent simple values that can be stored in variables.
Ch10 Classes and Objects, p.423The Pet class, p.476
What are a class’s responsibilities?
The things that the class is responsible for knowing and the actions that the class is responsible for doing
Ch10 Classes and Objects, p.423The Pet class, p.476
What two questions should you ask to determine a class’s responsibilities?
In the context of this problem, what must the class know? What must the class do?
Ch10 Classes and Objects, p.423The Pet class, p.476
Will all of a classes actions always be directly mentioned in the problem domain description?
No, not always
Ch10 Classes and Objects, p.423The Pet class, p.476
In this section we discussed superclasses and subclasses. Which is the general class and which is the specialized class?
A superclass is a general class, and a subclass is a specialized class.
Ch11 The Person and Customer Classes, p.505
What does it mean to say there is an “is a” relationship between two objects?
When one object is a specialized version of another object, there is an “is a” relationship between them. The specialized object “is a” version of the general object.
Ch11 The Person and Customer Classes, p.506
What does a subclass inherit from its superclass?
It inherits all the superclass’s attributes.
Ch11 The Person and Customer Classes, p.507
Look at the following code, which is the first line of a class definition. What is the name of the superclass? What is the name of the subclass? class Canary(Bird):
Bird is the superclass, and Canary is the subclass.
Ch11 The Person and Customer Classes, p.508
Look at the following class definitions: class Vegetable: def _ _init_ _(self, vegtype): self._ _vegtype = vegtype def message(self): print("I'm a vegetable.") class Potato(Vegetable): def _ _init_ _(self): Vegetable._ _init_ _(self, 'potato') def message(self): print("I'm a potato.") Given these class definitions, what will the following statements display? v = Vegetable('veggie') p = Potato() v.message() p.message()
I’m a vegetable. I’m a potato.
Ch11 The Person and Customer Classes, p.509
It is said that a recursive algorithm has more overhead than an iterative algorithm. What does this mean?
A recursive algorithm requires multiple method calls. Each method call requires several actions to be performed by the JVM. These actions include allocating memory for parameters and local variables and storing the address of the program location where control returns after the method terminates. All these actions are known as overhead. In an iterative algorithm, which uses a loop, such overhead is unnecessary.
Ch12 The Recursive Multiplication Problem, p.524
What is a base case?
A case in which the problem can be solved without recursion
Ch12 The Recursive Multiplication Problem, p.525
What is a recursive case?
Cases in which the problem is solved using recursion
Ch12 The Recursive Multiplication Problem, p.526
What causes a recursive algorithm to stop calling itself?
When it reaches the base case
Ch12 The Recursive Multiplication Problem, p.527
What is direct recursion? What is indirect recursion?
In direct recursion, a recursive method calls itself. In indirect recursion, method A calls method B, which in turn calls method A.
Ch12 The Recursive Multiplication Problem, p.528
What is a user interface?
The part of a computer and its operating system with which the user interacts
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
How does a command line interface work?
A command line interface typically displays a prompt, and the user types a command, which is then executed.
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
When the user runs a program in a text-based environment, such as the command line, what determines the order in which things happen?
The program
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
What is an event-driven program?
A program that responds to events that take place, such as the user clicking a button
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
Briefly describe each of the following tkinter widgets: a) Label b) Entry c) Button d) Frame
a) Label—An area that displays one line of text or an image b) Entry—An area in which the user may type a single line of input from the keyboard c) Button—A button that can cause an action to occur when it is clicked d) Frame—A container that can hold other widgets
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
How do you create a root widget?
You create an instance of the tkinter module’s Tk class.
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
What does the tkinter module’s mainloop function do?
This function runs like an infinite loop until you close the main window.
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
What does a widget’s pack method do?
The pack method arranges a widget in its proper position, and it makes the widget visible when the main window is displayed.
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
If you create two Label widgets and call their pack methods with no arguments, how will the Label widgets be arranged inside their parent widget?
One will be stacked on top of the other.
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
What argument would you pass to a widget’s pack method to specify that it should be positioned as far left as possible inside the parent widget?
side=–left—
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
How do you retrieve data from an Entry widget?
You use an Entry widget’s get method to retrieve the data that the user has typed into the widget.
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
When you retrieve a value from an Entry widget, of what data type is it?
It is a string.
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
What module is the StringVar class in?
tkinter
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
What can you accomplish by associating a StringVar object with a Label widget?
Any value that is stored in the StringVar object will automatically be displayed in the Label widget.
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
You want the user to be able to select only one item from a group of items. Which type of component would you use for the items, radio buttons or check boxes?
You would use radio buttons.
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
You want the user to be able to select any number of items from a group of items. Which type of component would you use for the items, radio buttons or check boxes?
You would use check buttons.
Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
How can you use an IntVar object to determine which Radiobutton has been selected in a group of Radiobuttons?

When you create a group of Radiobuttons, you associate them all with the same IntVar object. You also assign a unique integer value to each Radiobutton widget. When one of the Radiobutton widgets is selected, it stores its unique integer value in the IntVar object.

Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562
How can you use an IntVar object to determine whether a Checkbutton has been selected?

You associate a different IntVar object with each Checkbutton. When a Checkbutton is selected, its associated IntVar object will hold the value 1. When a Checkbutton is deselected, its associated IntVar object will hold the value 0.

Ch13 Creating a simple GUI Application, p.532Responding to Button Clicks, p.538The Name and Address Problem, p.562