20 Chapter 1.Computers and Programs 1.10 Exercises Review Questions True/False 1.Computer science is the study of computers. 2.The CPU is the "brain"of the computer. 3.Secondary memory is also called RAM. 4.All information that a computer is currently working on is stored in main memory. 5.The syntax of a language is its meaning,and semantics is its form. 6.A function definition is a sequence of statements that defines a new com- mand. 7.A programming environment refers to a place where programmers work. 8.A variable is used to give a name to a value so it can be referred to in other places. 9.A loop is used to skip over a section of a program. 10.A chaotic function describes things that are random and unpredictable. Multiple Choice 1.What is the fundamental question of computer science? a)How fast can a computer compute? b)What can be computed? c)What is the most effective programming language? d)How much money can a programmer make? 2.An algorithm is like a a)newspaper b)venus flytrap c)drum d)recipe 3.A problem is intractable when a)you cannot reverse its solution b)it involves tractors
20 Chapter 1. Computers and Programs 1,10| Exercises Review Questions True/ False 1. Computer science is the study of computers. 2. The CPU is the "brain" of the computer. 3. Secondary memory is also called RAM. 4. All information that a computer is currently working on is stored in main memory. 5. The S3mtax of a language is its meaning, and semantics is its form. 6. A function definition is a sequence of statements that defines a new com- mand. 7. A programming environment refers to a place where programmers work. 8. A variable is used to give a name to a value so it can be referred to in other places. 9. A loop is used to skip over a section of a program. 10. A chaotic function describes things that are random and unpredictable. Multiple Choice 1 . What is the fundamental question of computer science? a) How fast can a computer compute? b) What can be computed? c) What is the most effective programming language? d) How much money can a programmer make? 2. An algorithm aigontnm is like iK a a) newspaper b) venus flytrap c) drum d) recipe 3. A problem is intractable when a) you cannot reverse its solution b) it involves tractors
1.10.Exercises 21 c)it has many solutions d)it is not practical to solve 4.Which of the following is not an example of secondary memory? a)RAM b)hard drive c)floppy d)CD-Rom 5.Computer languages designed to be used and understood by humans are a)natural languages b)high-level computer languages c)machine languages d)fetch-execute languages 6.A statement is a)a translation of machine language b)a complete computer command c)a precise description of a problem d)a section of an algorithm 7.One difference between a compiler and an interpreter is a)a compiler is a program b)a compiler is used to translate high-level language into machine lan- guage c)a compiler is no longer needed after a program is translated d)a compiler processes source code 8.By convention,the statements of a program are often placed in a function called a)import b)IDLE c)program d)main 9.Which of the following is not true of comments? a)They make a program more efficient b)They are intended for human readers c)They are ignored by Python d)In Python,they begin with a pound sign(#) 10.The items listed in the parentheses of a function definition are called a)parentheticals b)scripts c)comments d)parameters Discussion 1.Compare and contrast the following pairs of concepts from the chapter:
1.10. Exercises 21 c) it has many solutions d) it is not practical to solve 4. Which of the following is not an example of secondary memory? a) RAM b) hard drive c) floppy d) CD-Rom 5. Computer languages designed to be used and understood by humans are a) natural languages b) high-level computer languages c) machine languages d) fetch-execute languages 6. A statement is a) a translation of machine language b) a complete computer command c) a precise description of a problem d) a section of an algorithm 7. One difference between a compiler and an interpreter is a) a compiler is a program b) a compiler is used to translate high-level language into machine language c) a compiler is no longer needed after a program is translated d) a compiler processes source code 8. By convention, the statements of a program are often placed in a function called a) import b) IDLE c) program d) main 9. Which of the following is not true of comments? a) They make a program more efficient b) They are intended for human readers c) They are ignored by Python d) In Python, they begin with a pound sign (#) 10. The items listed in the parentheses of a function definition are called a) parentheticals b) scripts c) comments d) parameters Discussion 1. Compare and contrast the following pairs of concepts from the chapter:
22 Chapter 1.Computers and Programs (a)Hardware vs.Software (b)Algorithm vs.Program (c)Programming Language vs.Natural Language (d)High-Level Language vs.Machine Language (e)Interpreter vs.Compiler (f)Syntax vs.Semantics 2.List and explain in your own words the role of each of the five basic func- tional units of a computer depicted in Figure 1.1. 3.Write a detailed algorithm for making a peanut butter and jelly sandwich (or some other everyday activity).You should assume that you are talking to someone who is conceptually able to do the task,but has never actually done it before.For example,you might be telling a young child. 4.As you will learn in a later chapter,many of the numbers stored in a com- puter are not exact values,but rather close approximations.For example, the value 0.1 might be stored as 0.10000000000000000555.Usually,such small differences are not a problem;however,given what you have learned about chaotic behavior in Chapter 1,you should realize the need for cau- tion in certain situations.Can you think of examples where this might be a problem?Explain. 5.Trace through the Chaos program from Section 1.6 by hand using 0.15 as the input value.Show the sequence of output that results. Programming Exercises 1.Start up an interactive Python session and try typing in each of the follow- ing commands.Write down the results you see. (a)print "Hello,world!" (b)print "Hello","world!" (c)print 3 (d)print 3.0 (e)print 2+3 (0 print2.0+3.0
22 Chapter 1. Computers and Programs (a) Hardware vs. Software (b) Algorithm vs. Program (c) Programming Language vs. Natural Language (d) High-Level Language vs. Machine Language (e) Interpreter vs. Compiler (f) Syntax vs. Semantics 2. List and explain in your own words the role of each of the five basic functional units of a computer depicted in Figure 1.1. 3. Write a detailed algorithm for making a peanut butter and jelly sandwich (or some other everyday activity). You should assume that you are talking to someone who is conceptually able to do the task, but has never actually done it before. For example, you might be telling a young child. 4. As you will learn in a later chapter, many of the numbers stored in a computer are not exact values, but rather close approximations. For example, the value 0.1 might be stored as 0.10000000000000000555. Usually such small differences are not a problem; however, given what you have learned about chaotic behavior in Chapter 1, you should realize the need for caution in certain situations. Can you think of examples where this might be a problem? Explain. 5. Trace through the Chaos program from Section 1.6 by hand using 0.15 as the input value. Show the sequence of output that results. Programming Exercises 1. Start up an interactive Python session and try typing in each of the following commands. Write down the results you see. (a) print "Hello, world!" (b) print "Hello", "world!" (c) print 3 (d) print 3.0 (e) print 2 + 3 (0 print 2.0 + 3.0
1.10.Exercises 23 (g)print"2"+"3" (h)print"2+3=",2+3 (i)print 2 3 (j)print 2 *3 (k)print 2/3 )print2.0/3.0 2.Enter and run the Chaos program from Section 1.6.Try it out with various values of input to see that it functions as described in the chapter. 3.Modify the Chaos program using 2.0 in place of 3.9 as the multiplier in the logistic function.Your modified line of code should look like this: x=2.0*x*(1-x) Run the program for various input values and compare the results to those obtained from the original program.Write a short paragraph describing any differences that you notice in the behavior of the two versions. 4.Modify the Chaos program so that it prints out 20 values instead of 10. 5.Modify the Chaos program so that the number of values to print is deter- mined by the user.You will have to add a line near the top of the program to get another value from the user: n input("How many numbers should I print?" Then you will need to change the loop to use n instead of a specific number. 6.(Advanced)Modify the Chaos program so that it accepts two inputs and then prints a table with two columns similar to the one shown in Sec- tion 1.8.(Note:You will probably not be able to get the columns to line up as nicely as those in the example.Chapter 4 discusses how to print numbers with a fixed number of decimal places.)
1.10. Exercises 23 (g) print "2" + "3" (h) print "2 + 3 =", 2+3 (i) print 2*3 (j) print 2 ** 3 (k) print 2/3 (1) print 2.0 / 3.0 2. Enter and run the Chaos program from Section 1.6. Try it out with various values of input to see that it functions as described in the chapter. 3. Modify the Chaos program using 2.0 in place of 3.9 as the multiplier in the logistic function. Your modified line of code should look like this: X = 2.0 * X * (1 - x) Run the program for various input values and compare the results to those obtained from the original program. Write a short paragraph describing any differences that you notice in the behavior of the two versions. 4. Modify the Chaos program so that it prints out 20 values instead of 10. 5. Modify the Chaos program so that the number of values to print is determined by the user. You will have to add a line near the top of the program to get another value from the user: n = input ("How mciny numbers should I print? ") Then you will need to change the loop to use n instead of a specific number. 6. (Advanced) Modify the Chaos program so that it accepts two inputs and then prints a table with two columns similar to the one shown in Section 1.8. (Note: You will probably not be able to get the columns to line up as nicely as those in the example. Chapter 4 discusses how to print numbers with a fixed number of decimal places.)