python programming chapter 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Write a String constant that is the empty string.

' '

Write a String constant consisting of exactly 5 exclamation marks.

'!!!!!'

Write an expression that computes the average of the variables exam1 and exam2 (both already assigned values).

(exam1+exam2)/2

Each of the walls of a room with square dimensions has been built with two pieces of sheetrock, a smaller one and a larger one. The length of all the smaller ones is the same and is stored in the variable small. Similarly, the length of all the larger ones is the same and is stored in the variable large. Write a single expression whose value is the total area of this room. DO NOT use any method invocations.

(small + large) ** 2

The dimensions (width and length) of room1 have been read into two variables : width1 and length1. The dimensions of room2 have been read into two other variables : width2 and length2. Write a single expression whose value is the total area of the two rooms.

(width1*length1)+(width2*length2)

In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Write an expression whose value is the 8th harmonic number.

1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8

Which of the following IS a legal identifier?

5_And_10 Five_&_Ten ( ____________ ) ........ answer LovePotion#9 "Hello World"

Given two variables , already defined, best_value and second_best_value, write a statement that swaps their associated values.

best_value,second_best_value=second_best_value,best_value

Calculate the BMI of a person using the formula BMI = ( Weight in Pounds / ( ( Height in inches ) x ( Height in inches ) ) ) x 703 and assign the value to the variable bmi. Assume the value of the weight in pounds has already been assigned to the variable w and the value of the height in inches has been assigned to the variable h. Take care to use floating-point division.

bmi = (w/(h)*(h))*703

A cookie recipe calls for the following ingredients: • 1.5 cups of sugar • 1 cup of butter • 2.75 cups of flour The recipe produces 48 cookies with this amount of ingredients. Write a program that asks the user how many cookies they want to make and then displays the number of cups of each ingredient needed for the specified number of cookies in the following format: You need 5 cups of sugar, 3 cups of butter, and 7 cups of flour.

cookies=input('Enter number of cookies:') x=(int(cookies)) s=(x/48)*1.5 b=(x/48)*1 f=(x/48)*2.75 print("You need "+str(s)+" cups of sugar, "+str(b)+" cups of butter," " and " +str(f)+" cups of flour.")

Given the variables cost_of_bus_rental and max_bus_riders, write an expression corresponding to the cost per rider (assuming the bus is full).

cost_of_bus_rental/max_bus_riders

In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Assume that n is an integer variable whose value is some positive integer N. Assume also that hn is a variable whose value is the Nth harmonic number. Write an expression whose value is the (N+1)th harmonic number.

hn + 1.0/(n+1)

Given two already defined variables , i and j, write a statement that swaps their associated values.

i ,j=j, i

Variables i and j each have associated values. Swap them, so that i becomes associated with j's original value, and j becomes associated with is original value. You can use two more variables itemp and jtemp.

i,j=j,i

Given a variable fraction that contains a double value, write a statement that writes the value of fraction to standard output.

import) sys sys.stdout.write(str(fraction)

Define a variable precise and make it refer to 1.09388641

precise=1.09388641

Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the amount of change (in cents) that would have to be paid.

price % 100

You are given two variables , both already defined. One is named price and is associated with a float and is the price of an order. The other is total_number and is associated with an int and is the number of orders. Write an expression that calculates the total price for all orders.

price*total_number

Given the variable price_per_case, write an expression corresponding to the price of a dozen cases.

price_per_case*12

Write an expression that computes the remainder of the variable principal when divided by the variable divisor. (Assume that each is associated with an int.)

principal%divisor

Given a variable count, which contains an integer value, write a statement that writes the value of count to standard output.

print (count)

Given two variables iVal and fVal, containing respectively an integer and a double value, write a statement that writes both of their values to standard output in the following format: i=iVal f=fVal.

print("i=" + str (iVal) + " f=" + str (fVal))

Write a single print statement that displays the value of variable num1 followed immediately (i.e., with no white space) by the value of variable num2. For example, if num1 has the value 123 and num2 has the value 456, your statement should result in the output 123456.

print(str(num1)+str(num2))

Write an expression that computes the difference of two variables sales_summer and sales_spring, both of which have already defined.

sales_summer-sales_spring

Assign 7 to a variable named seven.

seven=7

A wall has been built with two pieces of sheetrock, a smaller one and a larger one. The length of the smaller one is stored in the variable small. Similarly, the length of the larger one is stored in the variable large. Write a single expression whose value is the length of this wall.

small+large

Which of the following is not true?

An algorithm allows ambiguity. QUESTION 2: The programmer solves the problems of a user by expressing an algorithm in a programming language to make a program that can run on a computer.

Which of the following names in a program is equivalent to the name int?

Int INT All of the above (None of the above) .... answer

The character escape sequence to force the cursor to go to the next line is: The character escape sequence to force the cursor to advance forward to the next tab setting is: The character escape sequence to represent a single quote is: The character escape sequence to represent a double quote is: The character escape sequence to represent a backslash is:

\n \t \" \\

Write an expression that computes the average of the values 12 and 40, and assign it to the variable avg, which has already been

avg=(12+40)/2

Assign the average of the values in the variables a, b, and c to a variable avg. Assume that the variables a, b, and c have already been assigned a value, but do not assume that the values are all floating-point. The average should be a floating-point value.

avg=(a+b+c)/3.

Associate two String variables named background and selectionColor with the values "white" and "blue" respectively.

background="white" selectionColor="blue"

Assign 8 to a variable named eight.

eight=8

Initialize the variable empty to the empty string.

empty=''

Write an expression that computes the difference of the variables ending_time and starting_time.

ending_time-starting_time

Given two variables , first_place_winner and second_place_winner, write some code that swaps their associated values. Use any additional variables as necessary.

first_place_winner,second_place_winner=second_place_winner,first_place_winner

Initialize the variable foreground to "black"

foreground="black"

Associate the variable foreground with the value "red".

foreground="red"

Given the variables full_admission_price and discount_amount (already defined), write an expression corresponding to the price of a discount admission.

full_admission_price-discount_amount

Assume there is a variable , h already assigned a positive integer value. Write the code necessary to assign its square to the variable g. For example, if h had the value 8 then g would get the value 64.

g=h**2

Given two variables matric_age and grad_age, write a statement that makes the associated value of grad_age 4 more than that of matric_age.

grad_age=(4+matric_age)

Of the following variable names, which is the best one for keeping track of whether a patient has a fever or not?

hasFever

You have two siblings, Josh and Cindy, and you want to find the average of your three ages. Write a program that takes your ages as integer values from standard input and stores them in the variables josh, cindy, and me, then prints the average of the three numbers to standard output.

josh= int(input()) cindy= int(input()) me= int(input()) avg = (josh + cindy + me)/3 print (avg)

Use the variables k, d, and s so that they can read three different values from standard input--an integer, a double, and a string respectively. On one line, print these variables in reverse order with exactly one space in between each. On a second line, print them in the original order with one space in between them.

k = input() d = input() s = input() print (s, d, k) print (k, d, s)

Define two variables , one named length making it refer to 3.5 and the other named width making it refer to 1.55.

length=3.5 width=1.55

The purpose of testing a program with different combinations of data is to expose run-time and errors.

logical

Write a program that asks the user for the number of males and the number of females registered in a class using two separate inputs ("Enter number of males:", "Enter number of females:"). The program should display the percentage of males and females (round to the nearest whole number) in the following format: Percent males: 35% Percent females: 65% Use string formatting.

males=int(input("Enter number of males:")) females=int(input("Enter number of females:")) total= males+females percentmale= males/total percentfemale= females/total print("Percent males:", end=" ") print(format(percentmale,'.0%')) print("Percent females:", end=" ") print(format(percentfemale,'.0%'))

Of the following variable names, which is the best one for keeping track of whether an integer might be prime or not?

mightBePrime

Which is the best identifier for a variable to represent the amount of money your boss pays you each month?

monthlyPay

Which of the following is NOT a legal identifier?

outrageouslyAndShockinglyLongRunon _42 _ lovePotionNumber9 ( 7thheaven )...... answer

Given the variables taxable_purchases and tax_free_purchases (which already have been defined), write an expression corresponding to the total amount purchased.

taxable_purchases+tax_free_purchases

Define a variable temperature and make it refer to 98.6.

temperature=98.6

Associate the variable named text with the empty string.

text=''

Write an expression that computes the sum of two variables total1 and total2, which have already been defined.

total1+total2

You are given two variables , already defined, named total_weight, containing the weight of a shipment, and weight_of_box, containing the weight of the box in which a product is shipped. Write an expression that calculates the net weight of the product.

total_weight-weight_of_box

You are given two variables , both already defined, one associated with a float and named total_weight, containing the weight of a shipment, the other associated with an int and named quantity, containing the number of items in the shipment. Write an expression that calculates the weight of one item.

total_weight/quantity

A location in memory used for storing data and given a name in a computer program is called a because the data in the location can be changed.

variable

Write an expression that computes the sum of two variables verbal_score and math_score (assume that both have already been defined).

verbal_score + math_score

Assume that a variable x has been assigned a integer value,. Write an expression whose value is the last (rightmost) digit of x.

x%10


Ensembles d'études connexes

MKTG 361 EXAM 4 REVIEW QUESTIONS

View Set

Lesson 8: Money,Banking, Saving, Investing

View Set

Ch. 7: Radio, Recording, and Popular Music

View Set