ITS Python Practice Exam 1 - Python Programming

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Evaluate the following partial code snippet: cities=['Anchorage','Juneau','Fairbanks','Ketchikan','Sitka','Wasilla'] for city in range(cities): print(f'{city} is a famous Alaskan city.') if city == 'Ketchikan': break

4

if grade __ 100: print ("Outstanding") elif grade __ 90: print ("Great") elif grade __ 70: print("Study hard") else: print("You are doing well. Strive to improve")

== >= <=

When declaring variables in Python, a data type must be specified.

No

b == 2.5

No

b == 3

No

c is b

No

A developer needs to add, to an existing app, code that will print a message a set number of times. However, the developer does not yet know the message but the variables used in the loop for a message are used elsewhere in the app. Which keyword, when added to the end of the code, will serve as a placeholder for future text? Here is the code example: for x in range(1,5):

Pass

Evaluate the following code that writes a message to an existing log file at the start of each day: with open('log.txt','w') as file: file.write('Daily Log') file.close()

The close function is not needed. The log file is overwritten each time it is opened.

When using the sys.argv[0] command as part of running a Python app in a command prompt, what does the 0 represent in sys.argv?

The file name

A developer needs to write code to reverse the character output of a product code. Which variable declaration will reverse one of the product codes?

[::-1]

Look at the following code examples and then indicate which symbol is needed in the missing area to have the print statement for the sales representative output on a single line.

\

A developer needs to build a data structure with animals and then sort the structure. Using drag and drop, drag the following lines of code into the correct order to produce this output: Bears Jaguars Lions Panthers

animals=["Bears","Panthers","Lions"] animals.append("Jaguars") animals.sort() for animal in animals: print(animal)

A developer is building a code block to log a current date and time for app activity. Which code snippet will replace the #current date and time comment with the correct date and time? import datetime log_time = #current date and time print("entry time: ", log_time)

datetime.datetime.now()

Complete the code examples to build a function that does the following: Its name is calcSubtotal The function takes an amount and a sales tax rate and calculates a subtotal The new subtotal is returned

def calcSubtotal (amount, salesTaxRate): return subtotal

In the following code example, add the necessary code to tell python to read and print the text file being opened, if the file already exists. If the file does not exist, print a message indicating as such.

exists workFile.read()

Using the dropdown arrow, add the symbol needed to get the number of items to print instead of {items}. The following code example, as is, prints "You have {items} items in stock" instead of the number of items in stock.

f

The following code examples needs to print the number of minutes per day to walk, starting with 10 minutes a day in week 1 and ending with 50 minutes a day in week 5. Choose the correct line of code to start this iteration and finish this code example.

for week in range(1,6)

Drag the lines of code into the correct order to accomplish the following: The messages "Great month" and "Keep it going" are printed for month sales of over 10000. Not all lines of code will be used.

if month_sales>10000: print("Great month") print("Keep it going")

An inventory manager needs to take data from calculations, an average inventory number in this case and have three possible outcomes: the nearest whole number up, represented by the upper_bound variable, the nearest whole number down, represented by the lower_bound variable, and the integer of the whole number, represented by the bound variable. Using the dropdown lists, complete the code snippet needed to fulfill the inventory manager's needs.

import math math.ceil math.floor math.trunc

Using the snippets of code below, use the drag-and-drop method to build code that will do the the following: Store the actual value of pi in a variable called pi Use pi to calculate the area of a circle and store it in a variable called area. A user will enter the radius of the circle and needs an option to have the radius be carried out to decimal places. Print the result (the area), formatted to two decimal places. Not every line of code will be used.

import math pi=math.pi radius=flat(input("Enter a radius for a circle.")) area=pi*radius**2 print(f"A circle with a radius of {radius} will have an area of %.2f."%area)

A game developer is testing some random number generators. The results should be as follows: result1 should have a random country from a list of countries result2 should display the list in a random order result3 should display two random countries from a list of countries Using the dropdown arrows, fill in the code needed to generate the proper random numbers for each variable.

import random choice(countries) shuffle(countries) sample(countries,2)

Look at the following code and then select the correct keyword to use to check to see if the word "nine" is part of the quote. If this code runs, it should return a value of True.

in

In this scenario, the number of complete units needs to be displayed, meaning that anything after a decimal point needs to not be represented. To accomplish this, the int function is needed as it extracts the whole number from a value with a floating decimal. The round function will not work because it will round the number up to 20. The floor function rounds a number down to the nearest integer. The ceil function will raise the number to 20. The correct code is as follows:

int

Analyze the following code: x == 7 y == 4 print(x>y and x-y>=2 or x+y==11 and not x*y>25) Using drag and drop, arrange the sequence of execution order for the print statement.

not x*y>25 x>y and x-y>=2 x+y==11 and not x*y>25 x>y and x-y>=2 or x+y==11 and not x*y>25

Take a look at the following code example, used to calculate a subtotal for an order: def calcTotal(taxable, amount, sales tax, shipping): if taxable=="Yes": subtotal=amount+(1*salesTax)+shipping elif shipping==0: pass else: subtotal=amount+shipping return subtotal order1=calcTotal("No", 500, .07, 0) print("Your order total is ", order1)

null

Take a look at the following function: def subtotal(order_amt, sales_tax): subtotal=float(order_amt)*(1+float(sales_tax)) return subtotal Which code example properly calls the function and returns a calculation, stored in a variable called order_total??

order_toal = subtotal(500,.07)

pieces=["king", "queen", "rook", "bishop", "knight, "pawn"] pieces.sort()

print(pieces[5]) print(pieces[-1])

Take a look at the code example below. In the missing code area, indicate the keyword or method necessary to throw an exception that the calling code can catch. Also, indicate the missing keyword to show a block of code no matter the outcome of the try portion of the error handling process.

raise finally

Choose the correct lines of code to satisfy the needs of the following function: Students with scores of 90 or higher get an A. Students with scores from 80 to 89 get a B. Students with scores from 70 to 79 get a C. Everyone else gets an F.

score >== 90: score >= 80: score >= 70: grade = "F"

Evaluate the following anatomy of a unit test, part of a larger block of code. deftest_memory(self): a=3 b=a #insert assert test here Which statement will test to see if a and b share the same memory space?

self.assertIs(a,b)

Using drag and drop, drag over the lines of that will accomplish the follwoing: Open the shirts file in a mode to where it cannot be written to Read the contents of the entire file Print the contents of the entire file Not every line of code will be used

shirtFile=open("shirts.txt","r") shirtFileContents=shirtFile.read() print(shirtFileContents)

A developer wants to make sure a serial number cannot be used in a calculation and that a dollar amount entered as a whole number can have decimals. Then, a message should be displayed to tell a customer the serial number and price for the products. Using the drop-down arrows, fill in the code to use the proper functions to convert the data to the necessary data types.

str float ,

Take a look at the following code example: carLoan=20000 intRate=1.05 licenseFee=500 totalLaon=carLoan+licenseFee*intRate

totalLoan=(carLoan+licenseFee)*intRate

The following code examples does a simple calculation to divide two numbers. You want to handle any errors gracefully and ensure that the last print statement always prints, even if there is an error. Using the dropdown arrows, fill in the missing keywords in the code to accomplish this.

try: except: else: finally:

You are trying to loop through some values retrieved from a list. You want the list to keep printing these values, but if the list sees a value of "end of day", then the printing should stop. Select the missing code examples to finish writing this code block.

while break scheduledEvent +=1

x=5 y=3 z=x+y> and x*y<15 or x-y>2

z=

In Python, which character(s) should be placed before text in a line of code to make that line of code a comment?

#

a__b=13 a__b=7 a__b=3.33 a__b=3 a__3=1

+ - / // %

A developer is starting to build a unit test and needs help figuring out the build-in module that is needed for the test, the way to check to see if the unit test is run as the main module, and the actual unit test to run to see if two values are equal to each other. Using the dropdown arrows, fill in the code needed to finish building the unit test.

TestCase assertEqual __name___

Python makes the distinction between integers and floating variables.

Yes

When setting a boolean variable, the value must start with a capital letter.

Yes

a == 90

Yes

a == b

Yes

a is b

Yes

c == -9

Yes

Analyze the following code: a=5 b=2 c=3 a**=b b*=c a//=b For each statement regarding the values of these variables, indicate Yes if the statement is false. c==3 b==6 a==1

Yes Yes No

Evaluate the following code: a=10 b=7 c=5 Using these variables, determine if each calculation is true. Indicate a Yes if it is true and a No if it is not. a>b and b>c a>c and not (b+c>a) a+b*c==85 or a-b*c==15

Yes No No

A new Python developer is a learning build-in modules and the methods used in those modules. Specifically, the developer needs to know the modules used to open text files, find the mean from a series of test scores, make directories within Python, and exit a gaming app when the game is over. Using drag and drop, match each method with the built-in module used.

io math os sys

A junior Python programmer wants to have a user input a location and then have the location print on the screen. In addition, the developer wants to tell the user to enter North, South, East, or West for a location. Using drag and drop, drag over the lines of code necessary to accomplish this. Not every line of code will be used.

location=["North","South","West","East"] response=input["North,South,West, or East for a location."] while response not in location: print("Try again.") response=input["North,South,West, or East for a location."]


Kaugnay na mga set ng pag-aaral

Everything, Everything vocabulary

View Set

Financial Management Test 1 Whitledge MSState

View Set

Cognitive Psychology Exam 2 (Set: 1 of 5)

View Set

NASM Chpt. 14 Integrated Design & the OPT Model

View Set

Chapter 10: Principles and Practices of Rehabilitation

View Set

statistiques pour médecins cours 3

View Set