Seto_Kaiba_Python

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

Convert the decimal value 83 into binary. 10000111 11100001 01010011 01010100

01010011

What is the value stored in num after the following code segment executes? value = 72 div = 10 num = value % div

2 (with margin: 0)

What is the decimal equivalent of the binary value 00111010 ?

58 (with margin: 0)

What is the value stored in num after the following code segment executes? value = 72 div = 10 num = value // div

7 (with margin: 0)

What is the value stored in num after the following code segment executes? value = 72 div = 10 num = value / div

7.2 (with margin: 0)

explain shallow copy and deep copy give an example of shallow copy and deep copy

A shallow copy copies the I.D. from list one and has the values of list 1 and 2 the same, while deep copy changes the values of the lists. shallow: List 1 = List 2 Deep: import copy list1 = [ ] list2 = .deepcopy(list1)

Choose the best description for an algorithm: An algorithm is a plan or a recipe for logic that solves a problem An algorithm is an AI code generator An algorithm is just another word for a problem

An algorithm is a plan or a recipe for logic that solves a problem

What is printed by the following code? myName = "beth allen" myName = myName.capitalize() beth allen Beth allen Beth Allen BETH ALLEN

Beth allen

Match each component of the computer with its description. CPU ALU RAM Secondary Memory used to communicate with other computers the system bus on the motherboard disk drives volatile memory performs arithmetic and logical calculations the processor, or computational heart of a computer

CPU-the processor, or computational heart of a computer ALU-performs arithmetic and logical calculations RAM-Volatile Memory Secondary Memory-Disk Drives

Consider the Python function range(x, y). True or False: The value y is included in the range. True False

False

Developing a good algorithm is just a waste of valuable time True False

False

Evaluate the conditional expression: True and False and True True False

False

Given the following code segment, evaluate the boolean expression: b = "45" txt1 = "bet" txt2 = "betty" txt1 >= txt2 True False

False

Given the following variable assignments, evaluate the conditional expression. num = 72 x = 1.2 y = -3.4 letter = 'N' 0 < y < x True False

False

Given the following variable assignments, evaluate the conditional expression. num = 72 x = 1.2 y = -3.4 letter = 'N' letter == 'Y' or letter == 'n' True False

False

Given the following variable assignments, evaluate the conditional expression. num = 72 x = 1.2 y = -3.4 letter = 'N'flag = True num - num < y + x or not flag True False

False

It is not possible to write a function that has no parameters. True False

False

Lists are immutable True False

False

The following expression will change the first letter in the string myText into the letter Q. myText = "Hello" myText[0] = 'Q' True False

False

While loops and for loops are designed to be interchangeable in Python and you can solve all of the same problems with either of them. True False

False

What will the following code segment achieve, assuming that the variable values is a list with at least one element in it? list.reverse() x = list.pop() list.reverse() print(x) It prints the last value in the original list, and does not alter the contents of the list It prints the last value in the original list, and removes that value from the list It prints the first value in the original list, and does not alter the contents of list It prints the first value in the original list, and removes that value form the list

It prints the first value in the original list, and removes that value form the list

Match the file open access description string to its meaning "r" "a" "w" opens a file for reading opens a file for both reading and writing opens a new file for writing but does not erase the contents of the file opens a file for writing and erases any existing content in that file

R-opens a file for reading A-opens a new file for writing but does not erase the contents of the file W-opens a file for writing and erases any existing content in that file

What does it mean for an algorithm to be general purpose? The algorithm solves the general form of a problem so it can be used in other problems The algorithm solves every problem The algorithm is vague None of these answers are correct

The algorithm solves the general form of a problem so it can be used in other problems

Consider the Python function range(x, y). True or False: The value x is included in the range. True False

True

Dictionaries are similar to lists but instead of an index, you may use a key to look up a particular entry in it True False

True

Given the following variable assignments, evaluate the conditional expression. num = 72 x = 1.2 y = -3.4 letter = 'N' not num == 73 True False

True

Given the following variable assignments, evaluate the conditional expression. num = 72 x = 1.2 y = -3.4 letter = 'N' num < x or x > y True False

True

It is possible to write a function that has no return statement. True False

True

Given the following number line, showing the possible values for X (it's range(s)) write a conditional expression that will evaluate to True if the current value for X is in range. Closed dot on 1 and Closed dot from 5-7 X == 1 or X == 5 or X == 7 X == 1 and (X >=5 and X <= 7) X == 1 or (X >=5 and X <= 7) none of these answers are correct

X == 1 or (X >=5 and X <= 7)

What is printed by the following code segment? test1 = 74.19 test2 = 76.30 sum = test1 + test2 if sum > 200.0 : category = 'A' else : if sum < 100.0 : category = 'B' else : category = 'C' print("Your sum is ", sum) print("Category is ", category) nothing is printed Your sum is 150.49 Your sum is 150.49 Category is C Category is A

Your sum is 150.49 Category is C

The values passed into a function when it is called (or invoked) are called: parameters default parameters actual parameters formal parameters

actual parameters

Re-write the following code segment using a while loop instead of a for loop for alpha in range(1,27) : beta = beta + 2*alpha print(beta)

alpha = 1 beta = 0 while alpha <=27: beta = beta + 2*alpha print(beta) alpha += 1

Which of the following are valid variable names in python? True 1x beth "text123" text123 sum_total _sum_

beth, text123, sum_total, _sum_

The following statement creates a new dictionary that contains the capitol cities for some states. caps = { 'AL' : 'Montgomery' , 'MS' : 'Jackson' } Juneau is the capital of AK. Which statement adds it to our dictionary caps[ 'AK': 'Juneau' ] caps['AK'] = 'Juneau' caps{'AK'} = 'Juneau' caps.add('AK', 'Juneau)

caps['AK'] = 'Juneau'

For this: caps = { 'AL' : 'Montgomery' , 'MS' : 'Jackson' , 'AK' : 'Juneau' } remove the capitol of 'AK' del caps['AK'] del caps{'AK'} caps['AK'] = empty caps.delete('AK')

del caps['AK']

Write a code segment that uses a for loop to print out all values that are the multiples of 5 from 1000 through 2000 (please include 1000 and 2000 in the print out). Ex: 1000 1005 1010 ...

for _mult_of_5 in range(1000, 2001): #add 2001 to register 2000 if _mult_of_5 % 5 == 0: print(_mult_of_5)

The parameters that are described and named in the function's definition are called: parameters default parameters actual parameters formal parameters

formal parameters

Given the following table, write a python code segment (not a complete program) to print the required message. You may assume the variable named value already has a numeric value stored in it. value is in this range 0 through 10 (inclusive) 11 through 20 (inclusive) value contains any other number Print this message to the screen Entry-Level High-Level Out of Range

if value >= 0 and value <= 10: print("Entry-Level") elif value >= 11 and value <= 20: print("High-Level") else: print("Out of Range")

Write python code to accomplish the following: Prompt the user for an integer number. If the number is odd, print the message "odd", otherwise print "even."

integer_str = input("Please enter an integer: ") integer = int(integer_str) if input integer%2 == 0: print("Even") else: print("Odd")

There are 5280 feet in a mile. Write a python code segment to: prompt the user for a distance in miles and store it in a variable. compute the number of feet in the given number of miles. print out a message showing the mileage entered and the equivalent number of feet. For example, the running program might look like the following (the user's input is underlined/bold): You do not need to write comments for this question. Enter the number of miles: 1.5 1.5 miles is equivalent to 7920 feet.

miles_str = input('Enter the number of miles: ') miles = float(miles_str) feet = 5280 * miles print(,miles, 'miles is equivalent to', feet, 'feet.')

Which of the following is the correct order that the operators are applied in the following expression? (a == b - 1 and c < d) and, minus, ==, < minus, ==, <, and ==, <, and, minus ==, <, minus, and

minus, ==, <, and

Given the following code segment, select the statement that will start with the first character and copy every other character from myString to newString myString = "beth allen" newString = myString[:2] newString = myString[1:2] newString = myString/2 newString = myString[::2]

newString = myString[::2]

What is printed by the following code segment? test1 = 20.19 test2 = 5.00 sum = test1 + test2 if sum > 200.0 : category = 'A' else : if sum < 100.0 : category = 'B' else : category = 'C' print("Your sum is ", sum) print("Category is ", category) nothing is printed Your sum is 25.19 Your sum is 25.19Category is C Category is B

nothing is printed

Chose the statement that will display the last 6 letters in myString in reverse order. In other words, nohtyP should be printed. myString = "Welcome to Python" print( myString[-1:-6:-1] ) print( myString[12:18].reverse() ) print( myString[-1:-7:-1] ) print( myString[-1:-7:-1] ).reverse()

print( myString[-1:-7:-1] )

Write a function named is_negative . It takes a single parameter value, that is a number. It returns True if a number given as the only parameter is negative and should return False, otherwise. Also, show an example of using your function in a short code snippet. You may assume the number passed in as a parameter is a valid number.

print("Welcome to the if the number is negative") def is_negative(negative_number): #Definition for is_negative if negative_number < 0: #Use the if statement to see if the value is negative or positive with 2 different outcomes to return true or false return True else: return False #Code Snippet _number = 7 _answer = is_negative(_number) print(_number ,' is negative: ', _answer)

Suppose you have the following variable values. name = "Bob"age = 45payrate = 12.5 Write a single print statement that will display the example output based on the contents of the variables. Do NOT hard code in extra spaces and extra 0s. The same print statement should work even if Bob's name is longer or the pay rate is 100.25, for example. PLEASE NOTE: the - are ONLY used in the output to show you where the extra blanks would be in this print statement. There are 10 spots for the name value. And the numeric data is printed with two digits after the decimal point. Bob is 45 years old-------Bob earns $12.50 per hour

print('{:<10} is {} years old.\n{}, {} earns ${payrate: .2f} per hour'.format(name, age, ' ' * 10, name))

Choose the expression that will print only the last value in a list named values print(values[0]) print(values[]) print(value[-1]) none of the above

print(value[-1])

The following are some of the reasons why the ability to write functions is useful in programming languages. Match the term to its descrption. refactoring reuse complete does-one-thing Rewriting working code to break it into easier to understand functions Rewriting code that has bugs in it The function performs its task fully A function is focused on a single sub-problem A function solution can be used in another problem

refactoring Rewriting working code to break it into easier to understand functions does-one-thing A function is focused on a single sub-problem complete The function performs its task fully reuse A function solution can be used in another problem

Choose the correct Python statement that represents the following mathematical expression. You may assume all variables already exist. result = x + z-1/3 x + z - 1 / 3 result = x + z - 1 / 3 result = (x + z - 1) / 3 result = x + (z - 1) / 3

result = x + (z - 1) / 3

Choose the correct Python statement that represents the following mathematical expression. You may assume all variables already exist. Quadratic Formuler ARGARGARGARGARGAR (Mr. Krabs) root1 = -b + math.sqrt(b*2 - 4ac) / 2a root1 = -b + math.sqrt(b*b - 4*a*c) / (2*a) root1 = (-b + math.sqrt(b*b - 4*a*c)) / (2*a) none of these answers are correct

root1 = (-b + math.sqrt(b*b - 4*a*c)) / (2*a)

Given the following code segment, what value is stored in sum and what is the output? sum = 74.29 + 76.30 print("Your sum is {:.1f}".format(sum)) sum has the value 150.60. The following is printed:Your sum is 150.6 sum has the value 150.59. The following is printed:Your sum is 150.5 sum has the value 150.59. The following is printed:Your sum is 150.59 sum has the value 150.59. The following is printed:Your sum is 150.6

sum has the value 150.59. The following is printed:Your sum is 150.6

Which statement creates a new list named table that stores a new, empty list? table = new List(): table = [ ] table = { } table = empty

table = [ ]

Which statement creates a new dictionary named table that stores a new, empty dictionary? table = new Dictionary(): table = [ ] table = { } table = empty

table = { }

You have an existing input file named "myinputfile.txt". There are three lines of data in the file. The first line contains a person's name, followed by two lines, each with a real number. The first number is how many hours the person has worked and the second number is their hourly pay rate. For example: Beth Allen 40.08.5 Write a complete program to accomplish the following: Open the input file, read the data, and compute the total pay (pay = hours x payrate). Print the nicely formatted results to the screen (NOT TO A FILE). For the example data above, the output should look like this:Beth AllenHours Worked 40.0 Pay Rate $ 8.50Total Pay $340.00 Do not hardcode in data. Your program should work with any file containing data like the samples. You may assume that the data file given does exist and contains valid data. In other words, you do not have to do any error checking for this problem.

total_pay = 0 #Set all values equal to 0 at first so if no values are present nothing gets printed _hours = 0 _pay_rate = 0 file = open('inputfile.txt' , 'r') _workers_name = file.readline().strip() #Reads the files lines and strips the white spaces _hours = float(file.readline().stirp()) _pay_rate = float(file.readline().strip()) total_pay = _hours * _pay_rate print(_workers_name) print(_workers_name, "worked ", _hours, " with a pay rate of $", _pay_rate) print(

Below is a segment of code that takes a string named txtnumber, converts it to a float, and adds it to a sum. sum = 0sum = sum + float(txtnumber) Which of the following corrects the code to detect that txtnumber is not a numeric value and prevents the program from crashing? No changes need to be made if txtnumber is a number :sum = sum + float(txtnumber) try :sum = sum + float(txtnumber)except :print('Error in data') try :sum = sum + float(txtnumber)catch(error) :print('Error in data')

try :sum = sum + float(txtnumber) except :print('Error in data')


Kaugnay na mga set ng pag-aaral

missing Chapter 47: Assessment of Endocrine System

View Set

Varcarolis 8th Chapter 14: Depressive Disorders

View Set

Cellular respiration and fermentation

View Set

Coltons Server & Menu Knowledge test

View Set

Lesson 12 section 3 multiple choice quiz

View Set

Per Scholas Study Guide for 1 through 9

View Set

Renal and Acid Base Balance exam

View Set