Python

¡Supera tus tareas y exámenes ahora con Quizwiz!

Adventure Works Cycles is creating a program that allows customers to log the number of miles biked. The program will send messages based on how many miles the customer logs. You create the following Python code. Line numbers are included for reference only. 01 2 name = input("What is your name? ") 3 return name 4 5 calories = miles * calories_per_mile 7 return calories 8 distance = int(input("How many miles did you bike this week? ")) 9 burn_rate = 50 10 biker = get_name() 11 calories_burned = calc_calories(distance, burn_rate) 12 print(biker, ", you burned about" ,calories_burned, "calories.") You need to define the two required functions. Which code segments should you use for line 01 and line 04? Each correct answer presents part of the solution. Choose two.

01 def get_name(): 04 def calc_calories(miles, calories_per_mile):

You are writing a Python program to automate inventory. Your first task is to read a file of inventory transactions. The file contains sales from the previous day, including the item id, price, and quantity. The following shows a sample of data from the file: 10, 200, 5 20, 100, 1 The code must meet the following requirements: . Each line of the file must be read and printed . If a blank line is encountered, it must be ignored . When all lines have been read, the file must be closed You create the following code. Line numbers are included for reference only. 01 inventory = open("inventory.txt", 'r') 02 eof = False 03 while eof == False: 4 line = inventory.readline() 5 6 7 print(line) 8 else: 9 print ("End of file") 10 eof = True 11 inventory,close() Which code should you write for line O5 and line 06?

05 if line != '': 06 if line != "\n":

You develop a Python application for your company. You have the following code. Line numbers are ¡included for reference only. 01 def main(a,b,c,d): 2 value = a+b*c-d 3 return value Use the drop-down menus to select the answer choice that answers each question based on the information presented in the code segment. Answer Area Which part of the expression will be evaluated first? [1] . Which operation will be evaluated second? [2] . Which expression is equivalent to the expression in the function? [3] .

1. b*c 2. addition 3. (a + (b*c)) - d

You create the following program to locate a conference room and display the room name. Line numbers are included for reference only. 01 rooms = {1: 'Foyer', 2: 'conference Room'} 02 room = input('Enter the room number: ') 03 if not room in rooms: 4 print('Room does not exist.') 5 else: 6 print("The room name is " + rooms[room]) Colleagues report that the program sometimes produces incorrect results. You need to troubleshoot the program. Use the drop-down menus to select the answer choice mat answers each question based on the information presented in the code segment. Answer Area Which two data types are stored in the rooms list at line 01? [1] . What is the data type of room at line 02? [2] . Why does line 03 fail to find the rooms? [3] .

1.int and string 2.string 3.Mismatched data type(s)

Evaluate the following Python arithmetic expression:(3*(1+2)**2 - (2**2)*3)What is the result? *

15

During school holidays, you volunteer to explain some basic programming concepts to your younger siblings. You want to introduce the concept of data types in Python. You create the following three code segments: * Code segment 1 x1 = "20" y1 = 3 a = x1 * y1 * Code segment 2 x2 = 6 y2 = 4 b = x2 / y2 * Code segment 3 x3 = 2.5 y3 = 1 c = x3 / y3 · 204 · You need to evaluate the code segments. For each of the following statements, select Yes if the statement is true. Otherwise, select No. After executing code segment 1, the data type of variable a is str. After executing code segment 2, the data type of variable b is float. After executing code segment 3, the data type of variable c is int.

After executing code segment 1, the data type of variable a is str. YES After executing code segment 2, the data type of variable b is float. YES After executing code segment 3, the data type of variable c is int. NO

You write the following code: import datetime d = datetime.datetime(2017, 4, 7) print('{:%B-%d-%y}' .format(d)) num=1234567.890 print('{:, .4f}' .format(num)) You run the program. What is the output?

April--07--17 1,234,567.8900 Press any key to continue...

You evaluate the following code: numList = [0,1,2,3,4] print(5 in numList) What is the output of the print statement

False

You are creating a function that reads a data file and prints each line of the file. You write the following code. Line numbers are included for reference only. 01 import os 02 def read_file(file): 03 line = None 4 if os.path.isfile(file): 5 data = open(file,'r') 6 while line != '': 7 line = data.readline() 8 print(line) The code attempts to read the file even if the file does not exist. You need to correct the code. Which three lines have indentation problems? Each correct answer presents part of the solution. Choose three.

Line 06 Line 07 Line 08

Tailspin Toys uses Python to control its new toy Happy Clown. The program has errors that cause the clown to run around in an infinite circle. You have been hired to help debug the following Happy Clown code. Line numbers are included for reference only. 01 import math 02 #default motion for happy clown 03 power = True 04 move = 0 5 while(power): 6 if move == 0: 7 turnValue = math.pi/move 8 move+=5 9 else: 10 turnValue = 0 11 move = 0 Which error exists in the code?

Line 07 causes a runtime error due to division by zero.

You create a function to calculate the power of a number by using Python. You need to ensure that the function ¡s documented with comments. You create the following code. Line numbers are included for reference only. 01 # The calc_power function calculates exponents 02 # x is the base 03 # y is the exponent 04 # The value of x raised to the y power is returned 05 def calc_power(x, y): 6 comment = "#Return the value" 07 return x**y 4 raise x to the y power For each of the following statements, select Yes if the statement is true. Otherwise, select No.

Lines 01 through 04 will be ignored for syntax checking. YES The pound sign (#) is optional for lines 02 and 03. NO The string in line 06 will be interpreted as a comment. NO Line 07 contains an inline comment. YES

You write the following code: import sys try: file_in = open("in.txt", 'r') file_out = open("out.txt", 'w+') except lOError: print('cannot open', file_name) else: i = 1 for line in file_in: print(line. rstrip()) file_out.write("line " + str(i) + ": " + line) i = i + 1 file_in.close() file_out.close () The out.txt file does not exist. You run the code. The code will execute without error. Review the underlined text. If it makes the statement correct, select "No change is needed." If the statement is incorrect, select the answer choice that makes the statement correct.

No change is needed.

You develop a Python application for your company. You want to add notes to your code so other team members will understand it What should you do? *

Place the notes after the # sign on any line.

You are writing a program that calculates a user's year of birth. The program asks users for their age and the current year, then outputs the user's year of birth. You write the following code. Line numbers are included for reference only. 01 age = input("Enter your age: ") 02 year = input("Enter the four digit year: ") 03 born = eval(year) - eval(age) 04 message = "You were born in " + str(born) 05 print(message) You need to ensure that the program uses the appropriate data types. What data types are used? Use the drop-down menus to select the answer choice that answers each question based on the information presented in ¡ne code segment. NOTE: Each correct selection is worth one point. Answer Area What data type is age in line 01? [1] . What data type is born in line 03? [2] . What data type is message in line 04? [3] . [1] A. int B. str C. float D. bool [2] A. int B. str C. float D. bool [3] A. int B. str C. float D. bool

What data type is age in line 01? str What data type is born in line 03? int What data type is message in line 04? str

a = 'Config' print(a) b = a a += 'config2' print(a) print(b) Use the drop-down menus to select the answer choice that answers each question based on the information presented in the code segment.

What is displayed after the first print? Config1 What is displayed after the second print? Config1Config2 What is displayed after the third print? Config1

You are developing a Python application for your company. You write the following code: numList = [1,2,3,4,5] alphaList = ["a","b","c","d","e"] print(numList is alphaList) print(numList == alphaList) numList = alphaList print(numList is alphaList) print(numList == alphaList)

What is displayed after the first print? No What is displayed after the second print? No What is displayed after the third print? Yes What is displayed after the fourth print? Yes

You are writing a function that returns the data type of the value that is passed in. You write the following code. Line numbers are included for reference only. 01 def checkType(value): 2 dataType = type(value) 3 return dataType 4 print(checkType(True)) 5 print(checkType(1.0)) 6 print(checkType(1)) 7 print(checkType("True")) Use the drop-down menus to select the answer choice that answers each question based on the information presented in the code segment. Answer Area What is printed at line 04? [1] . What is printed at line 05? [2] . What is printed at line 06? [3] . What is printed at line 07? [4] .

What is printed at line 04? <class 'bool'> What is printed at line 05? <class 'float'> What is printed at line 06? <class 'int'> What is printed at line 07? <class 'str'>

01 def read_file(file): 02 line = None 3 if os.path.isfile(file): 4 data = open(file,'r') 5 for line in data: 6 print(line) When you run the program, you receive an error on line 03. What is causing the error?

You need to import the os library.

You are building a Python program that displays all of the prime numbers from 2 to 100. How should you complete the code? To answer, drag the appropriate code segments to the correct location. Each code segment may be used once, more than once, or not at all. You may need to drag the split bar between panes or scroll to view content. NOTE: Each correct selection is worth one point.

[A] p=2 while p <= 100: is_prime = True [G] for i in range(2, p): if p % i == 0: is_prime = False [C] break [E] p = p + 1

You write the following code: list_1 = [1, 2] list_2 = [3, 4] list_3 = list_1 + list_2 list_4 = list_3 * 3 print(list_4) You run the code. What is the output value?

[[1, 2], [3, 4], [1, 2], [3, 4], [1, 2], [3, 4]]

Woodgrove Bank must generate a report that shows the average balance for all customers each day. The report must truncate the decimal portion of the balance. two code segments should you use? Each correct answer presents a complete solution. Choose two.

average_balance = total_deposits//number_of_customers average_balance = int(total_deposits/number_of_customers)

You develop a Python application for your school.A list named colors contains 200 colors. You need to slice the list to display every othercolor starting with the second color.Which code should you use?

colors [1::2]

Woodgrove Bank is migrating their legacy bank transaction code to Python.You have been hired to document the migrated code.Which documentation syntax is correct? *

def get_balance(): #Returns the current balance of the bank account return balance

You develop a Python application for your company. A list named employees contains 200 employee names, the last five being company management. You need to slice the list to display all employees excluding management. Which two code segments should you use? Each correct answer presents a complete solution. Choose two.

employees [:-5] employees [0:-5]

You are writing an application that uses the sqrt function. The program must referencethe function using the name squareRoot.You need to import the function.Which code segment should you use? *

from math import sqrt as squareRoot

You work on a team that is developing a game for AdventureWorks. You need to write code that generates a random number that meets the following requirements: . The number is a multiple of 5.. The lowest number is 5. . The highest number is 100.Which two code segments will meet the requirements? Each correct answer presents a complete solution. Choose two.

from random import randrange print(randrange(5, 100, 5)) from random import randint print(randint(1, 20) * 5)

You are writing a Python application for a dance studio. The studio wants to encourage youth and seniors to sign up. Minors and seniors must receive a 10% discount. You write the following code. Line numbers are included for reference only. 01 def get_discount(minor, senior): 2 discount = .1 3 4 discount = 0 5 return discount You need to complete the code. Which code should you add on line 03

if not (minor or senior):

You find errors while evaluating the following code. Line numbers are included for reference only. 01 numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 02 index = 0 03 while (index < 10) 4 print(numbers[index]) 5 6 if numbers(index) = 6 7 break 8 else : 9 index += 1 You need to correct the code at line 03 and line 06. How should you correct the code? Use the drop-down menus to select the answer choice that answers each question based on the information presented in the code segment. Answer Area Which code segment should you use at line 03? [1] . Which code segment should you use at line 06? [2] .

line 3:while(index < 10): line 6:if numbers(index) == 6:

You are creating a function that manipulates a number. The function has the following requirements: . A float is passed into the function. The function must take the absolute value of the float. Any decimal points after the integer must be removed. Which two math functions should you use? Each correct answer is part of the solution. Choose two.

math.floor(x) math.fabs(x)

You develop a Python application for your company. You need to accept input from the user and print that information to the user screen. You have started with the following code. Line numbers are included for reference only. 01 print("What is your name?") 02 03 print(name) Which code should you write at line 02?

name = input()

You develop a Python application for your school.You need to read and write data to a text file. If the file does not exist it must be created.If the file has content the content must be removed.Which code should you use?

open("local_data", "w+")

A classmate has asked you to debug the following code: x = 4 while x >= 1: if x % 4 == 0: print ("party") elif x - 2 < 0: print("cake") elif x / 3 == 0: print("greeting") else: print("birthday") x = x - 1 What is the output that is printed to the screen?

party birthday birthday cake

You are creating a Python program that shows a congratulation message to employees on their service anniversary.You need to calculate the number of years of service and print a congratulatory message.You have written the following code. Line numbers are included for reference only. 01 start = input("How old were you on your start date?) 02 end = input('How old are you today?") 03 You need to complete the program. Which code should you use at line 03? *

print("congratulations on " + str(int(end) - int(start)) + " years of service!")

You are creating an ecommerce script that accepts input from the user and outputs the data in a comma delimited format. You write the following lines of code to accept input item = input("Enter the item name: ") sales = input("Enter the quantity: ") The output must meet the following requirements: . Strings must be enclosed inside of double-quotes . Numbers must not be enclosed in quotes or other characters . Each item must be separated with a comma You need to complete the code to meet the requirements. Which three code segments should you use? Each correct answer presents a complete solution. Choose three

print('"{0}",{1}'.format(item, sales)) print('"' + item + '",' + sales) print('"%s", %s' % (item, sales))

You are writing code that generates a random integer with a minimum value of 5 and a maximum value of 11. Which two functions should you use? Each correct answer presents a complete solution. Choose two.

random.randint(5, 11) random.randrange(5, 12, 1)

You need to write code that generates a random float with a minimum value of 0.0 and a maximum value of 1.0.Which statement should you use?

random.random()

Northwind Traders has hired you as an intern on the coding team that creates ecommerce applications.You must write a script that asks the user for a value. The value must be used as awhole number in a calculation, even if the user enters a decimal value.You need to write the code to meet the requirements.Which code segment should you use? *

totalltems = int(input("How many items would you like?"))


Conjuntos de estudio relacionados

8.3 Elements of a Valid Contract

View Set

Declaration Of Independence Review Eng3 - 1/27

View Set

New English File. Beginner. Unit 3 A

View Set

Project/Service Management 2015 - 2019

View Set

Solid State Drive vs. Hard Disk Drive

View Set

Chapter 10 understanding individual Behavior

View Set

BPS Ch 10 MC, BPS Ch 5 MC, BPS Ch 6 MC, BPS Ch 7 MC, BPS Ch 8 MC, BPS Ch 9 MC, BPS Ch 11 MC

View Set