CS 105 - Quiz 1

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

String literals can be surrounded with either single quotes (') or double quotes ("), but the starting and ending quote types must match. Create a single string literal for the following lines of text. You will need to use the escape sequence for the tab character. There should be no spaces in your answer. (feel free to cut and paste into your answer.) Make sure there are no spaces just inside your quotes. Elderberry Salmonberry Strawberry

"Elderberry\tSalmonberry\tStrawberry"

What is the value of the variable x after the following piece of code executes? x = "tebrxujzg"[-7]

"b" or 'b'

What is the value of variable x after the following piece of code executes? y = 'Banana' x = y y = 'Cantaloupe' y = x

'Banana'

The square root operator is not a built-in part of the Python language. Instead, it is part of the math library. To use the square root operator, you have to first import the math library (usually at the top of your program). After the following piece of code executes, what is the value contained in variable x? import math x = math.sqrt(100)

10 explanation: math.sqrt(100) = 10 because 10×10=100.

What value is printed when the following piece of code executes? x = 4 print(x + -2)

2

What is the value of the variable x after the following code executes? deltas = [3, -8, 12, 10, -12] deltas.remove(3) x = len(deltas)

4 explanation: The remove() function removes the first instance of a specified value from the list, making it one element shorter if the specified value is present.

How would you write an partially absolute reference for the cell B2, so that the row won't change no matter where you copied the reference, but the column would change normally. Write your answer in the box below. Don't write an equals sign.

B$2

All functions need to end with a return statement.

False explanation: Functions that end without return statements return a special value called None.

Which items are in the set my_fruits after the following code executes? some_fruits = {'Gooseberry', 'Cranberry', 'Marionberry'} more_fruits = {'Cranberry', 'Marionberry', 'Damson'} my_fruits = some_fruits.intersection(more_fruits)

Marionberry Cranberry

In the following Python statement, is the variable x being read, written, or both? y /= x

Read

Algorithm is basically a fancy word for 'recipe'.

True explanation: An algorithm, like a recipe, describes a series of steps for achieving a particular result. The design of algorithms is one of the most important facets of computer science.

The character sequence \n is used in Python output to move to the next line

True explanation: The character sequence \n is called a newline character and is treated by the Python language as a single character. For example, print('1 2 3') prints '1' on the first line, '2' on the second line, and '3' on the third line of output.

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

True explanation: The clear() function removes all of the elements from a set.

The last character of string s is at position len(s) - 1

True explanation: The indexing of a string in python starts from 0 to len(s) - 1

Every Python function returns some value.

True explanation: The value None is returned when no explicit value is provided.

Write a statement that removes the string 'ant' 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('ant')

Write a statement that creates a dictionary that associates the following cities with their 2010 population and assign it to a variable named city_populations. Northbrook 33170 Addison 36942

city_populations = {'Northbrook' : 33170, 'Addison' : 36942}

A Python function definition begins with:

def explanation: The functions in Python are defined by def function_name(): and the indentation block.

In the function below, return the last element (the one at the last index) of the list provided as the parameter called list1. Assume that the argument is a list.

def get_last_element(list1): return list1[-1]

Create a function (lowest_score) that takes in a list of scores and outputs the lowest score. Note: Your solution should used all blocks presented.

def lowest_score(scores): min_score = min(scores) print(f"The lowest score is {min_score}")

The following Python function prints the floating point number with exactly two decimal digits. Can you modify it to print out with exactly three decimal digits instead?

def print_number(number): format_string = f'My favorite number is{number:.3f}!!!' print(format_string.format(number)) explanation: .3f determines the decimal digits

The function below takes two arguments: a string (name) and an integer (position). Complete the function so that it prints out the a welcome statement that tells them person how many people are ahead of them in line. For example, your code should print out the following message if the person's name is Sally and their position is 24 (and hence have 23 people in front of them). Welcome Sally! There are 23 people ahead of you in line. Note: there are two spaces between the two sentences.

def print_welcome(name, position): name = input("Enter name: ") position = int(input("Enter position: ")) print("Welcome", name, "!", "There are", int(position - 1), "people ahead of you in line.")

A problem is intractable when:

it is not practical to solve explanation: From a computational complexity stance, intractable problems are problems for which there exist no efficient algorithms to solve them. A problem where an algorithm is known, but it would take longer than the time since the Big Bang to compute is an example of an intractable problem.

How do you determine the number of characters in the string s?

len(s) explanation: len() is the function used to calculate length of string or list.

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 explanation: To convert the number of gallons in liters, first convert gallons to quarts (gallons * 4.0, because 1 gallon = 4 quarts) and then convert quarts to liters by dividing (quarts / 1.05669, because 1 quart = 1 / 1.05669 liters)

The Distance Formula between two points can be solved for d with the following formula: d=√(x2−x1)^2+(y2−y1)^2 Where (x1,y1) and (x2,y2) are two points. Write a Python expression that corresponds to the above formula using the math.sqrt function and the variables x1, x2, y1 and y2.

math.sqrt((x2-x1)**2 + (y2 -y1)**2)

Write a statement that finds the index of the first instance of the value 'Papaya' in the existing list called out_of_stock. Put the result in a variable called value_index.

value_index = out_of_stock.index('Papaya', 0)


Ensembles d'études connexes

Questions on Assigned Articles Exam #2

View Set

Chapter 22 Nursing Management of the Postpartum Woman at Risk

View Set

Imperialism (overview, africa, india, china, japan)

View Set

MASTERING BIOLOGY-DNA REPLICATION

View Set

Bio 8e(Cmp-2) Q&A-Ch1 to Ch5 (Final)

View Set

Chemistry Hybridization and Bonding

View Set

Grade 12 Biology; Photosynthesis

View Set