Test Bank for Intro to Python for Computer Science and Data Science, 1st Edition

Preview Extract
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. 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 snippet in Part (a) 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. 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 2020 by Pearson Education, Inc. All Rights Reserved. 2 Chapter 2: Introduction to Python Programming Answer: c. 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) _______ (that is, a number with a decimal point). a. integer b. string c. floating-point number d. None of the above Answer: c. 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. 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. b. Floor division (//) divides a numerator by a denominator, yielding the highest integer thatโ€™s not greater than the result. c. The expression -13 / 4 evaluates to -3.25. d. The expression -13 // 4 evaluates to -3. Answer: d. Actually, the expression -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โ€”an indication 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. ยฉ Copyright 2020 by Pearson Education, Inc. All Rights Reserved. Chapter 2: Introduction to Python Programming 3 Answer: c. 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. 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. Answer: d. Actually, without the parentheses, the value of the expression in Part (b) would be 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. Actually, if an expression contains several exponentiation operations, Python applies them from right to left. ยฉ Copyright 2020 by Pearson Education, Inc. All Rights Reserved. 4 Chapter 2: Introduction to Python Programming 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). 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. 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. 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. 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. ยฉ Copyright 2020 by Pearson Education, Inc. All Rights Reserved. Chapter 2: Introduction to Python Programming 5 d. When print completes its task, it positions the screen cursor at the beginning of the next line by default. Answer: a. 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) or 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. Actually, placing two backslashes back-to-back tells print to display a backslash. 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. 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. Actually, a string delimited by double quotes may include double quotes, if you use the ” escape sequence, as in: print(“Display “hi” in quotes”) ยฉ Copyright 2020 by Pearson Education, Inc. All Rights Reserved. 6 Chapter 2: Introduction to Python Programming 2.6 Getting Input from the User 2.6 Q1: The value of the expression ‘7’ + ‘3’ is the ________. a. integer 10 b. string ’10’ c. string ’73’ d. integer 73. Answer: c. 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. 2.7 Decision Making: The if Statement and Comparison Operators 2.7 Q1: Which of the following statements a), b) or 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. All of the above statements are true. Answer: c. 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. ยฉ Copyright 2020 by Pearson Education, Inc. All Rights Reserved. Chapter 2: Introduction to Python Programming 7 2.7 Q3: Which of the following statements is false? a. A line that begins with the hash character (#) is a comment. b. If a line has a comment on it, that 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. 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. Answer: c. 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. ยฉ Copyright 2020 by Pearson Education, Inc. All Rights Reserved. 8 Chapter 2: Introduction to Python Programming d. Forgetting the colon (:) after the condition is a common syntax error. Answer: c. Actually, each suite must contain at least one statement. 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. 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. 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. 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 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 ยฉ Copyright 2020 by Pearson Education, Inc. All Rights Reserved. Chapter 2: Introduction to Python Programming 9 d. The following statement changes xโ€™s value: x = x + 10 Answer: c. 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]: x = 7 Out[1]: int In [2]: type(x) Out[2]: int In [3]: x = 4.1 In [4]: type(x) Out[4]: float In [5]: x = 'dog' In [6]: type(x) Out[6]: str 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 objects, even objects 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. 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. ยฉ Copyright 2020 by Pearson Education, Inc. All Rights Reserved. 10 Chapter 2: Introduction to Python Programming d. Measures of dispersion (also called measures of variability), such as count, help determine how spread out values are. Answer: d. 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. 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 (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. Actually, you can define custom reductions. ยฉ Copyright 2020 by Pearson Education, Inc. All Rights Reserved.

Document Preview (10 of 294 Pages)

User generated content is uploaded by users for the purposes of learning and should be used following SchloarOn's honor code & terms of service.
You are viewing preview pages of the document. Purchase to get full access instantly.

Shop by Category See All


Shopping Cart (0)

Your bag is empty

Don't miss out on great deals! Start shopping or Sign in to view products added.

Shop What's New Sign in