SolutionNLandNLAnswerNLGuideNLForNLAllNLChapters:NLLambert,NLFundamentalsNLofNLPython:NLFirstNLPrograms,NL3e,NLCY24,NL9780357881019;NLChapterNL1
,NLIntroduction
,SolutionNLandNLAnswerNLGuideNLForNLAllNLChapters:NLLambert,NLFundamentalsNLofNLPython:NLFirstNLPrograms,NL3e,NLCY24,NL9780357881019;NLChapterNL1
,NLIntroduction
TABLE OF CONTENTS NL NL
Exercise Solutions ....................................................................................................................................... 1
NL
Exercise 1.1 .............................................................................................................................................. 1
NL
Exercise 1.2 .............................................................................................................................................. 2
NL
Exercise 1.3 .............................................................................................................................................. 3
NL
Review Questions Answers ....................................................................................................................... 4
NL NL
Programming Exercises Solutions............................................................................................................ 8
NL NL
Debugging Exercises Solutions ................................................................................................................. 8
NL NL
EXERCISE SOLUTIONS NL
EXERCISE 1.1 NL
1. List three common types of computing agents.
NL NL NL NL NL NL
Solution:
Human beings, desktop computers, cell phones
NL NL NL NL NL
2. Write an algorithm that describes the second part of the process of making change (counting out
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
the coins and bills).
NL NL NL NL
Solution:
There are various ways to do this, but here is one:
NL NL NL NL NL NL NL NL NL NL
Repeat
Select the largest unit of money that is less than or equal to the remainin
NL NL NL NL NL NL NL NL NL NL NL NL NL NL
g change Subtract this unit from the remaining change
NL NL NL NL NL NL NL NL
Until the remaining change is 0
NL NL NL NL NL
The collection of units selected represent the change
NL NL NL NL NL NL NL
3. Write an algorithm that describes a common task, such as baking a cake.
NL NL NL NL NL NL NL NL NL NL NL NL
,SolutionNLandNLAnswerNLGuideNLForNLAllNLChapters:NLLambert,NLFundamentalsNLofNLPython:NLFirstNLPrograms,NL3e,NLCY24,NL9780357881019;NLChapterNL1
,NLIntroduction
Solution:
There are various ways to do this, but here is one:
NL NL NL NL NL NL NL NL NL NL
Preheat an oven to 375 degrees
NL NL NL NL NL
Add 1 cup of water and 1 egg to a mixing bowl
NL NL NL NL NL NL NL NL NL NL NL
Beat the liquid mixture in the bowl until the ingredients are blen
NL NL NL NL NL NL NL NL NL NL NL
ded Add the contents of a boxed cake mix to the mixing bo
NL NL NL NL NL NL NL NL NL NL NL NL
wl
Beat the mixture in the bowl until the ingredients are blended
NL NL NL NL NL NL NL NL NL NL
Pour the contents of the mixing bowl into a lightly greased cak
NL NL NL NL NL NL NL NL NL NL NL
e pan Bake the cake in the oven for 45 minutes
NL NL NL NL NL NL NL NL NL NL
4. Describe an instruction that is not well defined and thus could not be included as a step in a
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
n algorithm. Give an example of such an instruction.
NL NL NL NL NL NL NL NL
Solution:
Attempting to divide a number by 0
NL NL NL NL NL NL
5. In what sense is a laptop computer a general-purpose problem-solving machine?
NL NL NL NL NL NL NL NL NL NL
Solution:
A laptop computer is a general-purpose problem-
NL NL NL NL NL NL
solving machine because it is programmable and can solve any problem for which there is an al
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
gorithm.
6. List four devices that use computers and describe the information that they process. (Hint: Thi
NL NL NL NL NL NL NL NL NL NL NL NL NL NL
nk of the inputs and outputs of the devices.)
NL NL NL NL NL NL NL NL
Solution:
Digital camera—images, music player—sound, cell phone—text, ATM—numbers
NL NL NL NL NL NL
EXERCISE 1.2 NL
1. List two examples of input devices and two examples of output devices.
NL NL NL NL NL NL NL NL NL NL NL
Solution:
Input devices—keyboard and mouse, output devices—monitor and speakers
NL NL NL NL NL NL NL
2. What does the central processing unit (CPU) do?
NL NL NL NL NL NL NL
Solution:
The CPU fetches, decodes, and executes instructions.
NL NL NL NL NL NL
3. How is information represented in hardware memory?
NL NL NL NL NL NL
Solution:
Information is represented using binary notation, which in hardware is a pattern of voltage levels.
NL NL NL NL NL NL NL NL NL NL NL NL NL NL
, SolutionNLandNLAnswerNLGuideNLForNLAllNLChapters:NLLambert,NLFundamentalsNLofNLPython:NLFirstNLPrograms,NL3e,NLCY24,NL9780357881019;NLChapterNL1
,NLIntroduction
4. What is the difference between a terminal-based interface and a graphical user interface?
NL NL NL NL NL NL NL NL NL NL NL NL
Solution:
A terminal-
NL
based interface supports only the input and output of text with a keyboard and monitor. A graph
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
ical user interface supports the output of images and the manipulation of them with a pointing dev
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
ice, the mouse.
NL NL
5. What role do translators play in the programming process?
NL NL NL NL NL NL NL NL
Solution:
A translator converts a program written in a high-
NL NL NL NL NL NL NL NL
level language (human readable and writable) to an equivalent program in a low-
NL NL NL NL NL NL NL NL NL NL NL NL
level language (machine readable and executable).
NL NL NL NL NL
EXERCISE 1.3 NL
1. Describe what happens when the programmer enters the string "Greetings!" in the Python shell.
NL NL NL NL NL NL NL NL NL NL NL NL NL
Solution:
Python reads the string "Greetings!", evaluates it, and displays this string (including single
NL NL NL NL NL NL NL NL NL NL NL NL NL
quotes) in the shell.
NL NL NL
2. Write a line of code that prompts the user for their name and saves the user’s input in a variable call
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
ed
name.
Solution:
name= input("Enter your name: ")
L
N NL NL NL NL
3. What is a Python script?
NL NL NL NL
Solution:
A Python script is a complete Python program that can be run from a computer’s operating system.
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
4. Explain what goes on behind the scenes when your computer runs a Python program.
NL NL NL NL NL NL NL NL NL NL NL NL NL
Solution:
If the program has not already been translated, Python’s compiler translates it to byte code. The
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
Python virtual machine then executes this code.
NL NL NL NL NL NL
,NLIntroduction
,SolutionNLandNLAnswerNLGuideNLForNLAllNLChapters:NLLambert,NLFundamentalsNLofNLPython:NLFirstNLPrograms,NL3e,NLCY24,NL9780357881019;NLChapterNL1
,NLIntroduction
TABLE OF CONTENTS NL NL
Exercise Solutions ....................................................................................................................................... 1
NL
Exercise 1.1 .............................................................................................................................................. 1
NL
Exercise 1.2 .............................................................................................................................................. 2
NL
Exercise 1.3 .............................................................................................................................................. 3
NL
Review Questions Answers ....................................................................................................................... 4
NL NL
Programming Exercises Solutions............................................................................................................ 8
NL NL
Debugging Exercises Solutions ................................................................................................................. 8
NL NL
EXERCISE SOLUTIONS NL
EXERCISE 1.1 NL
1. List three common types of computing agents.
NL NL NL NL NL NL
Solution:
Human beings, desktop computers, cell phones
NL NL NL NL NL
2. Write an algorithm that describes the second part of the process of making change (counting out
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
the coins and bills).
NL NL NL NL
Solution:
There are various ways to do this, but here is one:
NL NL NL NL NL NL NL NL NL NL
Repeat
Select the largest unit of money that is less than or equal to the remainin
NL NL NL NL NL NL NL NL NL NL NL NL NL NL
g change Subtract this unit from the remaining change
NL NL NL NL NL NL NL NL
Until the remaining change is 0
NL NL NL NL NL
The collection of units selected represent the change
NL NL NL NL NL NL NL
3. Write an algorithm that describes a common task, such as baking a cake.
NL NL NL NL NL NL NL NL NL NL NL NL
,SolutionNLandNLAnswerNLGuideNLForNLAllNLChapters:NLLambert,NLFundamentalsNLofNLPython:NLFirstNLPrograms,NL3e,NLCY24,NL9780357881019;NLChapterNL1
,NLIntroduction
Solution:
There are various ways to do this, but here is one:
NL NL NL NL NL NL NL NL NL NL
Preheat an oven to 375 degrees
NL NL NL NL NL
Add 1 cup of water and 1 egg to a mixing bowl
NL NL NL NL NL NL NL NL NL NL NL
Beat the liquid mixture in the bowl until the ingredients are blen
NL NL NL NL NL NL NL NL NL NL NL
ded Add the contents of a boxed cake mix to the mixing bo
NL NL NL NL NL NL NL NL NL NL NL NL
wl
Beat the mixture in the bowl until the ingredients are blended
NL NL NL NL NL NL NL NL NL NL
Pour the contents of the mixing bowl into a lightly greased cak
NL NL NL NL NL NL NL NL NL NL NL
e pan Bake the cake in the oven for 45 minutes
NL NL NL NL NL NL NL NL NL NL
4. Describe an instruction that is not well defined and thus could not be included as a step in a
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
n algorithm. Give an example of such an instruction.
NL NL NL NL NL NL NL NL
Solution:
Attempting to divide a number by 0
NL NL NL NL NL NL
5. In what sense is a laptop computer a general-purpose problem-solving machine?
NL NL NL NL NL NL NL NL NL NL
Solution:
A laptop computer is a general-purpose problem-
NL NL NL NL NL NL
solving machine because it is programmable and can solve any problem for which there is an al
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
gorithm.
6. List four devices that use computers and describe the information that they process. (Hint: Thi
NL NL NL NL NL NL NL NL NL NL NL NL NL NL
nk of the inputs and outputs of the devices.)
NL NL NL NL NL NL NL NL
Solution:
Digital camera—images, music player—sound, cell phone—text, ATM—numbers
NL NL NL NL NL NL
EXERCISE 1.2 NL
1. List two examples of input devices and two examples of output devices.
NL NL NL NL NL NL NL NL NL NL NL
Solution:
Input devices—keyboard and mouse, output devices—monitor and speakers
NL NL NL NL NL NL NL
2. What does the central processing unit (CPU) do?
NL NL NL NL NL NL NL
Solution:
The CPU fetches, decodes, and executes instructions.
NL NL NL NL NL NL
3. How is information represented in hardware memory?
NL NL NL NL NL NL
Solution:
Information is represented using binary notation, which in hardware is a pattern of voltage levels.
NL NL NL NL NL NL NL NL NL NL NL NL NL NL
, SolutionNLandNLAnswerNLGuideNLForNLAllNLChapters:NLLambert,NLFundamentalsNLofNLPython:NLFirstNLPrograms,NL3e,NLCY24,NL9780357881019;NLChapterNL1
,NLIntroduction
4. What is the difference between a terminal-based interface and a graphical user interface?
NL NL NL NL NL NL NL NL NL NL NL NL
Solution:
A terminal-
NL
based interface supports only the input and output of text with a keyboard and monitor. A graph
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
ical user interface supports the output of images and the manipulation of them with a pointing dev
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
ice, the mouse.
NL NL
5. What role do translators play in the programming process?
NL NL NL NL NL NL NL NL
Solution:
A translator converts a program written in a high-
NL NL NL NL NL NL NL NL
level language (human readable and writable) to an equivalent program in a low-
NL NL NL NL NL NL NL NL NL NL NL NL
level language (machine readable and executable).
NL NL NL NL NL
EXERCISE 1.3 NL
1. Describe what happens when the programmer enters the string "Greetings!" in the Python shell.
NL NL NL NL NL NL NL NL NL NL NL NL NL
Solution:
Python reads the string "Greetings!", evaluates it, and displays this string (including single
NL NL NL NL NL NL NL NL NL NL NL NL NL
quotes) in the shell.
NL NL NL
2. Write a line of code that prompts the user for their name and saves the user’s input in a variable call
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
ed
name.
Solution:
name= input("Enter your name: ")
L
N NL NL NL NL
3. What is a Python script?
NL NL NL NL
Solution:
A Python script is a complete Python program that can be run from a computer’s operating system.
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
4. Explain what goes on behind the scenes when your computer runs a Python program.
NL NL NL NL NL NL NL NL NL NL NL NL NL
Solution:
If the program has not already been translated, Python’s compiler translates it to byte code. The
NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL NL
Python virtual machine then executes this code.
NL NL NL NL NL NL