cs 105

Ace your homework & exams now with Quizwiz!

The part of a program that uses a function is called the: (a) caller (b) statement (c) user (d) callee

(a) caller The function being called is referred to as thecalleeand the function that calls it is called thecaller.

What variable names does a function have access to? (a) Only the parameters passed to the function. (b) All variables used in a program. (c) Only variables created in the function. (d) Parameters that are passed to the function and any variables created in the function.

Parameters that are passed to the function and any variables created in the function.

What does the Python expression 3 * "A" evaluate to?

"AAA"

How do you determine which actual parameter to assign to each formal parameter?

The order of the parameters indicates how they are assigned.

The range function produces ranges that count from a low number to a high number.

false

What is the value of x after the loop completes? x = 0 for value in ['hamster', 'pug' , 'ferret', 'buffalo','worm', 'jackal', 'alligator']: if len(value) > x: x = len(value)

9

A Python while implements a definite loop.

False

A function's scope is the number of lines of code in that function.

False

A two-way decision is implemented using an if-elif statement.

False

A while True:loop will never end. (t/f)

False

After the statement x = 'hi!' is executed we would say that the variable x is a string literal

False

Anything implemented in a while loop can be implemented in as a for loop.

False

Does this boolean expression evaluate to True or False? False or False and True

False

Good practice suggests using tabs to indent if statements and spaces to indent functions (True/False)

False

In mathematical expressions, Python requires spaces between numbers and operators (e.g., +, -, *, /).

False

When mutable types are passed as arguments to a function, the value as seen by the code making the function call is guaranteed not to change

False

if statements can be nested (i.e., put inside the body of another if statement) up to four levels deep.

False

Python is a statically-typed language.

False (In a statically-typed language, the types of variables are determined when the program is written. Python is a dynamically-typed language.)

A simple decision can be implemented with an if statement.

True

All of the elements of a Python set can be removed in a single Python statement.

True

Arithmetic operators evaluate before relational operators.

True

Dynamically-typed languages are some times referred to as using duck typing. (true/false)

True

In Python, the body of an if statement uses indentation to specify its boundaries just like functions

True

Information can be passed into a function through parameters.

True

Programmers often implement functions to avoid having multiple copies of similar code.

True

The creation of functions in the global scope is generally discouraged.

True

The range function can be called with one, two, or three arguments.

True

While loops are used in situations when the loop exit is hard to predict, like interactive sessions with human users.

True

break statements are considered undesirable and, all things being equal, should be avoided.

True

The counted loop pattern uses a definite loop.

True (The counted loop pattern is where we write a loop that counts from one number to another number. A definite loop (one logical classification of loops) is one where we know before the loop executes how many iterations it will perform, which a counted loop is.)

Write a string list that could be passed to this function to get it to return the string "If you're offered a seat on a rocket ship, don't ask cloudberry seat! Just get on.". replace what with cloudberry

['what', 'cloudberry']

Write a statement that removes the string 'cattle' from an existing list called animals_to_feature. You can assume that the item is present and that it exists in the list only once.

animals_to_feature.remove('cattle')

The Python input function: (a) both displays text on the screen and allows someone using your program to enter value from the keyboard (b) only allows someone using your program to enter a value from the keyboard (c) only displays text on the screen (d) none of the other answers are correct.

both displays text on the screen and allows someone using your program to enter value from the keyboard

Define a function below called average_strings. The function takes one argument: a list of strings. Complete the function so that it returns the average length of the strings in the list. An empty list should have an average of zero. Hint: don't forget that in order to get a string's length, you need the len function.

def average_strings(list_strings): total = 0 for x in list_strings: total += len(x) if total ==0: return total else: return total/len(list_strings)

The function below takes a string arguments, input_string. Complete the function so that it returns True if input_string is a palindrome. Otherwise, return False.

def check_palindrome(input_string): if input_string== input_string[::-1]: return True else: return False

The function below takes one parameter: an integer (begin). Complete the function so that it prints every other number starting at begin down to and including 0, each on a separate line. There are two recommended approaches for this: (1) use a for loop over a range statement with a negative step value, or (2) use a while loop, printing and decrementing the value each time.

def countdown_trigger(begin): for i in range(begin,-1,-2): print(i)

The function below takes one non-negative numeric parameter: (credit_hours_earned) containing the number of credit hours that a student earned. Complete the function to return a string indicating the student's classification as indicated by the table below. For example, for 36 credit hours, your function should return 'Sophomore standing'. Freshman standing = 0-29.9 hoursSophomore standing = 30-59.9 hoursJunior standing = 60-89.9 hoursSenior standing = 90 or more hours

def determine_standing(credit_hours_earned): if credit_hours_earned >= 0 and credit_hours_earned <= 29.9: return "Freshman standing" elif credit_hours_earned >= 30 and credit_hours_earned <= 59.9: return "Sophomore standing" elif credit_hours_earned >= 60 and credit_hours_earned <= 89.9: return "Junior standing" elif credit_hours_earned >= 90: return "Senior standing"

Define a function below, sublist_of_first_three, which takes one parameter: a list. Complete the function to create a new list containing the first three elements of the given list and return it. You can assume that the provided list always has at least three elements. This can be implemented simply by creating (and returning) a list whose elements are the values at the zero'th, first and second indices of the given list. Alternatively, you can use the list slicing notation.

def first_three(argument_list): first = argument_list[0] second = argument_list[1] third = argument_list[2] return [first, second, third]

The function below takes a list of strings, shopping_cart, and a dictionary with strings as keys and floating points as values, price_lookup, as parameters. Use a loop to iterate over the values in shopping cart to lookup the prices in price_lookup and append them to a new list. Finally, return the new list as if it were a receipt for all the items in the shopping cart.

def get_receipt(shopping_cart, price_lookup): new_list = [] for values in shopping_cart: new_list.append(price_lookup.get(values)) return new_list

Create a function called is_list_empty that takes a single argument of type list. Your function should return True if the list is empty (i.e., has no elements). Otherwise, return False.

def is_list_empty(list_1): length = len(list_1) if (length == 0): return True else: return False

Complete the function below to return True if the input, input_number, is odd. Otherwise, return False.

def is_odd(input_number): if input_number % 2 == 0: return False else: return True

The function list_combination takes two lists as arguments, list_one and list_two. Return from the function the combination of both lists - that is, a single list such that it alternately takes elements from both lists starting from list_one. You can assume that both list_one and list_two will be of equal lengths. As an example if list_one = [1,3,5] and if list_two = [2,4,6]. The function must return [1,2,3,4,5,6].

def list_combination(list_one, list_two): result = [] for i in range(len(list_one)): result.append(list_one[i]) result.append(list_two[i]) return result

The function below takes a single string parameter: input_list. If the input list contains the lowercase letter z, return the string 'has the letter z'. Otherwise, return the string 'no z found'.

def list_contains(input_list): if 'z' in input_list: return 'has the letter z' else: return 'no z found'

Complete the function below to return True if the input, input_number, is negative or even. Otherwise, return False.

def negative_even_check(input_number): if input_number < 0: return True elif input_number % 2 == 0: return True else: return False

Define a function below, posodd_number_filter, which takes a single numerical argument. Complete the function to return True if the input is positive and odd. Otherwise, return False.

def posodd_number_filter(input_1): if input_1 > 0 and input_1 % 2 ==1: return True else: return False

The function below takes one parameter: a dictionary (dictionary). Complete the function so that it prints out each of the given dictionary's keys, each on a separate line. Recall that dictionaries store key:value pairs and that when you iterate through a dictionary with a for loop, you are iterating through the keys.

def print_dict_keys(dictionary): for value in dictionary: print(value)

The function below takes one parameter: a list of strings (string_list). Complete the function so that it prints out each string on a separate line.

def print_list_elements(string_list): for str in string_list: print (str)

The function below takes one parameter: a list of strings (string_list). Complete the function to compute the total combined length of all of the strings in the list The recommended approach for this: (1) create a variable to hold the current sum and initialize it to zero, (2) use a for loop to process each element of the list, adding its length to your current sum, (3) return the sum at the end.

def sum_lengths(string_list): result = 0 for str in string_list: result = result + len(str) return result

def f(x,y): return x.index(y)

finds the index of the first element of a list that matches the given value

A loop pattern that asks the user whether to continue on each iteration is called a(n)

interactive loop

If you want to convert from gallons to liters, which of the following expressions is correct given that one liter is 1.05669 quarts and there are four quarts in a gallon? liters = gallons / 4.0 * 1.05669 (b) gallons = liters * 1.05669 / 4.0 (c) liters = gallons * 4.0 * 1.05669 (d) liters = gallons * 4.0 / 1.05669

liters = gallons * 4.0 / 1.05669

A function can modify the value of a parameter only if it's (a) mutable (b) passed by reference (c) a variable (d) list

mutable

Write a statement that creates a single string from an existing list of strings called state_strings by inserting the string ' | ' between every pair of strings in the list. The resulting string should be put into a variable called output_str.

output_str = (' | ').join(state_strings)

A function can send output back to the program with a(n)

return

Write a statement that slices 4 characters of the string heading starting 5 characters from the beginning. Put this value into a variable named selection. For example, if the heading variable is bound to the string 'abcdefghijklmnopqrstuv', then selection should be assigned the value 'fghi'.

selection = heading[5:9]

Write a statement that slices a substring out of the string quote and puts it into a variable named selection. If given the string 'The only impossible journey is the one you never begin.', selection should contain 'possible jo' after your statement executes.

selection = quote[11:22]

All sequence types are zero indexed.

true

One reason to use functions is to reduce code duplication (true/false)

true

Write a statement that adds the float 7.51 to the end of an existing list called volumes_in_liters.

volumes_in_liters.append(float(7.51))


Related study sets

vSim for Nursing: Fundamentals: Vernon Russell

View Set

Chapter 21: Antidepressant Agents

View Set

Match each definition to the correct term:

View Set

Chapter 39: Pediatric Variations of Nursing Interventions Perry: Maternal Child Nursing Care, 6th Edition

View Set

Chapter 11: Long-Term Debt Financing

View Set