Chapter 2
Trace code that includes escape sequences What is displayed by the following code segment? print("\"Hello World!\"") 1. Hello World! 2. "Hello World!" 3. \"Hello World!\" 4. The program reports an error
2. "Hello World!"
What functions are used to convert strings to numbers? What functions can be used to convert a string into a number? 1. 1. stri and len 2. 2. int and float 3. 3. sqrt, abs and round 4. 4. integer and float
2. 2. int and float
Which is the name of a collection of programming instructions that carry out a particular task to control the behavior of an object? A ___________________________ is a collection of programming instructions that can be appllied to an object. 1. 1.function 2. 2. method 3. 3. class 4. 4. object
2. 2. method
Which statement correctly creates a new variable by combining the strings ? Which statement correctly creates a new variable by combining the two string variables: firstName and lastName? 1. 1.name = "firstName" + "lastName" 2. 2.name = firstName + lastName 3. 3.name = first name + last name 4. 4. name = firstName & lastName
2. 2.name = firstName + lastName
What is wrong with the following code snippet? What is wrong with the following code snippet? num = 78A 1. The num variable is never assigned a value 2. 78A is not a valid value in a Python program 3. The name num is not a valid variable name 4. The num variable is never initialized
2. 78A is not a valid value in a Python program
What is the value of the variable num after the following code statement? What is the value of the variable num after the following code snippet? num = 5 num2 = 6 num = num2 + 3 1. 5 2. 9 3. 8 4. 11
2. 9
Identify a character within a string. What letter is displayed by the following code segment? title = "Python for Everyone" print(title[3]) 1. e 2. h 3. o 4. t
2. h
What is the data type of the value returned by the input function? 1. integer 2. string 3. float 4. character
2. string
Given the following Code , what is printed? x = 5.0 y = x + 3 x = x - 1 z = 10 x = x + z print('v1: {}, v2: {}, v3: {}'.format(x, y, z)) 1. v1: 10, v2: 14, v3: 8 2. v1: 14.0, v2: 8.0, v3: 10 3. v1: 14, v2: 8, v3: 10 4. v1: 8, v2: 10, v3: 14
2. v1: 14.0, v2: 8.0, v3: 10
Working with Numbers and Strings Which of the following statements causes Python to report an error? 1. x = 17 + 18.4 2. x = 17 + "18.4" 3. x = 17 + int(18.4) 4. x = 17 + float("18.4")
2. x = 17 + "18.4"
Which line of code creates a variable named x and initializes it to the integer 5? Which line of code creates a variable named x and initializes it to the integer 5? 1. x = 5.0 2. x = 5 3. x = '5' 4. x = "5"
2. x = 5
Given a float variable a = 1234.4567890123 What is the output of print ("test: {0:.4f}".format(test))
Answer: test: 1234.5679
Given a float variable a = 1234.4567890123 What is the output of print ("test: %.3f" %test)
Answer: test: 1234.568
Consider the following code segment: a = input("Enter the value of a: ") b = input("Enter the value of b: ") print(a[0] + b[-1]) When this code segment is run the user enters Mr. at the first prompt and Payne at the second prompt. The output displayed is:
Your Answer: Me
Which output format string correctly allows for 5 positions before and two digits after the decimal point? 1. "%8.2f" 2. "%5.2f" 3. "%7.2f" 4. "%5d.2f"
1. "%8.2f"
Trace code that invokes the lower method on a string Consider the following code segment: product = "Cookies" product = product.lower() After this code segment executes, the value of the product variable is: 1. "cookies" 2. "cOOKIES" 3. "Cookies" 4. "COOKIES"
1. "cookies"
Which is the result of the following code snippet? What is printed by the following code snippet? name = "today is thursday" name.replace("t", "T") name.replace("i", "I") print(name) 1. 1. today is thursday 2. 2. Today is Thursday 3. 3. Today Is Thursday 4. 4. Today Is thursday
1. 1. today is thursday
What is it called when you join two strings together in Python? What is it called when you join two strings together in Python? 1. 1.concatenation 2. 2. addition 3. 3.repetition 4. 4.conversion
1. 1.concatenation
Given the following code, what would be printed if 25 was the input? myNumber =int( input("Enter a number")) test = 5 print (test + myNumber) 25 1. 5 + 25 2. 30 3. TypeError: must be str, not int 4. TypeError: unsupported operand type(s) for +: 'int' and 'str'
1. 5 + 25
What is the definition of a variable? A variable is: 1. A storage location with a name 2. An assignment statement 3. An expression 4. A point in a program where a decision is made
1. A storage location with a name
What is wrong with this assignment statement? What is wrong with this assignment statement? num + 10 = num2 1. The left hand side of an assignment statement cannot include an operator. 2. Nothing, this statement compiles and executes. 3. The value of 10 must be defined before this statement can be executed. 4. The num variable must be defined before this statement can be executed
1. The left hand side of an assignment statement cannot include an operator.
What is a variable called that should remain unchanged throughout your program? What is a variable called that should remain unchanged throughout your program? 1. a constant variable 2. a data variable 3. a string variable 4. a boolean variable
1. a constant variable
How is a value stored in a variable? How is a value stored in a variable? 1. an assignment statement 2. an expression 3. a print statement 4. an equality statement
1. an assignment statement
Which statement correctly saves the price in the variable cost? userInput = input("Please enter the price:") 1. cost = float(userInput) 2. cost = userInput 3. cost = int(userInput) 4. cost = float[userInput]
1. cost = float(userInput)
What function is used to read input from the keyboard? What function is used to read a value from the keyboard? 1. input 2. print 3. keyboard 4. next
1. input
Given the code snippet below, what code is printed? What output is generated by the following code snippet? firstName = "Pamela" middleName = "Rose" lastName = "Smith" print(firstName[0], middleName[0], lastName[5]) 1. 1. nothing, this causes an index of bounds error 2. 2. PRh 3. 3. P R h 4. 4. PRS
1. nothing, this causes an index of bounds error
Which statement correctly saves the price in the variable quantity? Which statement correctly saves the number of items in the variable quantity? userInput = input("Please enter the quantity:") 1. quantity = float(userInput) 2. quantity = userInput 3. quantity = int(userInput) 4. quantity = int[userInput]
1. quantity = float(userInput)
What is a sequence of characters? A sequence of characters is referred to as a: 1. 1. string 2. 2. module 3. 3. variable 4. 4. expression
1. string
Given the following Code , what is printed? x = 5 y = x + 3 x = x - 1 z = 10 x = x + z print('v1: {2}, v2: {0}, v3: {1}'.format(x, y, z)) 1. v1: 10, v2: 14, v3: 8 2. v1: 14.0, v2: 8.0, v3: 10 3. v1: 14, v2: 8, v3: 10 4. v1: 8, v2: 10, v3: 14
1. v1: 10, v2: 14, v3: 8
Trace code that invokes the replace method on a string Consider the following code segment: title = "Python for Everyone" newTitle = title.replace("e", "*") After this code runs, the value stored in newTitle is: 1. "Python for *veryone" 2. "Python for Ev*ryone" 3. "Python for Ev*ryon*" 4. "Python for *v*ryon*"
3. "Python for Ev*ryon*"
Trace code that reads two values from the user Consider the following code segment: a = input("Enter the value of a: ") b = input("Enter the value of b: ") print(a + b) When this code segment is run the user enters 1 at the first prompt and 5 at the second prompt. The output displayed is: 1. 1 2. 6 3. 15 4. 1 + 5
3. 15
What is printed by the following code snippet? cost = 25.45378 print("%.2f" % cost) 1. 25.45378 2. %25.45 3. 25.45 4. nothing, there is an error
3. 25.45
Which is the result of the following code snippet? What is printed by the following code snippet? name = "Robert" formalName = name.upper() print(formalName) 1. 1. Robert 2. 2. robert 3. 3. ROBERT 4. 4. formalName
3. 3. ROBERT
String Length What is the value of length after this statement: length = len("Good Morning")? 1. 1.10 2. 2.11 3. 3.12 4. 4.13
3. 3.12
Given the following Code , what is printed? x = 5 y = x + 3 x = x - 1 z = 10 x = x + z print('v1: {}, v2: {}, v3: {}'.format(x, y, z)) 1. v1: 10, v2: 14, v3: 8 2. v1: 14.0, v2: 8.0, v3: 10 3. v1: 14, v2: 8, v3: 10 4. v1: 8, v2: 10, v3: 14
3. v1: 14, v2: 8, v3: 10
What is the term used to store a value for later use in a Python program? To store a value for later use in Python, the programmer needs to create a: 1. number 2. character 3. variable 4. boolean
3. variable
Read numerical input from the user The line of code which reads a value from the user and stores it in a variable named x as a floating-point value is: 1. x = float() 2. x = input("Enter the value of x: ") 3. x = float(input("Enter the value of x: ")) 4. x = input(float())
3. x = float(input("Enter the value of x: "))
Given the following code, what is printed? x=1234.5678 print ("${0:,.2f}".format(x)) 1. 1234.568 2. 1,234.56789 3. $1234.57 4. $1,234.57
4. $1,234.57
What is the index value of the letter 'h' in the string below ? What is printed by the following code snippet? num = int("45") * float("1.5") print(num) 1. 1. nothing, this causes an error 2. 2. 46.5 3. 3. 45 * 1.5 4. 4. 67.5
4. 4. 67.5
Given the following code, what would be printed if 25 was the input? myNumber = input("Enter a number") test = 5 print (test + myNumber) 25 1. 5 + 25 2. 30 3. TypeError: must be str, not int 4. TypeError: unsupported operand type(s) for +: 'int' and 'str'
4. TypeError: unsupported operand type(s) for +: 'int' and 'str'
Python naming conventions for variables Which of the following variables should be coded as a constant in Python? 1. character: 'a' 2. string: "hello" 3. number: 1234 4. pi: 3.14159
4. pi: 3.14159
The message used to tell the user what input is expected is known as a(n): 1. input 2. keyword 3. comment 4. prompt
4. prompt
Given the following Code , what is printed? x = 5 y = x + 3 x = x - 1 z = 10 x = x + z print('v1: {1}, v2: {2}, v3: {0}'.format(x, y, z)) 1. v1: 10, v2: 14, v3: 8 2. v1: 14.0, v2: 8.0, v3: 10 3. v1: 14, v2: 8, v3: 10 4. v1: 8, v2: 10, v3: 14
4. v1: 8, v2: 10, v3: 14