98-381 : Introduction to Programming Using Python : Part 02
-
Evaluate the following Python arithmetic expression:
What is the result?
- 3
- 13
- 15
- 69
-
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 [1:-4]
- employees [:-5]
- employees [1:-5]
- employees [0:-4]
- employees [0:-5]
-
HOTSPOT
You are an intern for ABC electric cars company. You must create a function that calculates the average velocity of their vehicles on a 1320 foot (1/4 mile) track. The output must be as precise as possible.
How should you complete the code? To answer, select the appropriate code segments in the answer area.
-
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 removedWhich two math functions should you use? Each correct answer is part of the solution. (Choose two.)
- math.fmod(x)
- math.frexp(x)
- math.floor(x)
- math.ceil(x)
- math.fabs(x)
Explanation:
C: math.floor(x) returns the largest integer less than or equal to x.
E: math.fabs(x) returns the absolute value of x.Incorrect Answers:
A: math.fmod() takes two variables
B: math.frexp(x) returns the mantissa and exponent of x as the pair (m, e). m is a float and e is an integer
D: math.ceil(x) returns the smallest integer greater than or equal to x -
You are writing an application that uses the sqrt function. The program must reference the function using the name squareRoot.
You need to import the function.
Which code segment should you use?
- import math.sqrt as squareRoot
- import sqrt from math as squareRoot
- from math import sqrt as squareRoot
- from math.sqrt as squareRoot
-
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, 12)
- random.randint(5, 11)
- random.randrange(5, 12, 1)
- random.randrange(5, 11, 1)
-
DRAG DROP
You are writing a function that works with files.
You need to ensure that the function returns None if the file does not exist. If the file does exist, the function must return the first line.
You write the following code:
In which order should you arrange the code segments to complete the function? To answer, move all code segments from the list of code segments to the answer area and arrange them in the correct order.
-
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:
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 closedYou create the following code. Line numbers are included for reference only.
Which code should you write for line 05 and line 06?
-
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.
Which code should you write at line 02?
- name = input
- input(“name”)
- input(name)
- 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”, “r”)
- open(“local_data”, “r+”)
- open(“local_data”, “w+”)
- open(“local_data”, “w”)
Explanation:
Modes ‘r+’, ‘w+’ and ‘a+’ open the file for updating (reading and writing). Mode ‘w+’ truncates the file. -
HOTSPOT
The ABC organics company needs a simple program that their call center will use to enter survey data for a new coffee variety.
The program must accept input and return the average rating based on a five-star scale. The output must be rounded to two decimal places.
You need to complete the code to meet the requirements.
How should you complete the code? To answer, select the appropriate code segments in the answer area.
NOTE: Each correct selection is worth one point.
-
HOTSPOT
The ABC company is building a basketball court for its employees to improve company morale.
You are creating a Python program that employees can use to keep track of their average score.
The program must allow users to enter their name and current scores. The program will output the user name and the user’s average score. The output must meet the following requirements:
The user name must be left-aligned.
If the user name has fewer than 20 characters, additional space must be added to the right.
The average score must have three places to the left of the decimal point and one place to the right of the decimal (XXX.X).How should you complete the code? To answer, select the appropriate code segments in the answer area.
NOTE: Each correct selection is worth one point.
-
HOTSPOT
You find errors while evaluating the following code. Line numbers are included for reference only.
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.
NOTE: Each correct selection is worth one point.
-
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.
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 01
- Line 02
- Line 03
- Line 04
- Line 05
- Line 06
- Line 07
- Line 08
-
This question requires that you evaluate the underlined text to determine if it is correct.
You write the following code:
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
- The code runs, but generates a logic error
- The code will generate a runtime error
- The code will generate a syntax error
-
HOTSPOT
For each of the following statements, select Yes if the statement is true. Otherwise, select No.
-
HOTSPOT
You are developing a Python application for an online game.
You need to create a function that meets the following criteria:
– The function is named update_score
– The function receives the current score and a value
– The function adds the value to the current score
– The function returns the new scoreHow should you complete the code? To answer, select the appropriate code segments in the answer area.
-
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
- Place the notes after the last line of code separated by a blank line
- Place the notes before the first line of code separated by a blank line
- Place the notes inside of parentheses on any time
-
The ABC company 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.
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():
- 01 def get_name(biker):
- 01 def get_name(name):
- 04 def calc_calories():
- 04 def calc_calories(miles, burn_rate):
- 04 def calc_calories(miles, calories_per_mile):
-
HOTSPOT
You create a function to calculate the power of a number by using Python.
You need to ensure that the function is documented with comments.
You create the following code. Line numbers are included for reference only.
For each of the following statements, select Yes if the statement is true. Otherwise, select No.