Skip to main content

TEST BANK for Intro to Python for Computer Science and Data Science: Learning to Program with AI, Bi

Page 1

Chapter 1, Introduction to Computers and Python

1

Introduction to Computers and Python 1.1

Introduction

1.1 Q1: Which of the following statements is false? a. Software (that is, the Python instructions you write, which are also called code) controls hardware (that is, computers and related devices). b. Programmers use the Python Standard Library and various data-science libraries to “reinvent the wheel.” c. Programmers use libraries to create software objects that they interact with to perform significant tasks with modest numbers of instructions. d. IPython executes Python instructions interactively and immediately shows their results. Answer: b. Programmers use the Python Standard Library and various datascience libraries to “reinvent the wheel.” Actually, programmers use the Python Standard Library and various data-science libraries to avoid “reinventing the wheel.” 1.1 Q2: Which of the following statements a), b) or c) is false? a. Decades ago, most computer applications ran on networked computers. b. Today’s applications can be written with the aim of communicating among the world’s computers via the Internet. c. A key intersection between computer science and data science is artificial intelligence. d. All of the above statements are true. Answer: a. Decades ago, most computer applications ran on networked computers. Actually, decades ago, most computer applications ran on “standalone” computers (that is, not networked together).

1.2

Hardware and Software

1.2 Q1: Which of the following statements is false? a. Computers can perform calculations and make logical decisions phenomenally faster than human beings can. b. Computers process data under the control of sequences of instructions called computer programs (or simply programs). c. A computer consists of various physical devices referred to as hardware (such as the keyboard, screen, mouse, solid-state disks, hard disks, memory, d. Computing costs are rising dramatically, due to the increasing complexity of hardware and software technologies. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


2

Chapter 1, Introduction to Computers and Python

Answer: d. Computing costs are rising dramatically, due to the increasing complexity of hardware and software technologies. Actually, computing costs are dropping dramatically, due to rapid developments in hardware and software technologies.

1.2.1

Moore’s Law

1.2 Q2: Every year or two, the capacities of computers have approximately doubled inexpensively. This remarkable trend often is called ________. a. the law of large numbers b. the principal of least privilege c. Moore’s law d. Wirth’s law Answer: c. Moore’s Law

1.2.2

Computer Organization

1.2 Q3: Which logical unit of the computer is the receiving section? a. input unit b. output unit c. memory unit d. central processing unit. Answer: a. input unit. 1.2 Q4: Information in the memory unit is ________. It’s typically lost when the computer’s power is turned off. a. persistent b. constant c. sticky d. volatile Answer: d. volatile. 1.2 Q5: A gigabyte is approximately one ________ bytes. a. thousand b. million c. billion d. trillion. Answer: c. billion. 1.2 Q6: The arithmetic and logic unit contains the ________ mechanisms that allow the computer, for example, to compare two items from the memory unit to determine whether they’re equal. a. decision b. calculation c. addition © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 1, Introduction to Computers and Python

3

d. None of the above. Answer: a. decision 1.2 Q7: Information on secondary storage devices is ________ — it’s preserved even when the computer’s power is turned off. a. volatile b. unstable c. transient d. persistent Answer: d. persistent 1.2 Q8: Secondary storage information takes much ________ to access than information in primary memory, but its cost per unit is much ________. a. less time, less b. longer, less c. less time, more d. longer, more Answer: b. longer, less

1.3

Data Hierarchy

1.3 Q1: A(n) ________ is the smallest data item in a computer. It can have the value 0 or 1. a. bit b. byte c. field d. record Answer: a. bit 1.3 Q2: Which of the following statements a), b) or c) is false? a. A computer’s character set represents every character as a pattern of 1s and 0s. b. All Unicode® characters are composed of four bytes (32 bits)—a scheme known as UTF-8 encoding. c. Unicode contains characters for many of the world’s languages. d. All of the above statements are true. Answer: b. All Unicode® characters are composed of four bytes (32 bits)—a scheme known as UTF-8 encoding. Actually, Python uses Unicode® characters that are composed of one, two, three or four bytes (8, 16, 24 or 32 bits, respectively)—known as UTF-8 encoding. 1.3 Q3: The most popular database model is the ________ database, in which data is stored in simple tables. a. network b. graph © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


4

Chapter 1, Introduction to Computers and Python

c. relational d. hierarchical Answer: c. relational 1.3 Q4: Which one of the following statements is true? a. a terabyte is larger than a petabyte. b. a kilobyte is exactly 1000 bytes. c. a gigabyte is 1024 megabytes. d. an exabyte is 1024 zettabytes. Answer: c. a gigabyte is 1024 megabytes.

1.4 Machine Languages, Assembly Languages and HighLevel Languages 1.4 Q1: Which of the following statements is false? a. Any computer can directly understand only its own machine language, defined by its hardware design. b. The most widely used Python implementation—CPython (which is written in the C programming language for performance)—uses a clever mixture of compilation and interpretation to run programs. c. Translator programs called assemblers convert assembly-language programs to machine language at computer speeds. d. Interpreter programs, developed to execute high-level language programs directly, avoid the delay of compilation, and run faster than compiled programs. Answer: d. Interpreter programs, developed to execute high-level language programs directly, avoid the delay of compilation, and run faster than compiled programs. Actually, interpreter programs run slower than compiled programs. 1.4 Q2: Which of the following statements is false? a. With the advent of assembly languages, computer usage increased rapidly, but programmers still had to use numerous instructions to accomplish even the simplest tasks. b. To speed the programming process, high-level languages were developed in which single statements could be written to accomplish substantial tasks. c. Translator programs called assemblers convert high-level-language source code into machine language. d. High-level languages instructions look almost like everyday English and contain commonly used mathematical notations. Answer: c. Translator programs called assemblers convert high-level-language source code into machine language. Actually, those translator programs are compilers. Assemblers convert assembly language programs into machine language. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 1, Introduction to Computers and Python

1.5

5

Introduction to Object Technology

1.5 Q1: Which of the following statements a), b) or c) is false? a. The classes that objects come from, are essentially reusable software components. b. Almost any verb can be reasonably represented as a software object in terms of attributes (e.g., name, color and size) and behaviors (e.g., calculating, moving and communicating). c. A class that represents a bank account might contain one method to deposit money to an account, another to withdraw money from an account and a third to inquire what the account’s balance is. d. All of the above statements are true. Answer: b. Almost any verb can be reasonably represented as a software object in terms of attributes (e.g., name, color and size) and behaviors (e.g., calculating, moving and communicating). Actually, almost any noun can be reasonably represented as a software object in terms of attributes (e.g., name, color and size) and behaviors (e.g., calculating, moving and communicating). 1.5 Q2: A program might call a bank-account object’s deposit ________ to increase the account’s balance. a. attribute b. method c. class d. function Answer: b. method 1. 5 Q3: A new class of objects can be created conveniently by inheritance—the new class (called the ________) starts with the characteristics of an existing class (called the ________), possibly customizing them and adding unique characteristics of its own. a. superclass, subclass b. hyperclass, subclass c. target, superclass d. subclass, superclass Answer: d. subclass, superclass

1.6

Operating Systems

1.6 Q1: Windows is a(n) ________ operating system—it’s controlled by Microsoft exclusively. a. proprietary b. private c. open source © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


6

Chapter 1, Introduction to Computers and Python

d. None of the above. Answer: a. proprietary 1.6 Q2: ________ is by far the world’s most widely used desktop operating system. a. Linux b. MacOS c. Windows d. none of the above Answer: c. Windows 1.6 Q3: With ________ software development, individuals and companies contribute their efforts in developing, maintaining and evolving software in exchange for the right to use that software for their own purposes, typically at no charge. a. object-oriented b. high-level c. open-source d. proprietary Answer: c. open-source 1.6 Q4: Which of the following organizations has millions of open-source projects under development? a. Python Software Foundation b. GitHub c. The Apache Software Foundation d. The Eclipse Foundation Answer: b. GitHub

1.7

Python

1.7 Q1: Python recently surpassed the programming language ________ as the most popular data-science programming language. a. DSPL b. Java c. C++ d. R Answer: d. R 1.7 Q2: Which of the following are reasons why Python is popular and everyone should consider learning it: a. It’s open source, free and widely available with a massive open-source community. b. It’s easier to learn than languages like C, C++, C# and Java, enabling novices and professional developers to get up to speed quickly. c. It’s easier to read than many other popular programming languages. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 1, Introduction to Computers and Python

7

d. All of the above. Answer: d. All of the above. 1.7 Q3: Which of the statements a), b) and c) below about the Anaconda Python distribution is false? a. It’s easy to install on Windows, macOS and Linux. b. It supports the latest versions of Python, the IPython interpreter and Jupyter Notebooks. c. It includes other software packages and libraries commonly used in Python programming and data science, allowing students to focus on learning Python, computer science and data science, rather than software installation issues. d. All of the above statements are true. Answer: d. All of the above statements are true.

1.8

It’ s the Libraries!

1.8 Q1: Which of the following statements about libraries is false? a. Using existing libraries helps you avoid “reinventing the wheel,” thus leveraging your program-development efforts. b. Rather than developing lots of original code—a costly and time-consuming process—you can simply create an object of a pre-existing library class, which takes only three Python statements. c. Libraries help you perform significant tasks with modest amounts of code. d. All of the above statements are false. Answer: b. Rather than developing lots of original code—a costly and timeconsuming process—you can simply create an object of a pre-existing library class, which takes only three Python statements. Actually, it takes only one statement.

1.8.1

Python Standard Library

1.8 Q2: Which of the following Python Standard Library modules offers additional data structures beyond lists, tuples, dictionaries and sets? a. sys and statistics b. collections c. queue d. b) and c) Answer: d. b) and c). 1.8 Q3: Which Python Standard Library module do we use for performance analysis? a. datetime b. time c. timeit © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


8

Chapter 1, Introduction to Computers and Python

d. sys Answer: c. timeit

1.8.2

Data-Science Libraries

1.8 Q4: Which of the following popular Python data science libraries are central to machine learning, deep learning and/or reinforcement learning: a. scikit-learn b. keras and tensorflow c. OpenAIGym d. All of the above Answer: d. All of the above

1.9

Other Popular Programming Languages

1.9 Q1: The popular programming languages Python and ________ are the two most widely used data-science languages. a. C b. Java c. JavaScript d. R Answer: d. R

1.10 Test-Drive: Using IPython and Jupyter Notebooks 1.10 Q1: In which IPython interpreter mode do you enter small bits of Python code called snippets and immediately see their results: a. interactive mode b. script mode c. program mode d. None of the above Answer: a. interactive mode

1.10.1 Using IPython Interactive Mode as a Calculator 1.10 Q2: When using IPython in interactive mode to evaluate a simple arithmetic expression in a new session, which of the following is false? a. The text "In [1]:" is a prompt, indicating that IPython is waiting for your input. You can type ? for help or you can begin entering snippets b. After you type an expression like 45 + 72 and press Enter, IPython reads the snippet, evaluates it and prints its result in Out[1]:. c. Then IPython displays the In [2] prompt to show that it’s waiting for you to enter your second snippet d. Python uses x for multiplication and the forward slash (/) for division. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 1, Introduction to Computers and Python

9

Answer: d. Python uses x for multiplication and the forward slash (/) for division. Actually, Python uses the asterisk (*) for multiplication. 1.10 Q3: What value is produced when Python evaluates the following expression? 5 * (12.7 - 4) / 2 a. 21 b. 21.75 c. 29.5 d. None of the above. Answer: b. 21.75

1.10.2 Executing a Python Program Using the IPython Interpreter No questions.

1.10.3 Writing and Executing Code in a Jupyter Notebook 1.10 Q4: Which of the following statements a), b) or c) is false? a. The Anaconda Python Distribution comes with the Jupyter Notebook—an interactive, browser-based environment in which you can write and execute code and intermix the code with text, images and video. b. The JupyterLab interface enables you to manage your notebook files and other files that your notebooks use (like images and videos). c. Jupyter Notebooks use IPython by default. d. All of the above statements are true. Answer: d. All of the above statements are true. 1.10 Q5: Which of the following statements a), b) or c) about the Jupyter Notebook is false? a. The unit of work in a notebook is a cell in which you can enter code snippets. b. By default, a new notebook contains one cell, but you can add more. c. To execute the current cell’s code, type Ctrl + Enter (or control + Enter). JupyterLab executes the code in IPython, then displays the results below the cell. d. All of the above statements are true. Answer: d. All of the above statements are true.

1.11 Internet and World Wide Web 1.11 Q1: Which of the following statements about the ARPANET and the Internet is false? a. The ARPANET was the precursor to today’s Internet. b. Today’s fastest Internet speeds are on the order of millions of bits per second with billion-bits-per-second (gigabit) speeds already being tested. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


10

Chapter 1, Introduction to Computers and Python

c. Although the ARPANET enabled researchers to network their computers, its main benefit proved to be the capability for quick and easy communication via what came to be known as electronic mail (e-mail). d. The protocol (set of rules) for communicating over the ARPANET became known as the Transmission Control Protocol (TCP). TCP ensured that messages, consisting of sequentially numbered pieces called packets, were properly delivered from sender to receiver, arrived intact and were assembled in the correct order. Answer: b. Today’s fastest Internet speeds are on the order of millions of bits per second with billion-bits-per-second (gigabit) speeds already being tested. Actually, today’s fastest Internet speeds are on the order of billions of bits per second with trillion-bits-per-second (terabit) speeds already being tested.

1.11.1 Internet: A Network of Networks 1.11 Q2: Which of the following statements about the Internet is false? a. One challenge was to enable different networks to communicate with each other. ARPA accomplished this by developing the Internet Protocol (IP), which created a true “network of networks,” the current architecture of the Internet. b. The combined set of Internet protocols is now called TCP/IP. c. Each Internet-connected- device has a TCP address—a unique numerical identifier used by devices communicating via TCP/IP to locate one another on the Internet. d. Bandwidth—the information-carrying capacity of communications lines—on the Internet has increased tremendously, while hardware costs have plummeted. Answer: c. Each Internet-connected- device has a TCP address—a unique numerical identifier used by devices communicating via TCP/IP to locate one another on the Internet. Actually, each Internet-connected- device has an IP address—a unique numerical identifier used by devices communicating via TCP/IP to locate one another on the Internet.

1.11.2 World Wide Web: Making the Internet User-Friendly 1.11 Q3: Which of the following statements is false? a. The World Wide Web is a collection of hardware and software associated with the Internet that allows computer users to locate and view documents (with various combinations of text, graphics, animations, audios and videos) on almost any subject. b. In 1989, Tim Berners-Lee of CERN (the European Organization for Nuclear Research) began developing HyperText Markup Language (HTML)—the technology for sharing information via “hyperlinked” text documents.

© Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 1, Introduction to Computers and Python

11

c. Berners-Lee also wrote communication protocols such as JavaScript Object Notation (JSON) to form the backbone of his new hypertext information system, which he referred to as the World Wide Web. d. In 1994, Berners-Lee founded the World Wide Web Consortium (W3C), devoted to developing web technologies. Answer: c. Berners-Lee also wrote communication protocols such as JavaScript Object Notation (JSON) to form the backbone of his new hypertext information system, which he referred to as the World Wide Web. Actually, in 1989, Tim Berners-Lee of CERN (the European Organization for Nuclear Research) began developing HyperText Markup Language (HTML)—the technology for sharing information via “hyperlinked” text documents.

1.11.3 The Cloud 1.11 Q4: Which of the following statements about the cloud is false? a. A service that provides access to itself over the Internet is known as a web service. b. Using cloud-based services in Python often is as simple as creating a software object and interacting with it. That object then uses web services that connect to the cloud on your behalf. c. We’ll use Twitter’s web services via the Python library Tweepy to get information about specific Twitter users, search for recent tweets and receive streams of tweets as they occur—that is, in real time. d. Azure is Google’s set of cloud-based services. Answer: d. Azure is Google’s set of cloud-based services. Actually, Azure is Microsoft’s set of cloud-based services. 1.11 Q5: The applications-development methodology of ________ enables you to rapidly develop powerful software applications by combining (often free) complementary web services and other forms of information feeds. a. cloud computing b. design patterns c. proprietary computing d. mashups Answer: d. mashups.

1.11.4 Internet of Things 1.11 Q6: The Internet is no longer just a network of computers—it’s an Internet of Things. A thing is any object with which of the following? a. an IP address b. the ability to send data over the Internet c. in some cases, the ability to receive data over the Internet d. Things can have all of the above. Answer: d. Things can have all of the above. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


12

Chapter 1, Introduction to Computers and Python

1.12 Software Technologies 1.12 Q1: Reworking programs to make them clearer and easier to maintain while preserving their correctness and functionality is called ________. a. refactoring b. design patterns c. editing d. None of the above. Answer: a. refactoring 1.12 Q2: Proven architectures for constructing flexible and maintainable objectoriented software are called ________. a. refactored architectures b. software blueprints c. engineered architectures d. design patterns Answer: d. design patterns

1.13 How Big Is Big Data? 1.13 Q1: Which of the following statements about big data is false? a. For computer scientists and data scientists, data is now as important as writing programs. b. One megabyte is exactly one million (1,000,000) bytes. c. According to a March 2016 AnalyticsWeek article, within five years there will be over 50 billion devices connected to the Internet (most of them through the Internet of Things, ) and by 2020 we’ll be producing 1.7 megabytes of new data every second for every person on the planet. d. The speed at which quantum computers now under development could operate at is so extraordinary that in one second, a quantum computer theoretically could do staggeringly more calculations than the total that have been done by all computers since the world’s first computer appeared. Answer: b. One megabyte is exactly one million (1,000,000) bytes. Actually, one megabyte is exactly 220 = 1024 x 1024 = 1,048,576 bytes. 1.13 Q2: Which of the following statements about energy consumption is false? a. According to a recent article, energy use for processing data in 2015 was growing at 20% per year and consuming approximately three to five percent of the world’s power. b. Another enormous electricity consumer is the blockchain-based cryptocurrency Bitcoin—processing just one Bitcoin transaction uses approximately the same amount of energy as powering the average American home for a year. c. The energy use comes from the process Bitcoin “miners” use to prove that transaction data is valid. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 1, Introduction to Computers and Python

13

d. Together, Bitcoin and Ethereum (another popular blockchain-based platform and cryptocurrency) consume more energy per year than Israel and almost as much as Greece. Answer: b. Another enormous electricity consumer is the blockchain-based cryptocurrency Bitcoin—processing just one Bitcoin transaction uses approximately the same amount of energy as powering the average American home for a year. Actually, processing just one Bitcoin transaction uses approximately the same amount of energy as powering the average American home for a week.

1.13.1 Big Data Analytics 1.13 Q3: Which of the following "V’s of big data" is described by "the validity of the data—is it complete and accurate? Can we trust that data when making crucial decisions? Is it real? a. Volume b. Velocity c. Variety d. Veracity Answer: d. Veracity

1.13.2 Data Science and Big Data Are Making a Difference: Use Cases No questions.

1.14 Case Study—A Big-Data Mobile Application 1.14 Q1: Once Waze converts a spoken command to text, it must determine the correct action to perform, which requires: a. JSON b. speech recognition c. natural language processing d. speech synthesis Answer: c. natural language processing 1.14 Q2: Each of the following statements about a typical mobile social navigation app is true. Which most captures the essence of the application? a. The app processes massive amounts of crowdsourced data—that is, the data that’s continuously supplied by users through their mobile GPS-based devices worldwide. b. The app uses speech synthesis to speak driving directions and alerts to you, and speech recognition to understand your spoken commands. c. The app uses your phone as a streaming Internet of Things (IoT) device. Each phone is a GPS sensor that continuously streams data over the Internet to the app. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


14

Chapter 1, Introduction to Computers and Python

d. The app probably stores its routing information in a graph database. Such databases can efficiently calculate shortest routes. Answer: a. The app processes massive amounts of crowdsourced data—that is, the data that’s continuously supplied by users through their mobile GPSbased

1.15 Intro to Data Science: Artificial Intelligence—at the Intersection of CS and Data Science 1.15 Q1: What is the "ultimate goal" of the field of artificial intelligence? a. Computers learning from massive amounts of data. b. Computer vision. c. Self-driving cars. d. Artificial general intelligence—computers that perform intelligence tasks as well as humans. Answer: d. Artificial general intelligence—computers that perform intelligence tasks as well as humans. 1.15 Q2: Which of the following statements about AI is false: a. For many decades, AI has been a field with solutions and no problems. b. Google’s AlphaZero is a game-playing AI that teaches itself to play games. c. In a 1997 match between IBM’s DeepBlue computer system and chess Grandmaster Gary Kasparov, DeepBlue became the first computer to beat a reigning world chess champion under tournament conditions. d.After training- itself in Go for just eight hours, AlphaZero was able to play Go vs. its AlphaGo predecessor, winning 60 of 100 games. Answer: a. For many decades, AI has been a field with solutions and no problems. Actually, for many decades, AI has been a field with problems and no solutions.

© Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 2: Introduction to Python Programming

1

Introduction to Python Programming 2.1 Introduction No questions.

2.2 Variables and Assignment Statements 2.2 Q1: Which of the following statements is false? a. Variables store values for later use in your code. b. The following statement creates x and uses the assignment symbol (=) to give x a value. x = 7 c. Every statement stops at the end of the line in which it begins. d. Once x and y are created and assigned values, you can use the values in expressions like: x + y Answer: c. Every statement stops at the end of the line in which it begins. Actually, it’s possible for statements to span more than one line. 2.2 Q2: Which of the following statements is false? a. The following assignment statement adds the values of variables x and y and assigns the result to the variable total: total = x + y b. The preceding snippet is read, “total is assigned the value of x + y.” c. The = symbol is the assignment operator. d. The right side of the = symbol always executes first, then the result is assigned to the variable on the symbol’s left side. Answer: c. The = symbol is the assignment operator. Actually, the = symbol is not an operator in Python. 2.2 Q3: Which of statements a), b) or c) is false? a. A variable name, such as x, is an identifier. b. Each identifier may consist of letters, digits and underscores (_) but may not begin with a digit. c. Python is case insensitive, so number and Number are the same identifier despite the fact that one begins with a lowercase letter and the other begins with an uppercase letter. d. All of the above statements are true. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


2

Chapter 2: Introduction to Python Programming

Answer: c. Python is case insensitive, so number and Number are the same identifier despite the fact that one begins with a lowercase letter and the other begins with an uppercase letter. Actually, Python is case sensitive, so number and Number are different identifiers because one begins with a lowercase letter and the other begins with an uppercase letter. 2.2 Q4: The value 10.5 is a(n) a. integer b. string c. floating-point number d. None of the above Answer: c. floating-point number.

(that is, a number with a decimal point).

2.2 Q5: Which of the following statements a), b) and c) is false. a. Python’s type built-in function determines a value’s type. b. A function performs its task when you call it by writing its name, followed by square brackets, []. c. The parentheses after a function name in a function call contain the function’s argument—the data that the type function needs to perform its task. d. All of the above statements are true. Answer: b. A function performs its task when you call it by writing its name, followed by square brackets, []. Actually, a function performs its task when you call it by writing its name, followed by parentheses, ().

2.3 Arithmetic 2.3 Q1: Which of the following statements a), b) or c) is false? a. Python uses the asterisk (*) as the multiplication operator (e.g., 7 * 4). b. The exponentiation (**) operator raises one value to the power of another (e.g., 2 ** 10). c. To square a number, you can use the exponent 1/2 (that is, 0.5), as in: 9 ** (1 / 2) d. All of the above statements are true. Answer: c. To square a number, you can use the exponent 1/2 (that is, 0.5). Actually, to square a number, use the exponent 2. The exponent 1/2 takes the square root of the number. 2.3 Q2: Which of the following statements is false? a. True division (/) divides a numerator by a denominator and yields a floatingpoint number with a decimal point.

© Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 2: Introduction to Python Programming

3

b. Floor division (//) divides a numerator by a denominator, yielding the highest integer that’s not greater than the result. Python truncates (discards) the fractional part. c. The expression -13 / 4 evaluates to -3.25. d. The expression -13 // 4 evaluates to -3. Answer: d. The expression -13 // 4 evaluates to -3. Actually, the expresion -13 // 4 evaluates to -4. 2.3 Q3: Which of the following statements is false? a. Dividing by zero with / or // is not allowed and results in an exception—a sign that a problem occurred: b. Python reports an exception with a traceback. c. Most exception names end with Exception. d. In IPython interactive mode, the line that begins with ----> shows the code that caused the exception. Answer: c. Most exception names end with Exception. Actually, most exception names end with Error. 2.3 Q4: Which of the following statements is false? a. The value of the following expression is 3: 17 % 5 b. The value of the following expression is 0.5: 7.5 % 3.5 c. You can use the remainder operator for applications such as determining whether one number is a multiple of another. d. You can use the remainder operator to determine whether a number is odd or even. Answer: a. The value of the following expression is 3: 17 % 5 Actually, the value of the expression is 2. 2.3 Q5: Which of the following statements is false? a. Algebraic expressions must be typed in straight-line form using Python’s operators. b. The following code multiplies 10 times the quantity 5 + 3, yielding the result 80: 10 * (5 + 3) c. Parentheses in an expression are said to be redundant (unnecessary) if removing them yields the same result. d. The parentheses in part b) above are redundant. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


4

Chapter 2: Introduction to Python Programming

Answer: d. The parentheses in part b) above are redundant. Actually, without the parentheses, the value of the expression is different—it’s now 53, so the parentheses are not redundant. 2.3 Q6: Which of the following statements is false? a. Python applies the operators in arithmetic expressions according to the rules of operator precedence, which are generally the same as those in algebra. b. Parentheses have the highest level of precedence, so expressions in parentheses evaluate first—thus, parentheses may force the order of evaluation to occur in any sequence you desire. c. In expressions with nested parentheses, such as (a / (b - c)), the expression in the innermost parentheses (that is, b - c) evaluates first. d. If an expression contains several exponentiation operations, Python applies them from left to right. Answer: d. If an expression contains several exponentiation operations, Python applies them from left to right. Actually, if an expression contains several exponentiation operations, Python applies them from right to left. 2.3 Q7: Which of the following statements is false? a. When we say that Python applies certain operators from left to right, we are referring to the operators’ grouping. b. In the following expression, the addition operators (+) group as if we parenthesized the expression as a + (b + c): a + b + c c. All Python operators of the same precedence group left-to-right except for the exponentiation operator (**), which groups right-to-left. d. You can use redundant parentheses to group subexpressions to make expressions clearer. For example, the second-degree polynomial y = a * x ** 2 + b * x + c can be parenthesized, for clarity, as y = (a * (x ** 2)) + (b * x) + c Answer: b. In the following expression, the addition operators (+) group as if we parenthesized the expression as a + (b + c): a + b + c Actually, the addition operators (+) group from left to right as if we parenthesized the expression as (a + b) + c. 2.3 Q8: Which of the following statements about arithmetic operators is false? a. Each arithmetic operator may be used with integers and floating-point numbers. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 2: Introduction to Python Programming

5

b. If both operands are integers, the result is an integer—except for the true-division (/) operator, which always yields a floating-point number. c. If both operands are floating-point numbers, the result is a floating-point number. d. Expressions containing an integer and a floating-point number are mixed-type expressions—these always produce an integer. Answer: d. Expressions containing an integer and a floating-point number are mixed-type expressions—these always produce an integer. Actually, these mixed-type expressions always produce a floating-point number.

2.4 Function print and an Intro to Single- and DoubleQuoted Strings 2.4 Q1: Which of the following statements is false? a. When you evaluate expressions in interactive mode, the text that print displays is preceded by Out[] with a snippet number in the square brackets. b. print does not display a string’s quotes, though there is a way to display quotes in strings. c. You also may enclose a string in double quotes ("), as in: print("Welcome to Python!") but Python programmers generally prefer single quotes. d. When print completes its task, it positions the screen cursor at the beginning of the next line. Answer: a. When you evaluate expressions in interactive mode, the text that print displays is preceded by Out[] with a snippet number in the square brackets. Actually, unlike when you evaluate expressions in interactive mode, the text that print displays is not preceded by Out[] with a snippet number in the square brackets. 2.4 Q2: Which of the following statements a), b) and c) is false? a. When a backslash (\) appears in a string, it’s known as the escape character. b. The backslash and the character immediately following it form an escape sequence. For example, \n represents the newline character escape sequence, which tells print to move the output cursor to the next line. c. Placing two backslashes back-to-back tells print to display a blank line. d. All of the above statements are true. Answer: c. Placing two backslashes back-to-back tells print to display a blank line. Actually, placing two backslashes back-to-back tells print to display a backslash. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


6

Chapter 2: Introduction to Python Programming

2.5 Triple-Quoted Strings 2.5 Q1: Triple-quoted strings are used to create: a. multiline strings, b. strings containing single or double quotes c. docstrings, which are the recommended way to document the purposes of certain program components. d. All of the above Answer: d. All of the above. 2.5 Q2: Which of the following statements is false? a. A string delimited by single quotes may include double-quote characters: print('Display "hi" in quotes') b. A string delimited by double quotes may include single quote characters: print("Display the name O'Brien") c. A string delimited by double quotes may not include double quotes. d. To avoid using \' and \" inside strings, you can enclose such strings in triple quotes: Answer: c. A string delimited by double quotes may not include double quotes. Actually, a string delimited by double quotes may not include double quotes, unless you use the \" escape sequence: print("Display \"hi\" in quotes")

2.6 Getting Input from the User 2.6 Q1: When Python “adds” the string values '7' and '3', it produces: a. the integer 10 b. the string '10' c. the string '73' d. the integer 73. Answer: c. the string '73' 2.6 Q2: What does the int function attempt to do in the following code? value = input('Enter an integer: ') value = int(value) a. Convert an integer to a string. b. Convert a string to an integer. c. Convert a non-object to an object. d. None of the above. Answer: b. Convert a string to an integer. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 2: Introduction to Python Programming

7

2.7 Decision Making: The if Statement and Comparison Operators 2.7 Q1: Which of the following statements a), b) and c) is false? a. A condition is a Boolean expression with the value True or False. b. True and False are keywords—words that Python reserves for its language features. c. Using a keyword as an identifier causes a ValueError. d. None of the above statements is false. Answer: c. Using a keyword as an identifier causes a ValueError. Actually, using a keyword as an identifier causes a SyntaxError. 2.7 Q2 Which of the following statements is true? a. The if statement uses a condition to decide whether to execute a statement (or a group of statements). b IPython interactive mode is helpful for executing brief code snippets and seeing immediate results. c. When you have many statements to execute as a group, you typically write them as a script stored in a file with the .py (short for Python) extension. d. All of the above statements are true. Answer: d. All of the above statements are true. 2.7 Q3: Which of the following statements is false? a. A line that begins with the hash character (#) is a comment. b. A comment must begin the line with a hash character (#). c. Comments do not cause the computer to perform any action when the code executes. d. The Style Guide for Python Code states that each script should start with a docstring that explains the script’s purpose, Answer: b. A comment must begin the line with a hash character (#). Actually, a comment also can begin to the right of the code on a given line and continue until the end of that line. Such a comment documents the code to its left. 2.7 Q4: Which of the following statements is false? a. You use blank lines and space characters to make code easier to read. b. Together, blank lines, space characters and tab characters are known as white space. c. Python ignores all white space. d. Python allows you to split long code lines in parentheses without using continuation characters.

© Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


8

Chapter 2: Introduction to Python Programming

Answer: c. Python ignores all white space. Actually, Python ignores most white space—some indentation (which is created with whitespace characters) is required. [Also, white space in strings is significant.] 2.7 Q5: You may spread a lengthy statement over several lines with the ________ continuation character. a. @ b. \ c. % d. ^ Answer: b. \ 2.7 Q6:Which of the following statements is false? a. The following if statement uses the == comparison operator to determine whether the values of variables number1 and number2 are equal: if number1 == number2: print(number1, 'is equal to', number2) b. Each if statement consists of the keyword if, the condition to test, and a colon (:) followed by an indented body called a suite. c. Each suite contains zero or more statements. d. Forgetting the colon (:) after the condition is a common syntax error. Answer: c. Each suite contains zero or more statements. Actually, each suite must contain one or more statements. 2.7 Q7: Which of the following statements is false? a. Python requires you to indent the statements in suites. b. Incorrect indentation of the statements in a suite can cause errors. c. Using the assignment symbol (=) instead of the equality operator (==) in an if statement’s condition is a common logic error. d. Using == in place of = in an assignment statement can lead to subtle problems. Answer: c. Using the assignment symbol (=) instead of the equality operator (==) in an if statement’s condition is a common logic error. Actually, Using the assignment symbol (=) instead of the equality operator (==) in an if statement’s condition is a common syntax error. 2.7 Q8: The chained comparison 3 < y <= 11 is false for which of the following values of y: a. 11 b. 10 c. 4 d. 3 Answer: d. 3 © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 2: Introduction to Python Programming

9

2.8 Objects and Dynamic Typing 2.8 Q1: Assume x is 3, y is 7.1 and z is ’11.5’. Which of the following statements is incorrect? a. type(x) is int b. the value of z is 11.5 c. the value of y is 7.1 d. type(z) is str Answer: b. the value of z is 11.5. Actually, the value of z is ’11.5’. 2.8 Q2: Which of the following statements is false? a. Assigning an object to a variable binds (associates) that variable’s name to the object. You can then use the variable in your code to access the object’s value. b. After the following snippet’s assignment, the variable x refers to the integer object containing 7. x = 7 c. The following statement changes x’s value: x + 10 d. The following statement changes x’s value: x = x + 10 Answer: c. The following statement changes x’s value: x + 10 Actually, this statement does not change x’s value—you can change x’s value via assignment as shown in Part (d). 2.8 Q3: Which of the following Python concepts are demonstrated directly or indirectly by the following code: In [1]: type(x) Out[1]: int In [2]: x = 4.1 In [3]: type(x) Out[3]: float In [4]: x = 'dog' In [5]: type(x) Out[5]: str © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


10

Chapter 2: Introduction to Python Programming

a. Python uses dynamic typing—it determines the type of the object a variable refers to while executing your code. b. Over its lifetime a variable can be bound to different variables, even variables of different types. c. Python creates objects in memory and removes them from memory as necessary. This removal process—called garbage collection—helps ensure that memory is available for new objects you create. d. All of the above. Answer: d. All of the above.

2.9 Intro to Data Science: Basic Descriptive Statistics 2.9 Q1: Which of the following statements about descriptive statistics is false? a. The minimum is the smallest value in a collection of values. b. The range is the values starting with the minimum and ending with the maximum. c. The count is the number of values in a collection. d. Measures of dispersion (also called measures of variability), such as count, help determine how spread out values are. Answer: d. Measures of dispersion (also called measures of variability), such as count, help determine how spread out values are. Actually, measures of dispersion (also called measures of variability), such as range, help determine how spread out values are. 2.9 Q2: Which of the following statements is incorrect: a. min(17, 19, 23, 29, 31, 37, 43) is a valid call to built-in function min, b. The range of values is simply the minimum through the maximum value. c. Much of data science is devoted to getting to know your data. d. All of the above are correct. Answer: d. All of the above are correct. 2.9 Q3: Which of the following statements about functional-style programming is false? a. With functional-style programming capabilities you can write code that is more concise, clearer and easier to debug—that is, find and correct errors. b. The min and max functions are examples of a functional-style programming concept called reduction. They reduce a collection of values to a single value. c. Other reductions you’ll see include the sum, average, variance and standard deviation of a collection of values. d. All reductions are built into Python—you cannot define custom reductions. Answer: d. All reductions are built into Python—you cannot define custom reductions. Actually, you can define custom reductions. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Chapter 3: Control Statements and Program Development

1

Control Statements and Program Development 3.1 Introduction No questions.

3.2 Algorithms 3.2 Q1: Which of the following statements a), b) or c) is false? a. You can solve any computing problem by executing a series of actions in a specific order. b. An algorithm is a procedure for solving a problem in terms of the actions to execute, and the order in which these actions execute. c. Program control specifies the order in which statements (actions) execute in a program. d. All of the above statements are true. Answer: d. All of the above statements are true.

3.3 Pseudocode 3.3 Q1: is an informal English-like language for “thinking out” algorithms. a. Code b. Python c. Quasicode d. Pseudocode Answer: d. Pseudocode

3.4 Control Statements 3.4 Q1: Various Python statements enable you to specify that the next statement to execute may be other than the next one in sequence. This is called and is achieved with Python control statements. a. transfer of control b. alternate execution c. spread execution d. None of the above © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


2

Chapter 3: Control Statements and Program Development

Answer: a. transfer of control 3.4 Q2: Which of the following statements is false? a. The notion of structured programming became almost synonymous with “goto elimination.” b. Although Python has a goto statement, you should avoid it to minimize errors. c. Bohm and Jacopini demonstrated that all programs could be written using three forms of control—namely, sequential execution, the selection statement and the iteration statement. d. Python statements execute one after the other “in sequence,” unless directed otherwise. Answer: b. Although Python has a goto statement, you should avoid it to minimize errors. Actually, Python does not have a goto statement. 3.4 Q3: The most important flowchart symbol is the that a decision is to be made, such as in an if statement. a. rectangle b. flowline c. diamond d. small circle Answer: c. diamond

, which indicates

3.4 Q4: Which of the following statements is false? a. Python provides three types of selection statements that execute code based on a condition—an expression that evaluates to either True or False: b. The if...else statement performs an action if a condition is True or performs a different action if the condition is False. c. Anywhere a single action can be placed, a group of actions can be placed. d. The if…elif…else statement is called a double-selection statement because it selects one of two different actions (or groups of actions). Answer: d. The if…elif…else statement is called a double-selection statement because it selects one of two different actions (or groups of actions). Actually, the if…elif…else statement is called a multiple-selection statement because it selects one of many different actions (or groups of actions). 3.4 Q5: Python provides two iteration statements— a. do while and for b. while and for each c. do while and for each d. while and for Answer: d. while and for

and

3.4 Q6: Which of the following statements a), b) or c) is false? © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.

:


Chapter 3: Control Statements and Program Development

3

a. You form each Python program by combining as many control statements of each type as you need for the algorithm the program implements. b. With Single-entry/single-exit (one way in/one way out) control statements, the exit point of one connects to the entry point of the next. This is similar to the way a child stacks building blocks—hence, the term control-statement stacking. c. You can construct any Python program from only six different forms of control (sequential execution, and the if, if…else, if…elif…else, while and for statements). You combine these in only two ways (control-statement stacking and control-statement nesting). This is the essence of simplicity. d. All of the above statements are true. Answer: d. All of the above statements are true.

3.5 if Statement 3.5 Q1: Which of the following statements is false? a. Indenting a suite is required; otherwise, an IndentationError syntax error occurs. b. If you have more than one statement in a suite, those statements do not need to have the same indentation. c. Sometimes error messages may not be clear. The fact that Python calls attention to the line is usually enough for you to figure out what’s wrong. d. Programs that are not uniformly indented are hard to read. Answer: b. If you have more than one statement in a suite, those statements do not need to have the same indentation. Actually, an IndentationError occurs if you have more than one statement in a suite and those statements do not have the same indentation. 3.5 Q2: Which of the following statements is false? a. The decision (diamond) symbol contains a condition that can be either True or False. b. The diamond flowchart symbol has three flowlines emerging from it. c. One flowline emerging from the diamond flowchart symbol indicates the direction to follow when the condition in the symbol is True. This points to the action (or group of actions) that should execute. d. Another flowline emerging from the diamond flowchart symbol indicates the direction to follow when the condition is False. This skips the action (or group of actions). Answer: b. The diamond flowchart symbol has three flowlines emerging from it. Actually, the diamond flowchart symbol has two flowlines emerging from it. 3.5 Q3 Which of the following statements is false? a. You can base decisions on any expression. © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


4

Chapter 3: Control Statements and Program Development

b. A condition which evaluates to a nonzero value is considered to be True. c. A condition which evaluates to a value of zero is considered to be False: d. Strings containing characters are True and empty strings ('', "" or """""") are False. Answer: All of the above statements are true. 3.5 Q4 Which of the following statements a), b) or c) is false? a. Using the equality operator == instead of the assignment symbol = in an assignment statement can lead to subtle problems. b. If instead of defining a variable grade with the assignment: grade = 85 we accidentally write: grade == 85 then grade would be undefined and we’d get a NameError. c. If grade had been defined before the statement grade == 85, the statement would evaluate to True or False, depending on grade’s value, and not perform the intended assignment—fortunately, this is harmless. d. All of the above statements are true. Answer: c. If grade had been defined before the statement grade == 85, the statement would evaluate to True or False, depending on grade’s value, and not perform the intended assignment—fortunately, this is harmless. Actually, this is a logic error.

3.6 if…else and if…elif…else Statements 3.6 Q1: Which of the following statements a), b) or c) is false? a. The if…else statement performs different suites, based on whether a condition is True or False. b. If we initialize the variable grade to 55, the following code snippet would display Failed: if grade >= 60: print('Passed') else: print('Failed') c. The condition above is True, so the if suite displays Passed. d. All of the above statements are true. Answer: c. The condition above is True, so the if suite displays Passed. Actually, the condition above is False, so the if suite displays Failed. 3.6 Q2: Which of the following statements about navigating through snippets is false? © Copyright 1992-2020 by Pearson Education, Inc. All Rights Reserved.


Turn static files into dynamic content formats.

Create a flipbook