9.19.2015 CSC108

Ace your homework & exams now with Quizwiz!

(a) 2.5 (b) 17.5 (c) 10

(b) After line 13 of this code has been executed, what value does credits_to_go refer to? 18 17.5 17 (c) After line 19 of this code has been executed, what value does completed refer to? 10 15 20

s = s(:5) + 'ed' + s[5:]

0 1 2 3 4 5 6 7 L e a r n t o How would I have to modify the S string here to change it to Learned to?

1 2/3 (function) So the return is 2 because modulo returns the numerator [1.1]

>>> 5 % 3 What is the return?

1 5/3 (function) 1 is the nearest whole number [1.1]

>>> 5 // 3 What is the return

return country

>>> def announce_location(country): # Missing function body >>> instructor_location = announce_location('Canada') >>> print(instructor_location) Canada Select the missing function body from the options below. (1) print('Canada') (2) return country (3) return instructor_location (4) print(country)

(3) print(country) return country

>>> def announce_location(country): # Missing function body >>> instructor_location = announce_location('Canada') Canada >>> print(instructor_location) Canada Select the missing function body from the options below. (1) return country print(country) (2) print(country) (3) print(country) return country (4) return country

(a) white_queen.count("jam").lower()

>>> white_queen = "Jam tomorrow and jam yesterday - but never jam today." >>> white_queen.count("jam") 2 The code above counts only lowercase occurrences of "jam". Below is the output of help(str.lower): lower(...) S.lower() -> str Return a copy of the string S converted to lowercase. Which expression produces the number of occurrences of "jam" ignoring letter case? (a) white_queen.count("jam").lower() (b) white_queen.lower().count("jam") (c) white_queen.count("Jam".lower())

def report_status(scheduled_time, estimated_time): ''' (number, number) -> str Return the flight status (on time, early, delayed for a flight that was scheduled to arrive at scheduled_time, but is now estimated to arrive at estimated_time. Pre-condition: 0.0 <= scheduled_time < 24 and 0.0 <= estimated_time < 24 >>> report_status (14.3, 14.3) 'on time' >>>> report_status (12.5, 11.5) 'early' >>> report_status (9.0, 9.5) 'delayed' if scheduled_time == estimated_time: return 'on time' elif scheduled_time > estimated_time: return 'early' else: return 'delayed'

A flight was scheduled to arrive at a particular time and it is now estimaed to arrive at another time. Write a function that reutnrs the flight's status on time, early, or delayed.

object.method(arguments)

A method is a function inside of an object. The general form of a method call is:

import triangle

A new module equilateral.py contains functions for equilateral triangles (triangles where all the sides have the same length). To use the functions from triangle.py in equilateral.py, which import statement should we use? import triangle import(triangle) import triangle.py import('triangle.py')

str

After the code above has been executed and the user types 12 followed by Enter/Return, what type of value does variable age refer to? [2.1.2]

dance_style[-4] dance_style[3]

After the following assignment statement has been executed, which expression(s) produce the letter "g"? dance_style = "Gangnam" dance_style[-5] dance_style[-4] dance_style[2] dance_style[4] dance_style[-3] dance_style[3]

(c) a contains memory address x1 and b contains memory adddress x5. [3]

Assume that value 4 has memory address x27, value 3.2 has memory address x5, and value 8 has memory address x1. Which of the following is true after this code is executed? (a) a refers to value 4 and b refers to value 3.2. (b) a stores x27 and b contains x5. (c) a contains memory address x1 and b contains memory adddress x5. (d) a refers to the same value as b.

(b) s = s[:-5] + 'Perhaps' (c) s = s[0:-5] + 'Perhaps'

Consider the following code: s = 'Call Me Maybe' Select the statement(s) that will result in s referring to 'Call Me Perhaps'. (a) s[-5:] = 'Perhaps' (b) s = s[:-5] + 'Perhaps' (c) s = s[0:-5] + 'Perhaps' (d) s = s - s[-5:] + 'Perhaps'

45.34

Consider this code: round(45.342, 2) What value does the expression above produce? 45.342 45.34 45.3 45

4

Consider this code: x = 12 // 3 What value does x refer to? 0.3 1 4 4.0

print(str(int(bro) + int(sis)) + sibs)

Consider this code: >>> bro = input("How many brothers do you have? ") How many brothers do you have? 4 >>> sis = input("How many sisters do you have? ") How many sisters do you have? 2 >>> sibs = ' siblings!' Assuming the code above has been executed, choose the statement below that prints this: 6 siblings! print((bro + sis) + sibs) print(str(int(bro) + int(sis)) + sibs) print(str(int(bro + sis)) + sibs)

It's not so big.

Consider this code: def how_big(n): if n > 10: return "It's pretty big." else: return "It's not so big." print(how_big(5)) It's pretty big. It's not so big. It's pretty big. It's not so big.

(b) if temp == 22.5: return True else: return False (c) if temp == 22.5: return True elif temp != 22.5: return False (d) if temp == 22.5: return True return False

Consider this code: return temp == 22.5 Select the code fragment(s) that are equivalent to the one above. (a) if temp == 22.5: return True (b) if temp == 22.5: return True else: return False (c) if temp == 22.5: return True elif temp != 22.5: return False (d) if temp == 22.5: return True return False

robot.isupper() robot.isalnum()

Consider this code: robot = 'R2D2' Assuming the code above has been executed, select the expression(s) that produce True. robot.isupper() robot.isalpha() robot.isalnum() robot.isdigit()

to the value 0 and by the end of the function it refers to the number of vowels in s.

Create a function that will loop over each character that s refers to, in turn. The body of the loop is executed for each character, and when a character is a vowel, the if condition is True and the value that num_vowels refers to is increased by one. The variable num_vowels is an accumulator, because it accumulates information. It starts out referring ____________________________

\n new line \t tab \\ backlash \' single quote (') \" double quote (")

Escape sequence /n /t \\ \' \"

>>> 5 % 3 2 1 2/3 (function) So the return is 2 because modulo returns the numerator [1.1]

Example of %

>>> 5 // 3 1 [1.1]

Example of integer division

It doesn't say what the function returns. It doesn't mention the parameter by name

Here is an incomplete docstring for function count_vowels: def count_vowels(word): """(str) -> int Count the vowels in the string. """ Identify the problem(s) with the Description in the docstring above. It doesn't say what the function returns. It's too short. It doesn't mention the type of the parameter. It doesn't mention the parameter by name. [2.2.1]

integer: with decimal float: without decimal [1.1]

Integer and float division for python

* [1.1]

Multiplication for python

(1) temp2_celsius = convert_to_celsius(temp2) return min(temp1, temp2_celsius)

Recall function convert_to_celsius from the Design Recipe lecture. That function has one parameter representing a temperature in Fahrenheit and returns that temperature in Celsius. In the same file, we define another function: def colder_temperature(temp1, temp2): """(number, number) -> number Return the colder of the two temperatures, temp1 (degrees Celsius) and temp2 (degrees Fahrenheit), in degrees Celsius. """ Which of the following is the best choice for the function body? (1) temp2_celsius = convert_to_celsius(temp2) return min(temp1, temp2_celsius) (2) temp2_celsius = (temp2 - 32) * 5 / 9 return min(temp1, temp2_celsius)

"yes no" 'yes no'

Select the code fragment(s) that result in a SyntaxError. (a) 'yes\nno' (b) "yes no" (c) '''yes no''' (d) 'yes no'

happy_day happy_45day

Select the legal Python name(s) below. happy_day happy!day? 18happy_day happy_45day

(b) 3 (type int)

The Python expression 9 / 3 produces the value 3.0, which has type float. For the expression 9 // 3, what value is produced and what type does it have? (a)3.0 (type float) (b) 3 (type int)

(3) print('yesterday\ntoday\ntomorrow') (4) print('''yesterday today tomorrow''')

The following is printed by a print function call: yesterday today tomorrow (1) print('''yesterday \ntoday \ntomorrow''') (2) print('yesterday today tomorrow') (3) print('yesterday\ntoday\ntomorrow') (4) print('''yesterday today tomorrow''')

math.ceil(84.2)

The math module has a function that finds the ceiling of a number (the smallest int value greater or equal to the number). Assuming that the math module has already been imported, write an expression that calls the ceiling function from math to find the ceiling of 84.2. Hint: In the Python shell, import math and then use dir and help on the math module to determine the name of the function that you need to use. math.ceiling(84.2) ceiling(84.2) math.ceil(84.2) ceil(84.2)

(d) longer(get_capital(country1), get_capital(country2))

Two function definitions are saved in the same file: A function get_capital has one str parameter that represents a country and returns its capital. A function longer has two str parameters and returns the longer of the two strs. Variables country1 and country2 refer to str values. Select the expression that produces the longer of the capitals of country1 and country2. (Note: the expression must include calls on both get_capital and longer. (a) max(get_capital(country1), (b) get_capital(country2)) (c) get_capital(longer(country1, country2)) (d) longer(get_capital(country1), get_capital(country2))

cents < 8 or cents > 3 not cents < 12 not not cents >= 33

Variable cents refers to the value 34. Select the expression(s) that produce True. cents < 10 or cents > 100 not cents * 2 > 34 cents < 8 or cents > 3 not cents < 12 not not cents >= 33 cents == 34 and cents < 20

(b) 8 >= dollars (d) dollars == 8.0

Variable dollars refers to the value 8. Select the expression(s) that produce True. (a) 8 = dollars (b) 8 >= dollars (c) dollars != 8 (d) dollars == 8.0

(a) if grade1 >= 50: num_passed = num_passed + 1 if grade2 >= 50: num_passed = num_passed + 1 (d) if grade1 >= 50 and grade2 >= 50: num_passed = 2 elif grade1 >= 50: num_passed = 1 elif grade2 >= 50: num_passed = 1

Variables grade1 and grade2 represent grades in two courses. Variable num_passed currently refers to 0. Select the code fragment(s) that make num_passed refer to the number of courses passed (with a 50 or higher). Watch your ifs and elifs! (a) if grade1 >= 50: num_passed = num_passed + 1 if grade2 >= 50: num_passed = num_passed + 1 (b) if grade1 >= 50: num_passed = num_passed + 1 elif grade2 >= 50: num_passed = num_passed + 1 (c) if grade1 >= 50 and grade2 >= 50: num_passed = 2 if grade1 >= 50: num_passed = 1 if grade2 >= 50: num_passed = 1 (d) if grade1 >= 50 and grade2 >= 50: num_passed = 2 elif grade1 >= 50: num_passed = 1 elif grade2 >= 50: num_passed = 1

>>> pow(2,5) 32 Same as ** [3.3]

What does the POW function do?

area = math.sqrt(semi * (semi - side1) * (semi - side2) * (semi - side3)

What is missing here?

(int) -> str, str (str, str) -> int

What is the Type Contract for the following function definition? def total_vowels(word1, word2): """ Return the number of vowels in words word1 and word2. >>> total_vowels('hello', 'hi') 3 """ (str, str) -> float str == int (int) -> str, str (str, str) -> int

45.0

What value does the expression float(str(45)) produce? 45 45.0 '45.0' An error occurs.

true

What value does the expression str(int('99')) == '99' produce? '99' False True An error occurs.

1 (type int)

When the expression 10 % 3 is evaluated, what value is produced and what type does that value have? 1 (type int) 0 (type int) 1.0 (type float) 0.3333333333333333 (type float)

2 (type int)

When the expression 10 // 4 is evaluated, what value is produced and what type does that value have? 2 (type int) 2.0 (type float) 2.5 (type float)

(a) def double(x): return 2 * x [4.1]

Which of the following is an appropriate definition for the function double that returns two times the number it is passed? (a) def double(x): return 2 * x (b) double(x) = return 2 * x (c) double(x) = 2 * x (d) def double(x): 2 * x

def is_even(num) ''' (int) -> bool return num % 2 == 0

Without using and "if" and "else" write a program that will return true whenever a number is even and false when it is not even

Def square_return (num): Return num **2 def square_print (num): print ("The square of num is ", num **2) answer_print = square_print(4)

Write in python the code to print back the sum of square is number ** 2. (3 lines) [2.1.2]

to join together [2.1]

concatenate

an argument

data = 3 data2 = 7.5 result = min(data, data2) Select the phrase that describes data in the third line. a parameter an argument a function name

(1) a parameter

def f(data): return data * 0.5 Select the phrase that describes data. (1) a parameter (2) an argument (3) a function name

(d) string1 and string2 have the same length

def hamming(string1, string2): """ (str, str) -> int Return the hamming distance between string1 and string2. Precondition: _________________ >>> hamming("cab", "bat") 2 >>> hamming("badger", "badger") 0 """ (a) string1 and string2 are of type str. (b) string1 and string2 are not the same. (c) string1 and string2 have different lengths. (d) string1 and string2 have the same length

(b) It's huge.

def howbig(n): if n > 100: return "It's huge." elif n > 10: return "It's pretty big." else: return "It's not so big." print(howbig(150)) hen the code above is executed, what is printed? (a) It's pretty big. (b) It's huge. (c) It's huge. It's pretty big. (d) It's huge. It's pretty big. It's not so big.

(number) -> str

def is_passing_grade(grade): """ Return 'pass' if grade is at least 50 and return 'fail' otherwise. >>> is_passing_grade(45) 'fail' >>> is_passing_grade(80.5) 'pass' """ (int) -> str (int, float) -> str (number) -> str (float) -> str

def number_of_cents(change): """ (float) -> int >>> number_of_cents(1.25) 25 >>> number_of_cents(20.00) 0 """ The number of cents in the change returned with the input of the amount of dollar dollar_remainder = change % 1 cents = dollar_remainder * 100 return round(cents)

def number_of_cents(change): """ >>> number_of_cents(1.25) 25 >>> number_of_cents(20.00) 0 """ dollar_remainder = change % 1 cents = dollar_remainder * 100 return round(cents) (a) Add the missing Type Contract. (b) The description of this function is poor: The number of pennies left when you take away all the dollars from an amount of money. Write a better function description.

4

def sum(number1, number2): print(number1 + number2) result = sum(1, 3)

(a) TypeError

def sum(number1, number2): print(number1 + number2) result = sum(1, 3) new_result = result + 1 (a) TypeError (b) new_result refers to the value 5 [2.1.3]

hello

def sum(number1, number2): return number1 + number2 print("hello") result = sum(1, 3)

Nothing. The function exits when the return statement is executed, so the print function call is never reached.

def sum(number1, number2): return number1 + number2 print("hello") result = sum(1, 3) What is printed by the code above?

All even positive integers

define parameter, def, and return [1.4]

(a) if 18 < temp and temp < 26: print("A comfortable temperature") print("Let's go for a swim") (c) if 18 < temp and temp < 26: print("A comfortable temperature") elif temp > 26: print("It's quite warm in here") (d) if temp < 0: print("It's toque weather!")

elect the syntactically correct if statement(s). (a) if 18 < temp and temp < 26: print("A comfortable temperature") print("Let's go for a swim") (b) if 18 < temp and temp < 26: print("A comfortable temperature") else: print("Be prepared for anything") elif temp < 18: print("Better bring a sweater") (c) if 18 < temp and temp < 26: print("A comfortable temperature") elif temp > 26: print("It's quite warm in here") (d) if temp < 0: print("It's toque weather!")

** [1.1]

exponentiation for python

return not eggs % 12 == 0 return eggs % 12 != 0

if eggs % 12 == 0: return False else: return True Select the statement(s) that are equivalent to the code fragment above. return eggs % 12 == 0 return not eggs % 12 == 0 return eggs % 12 != 0 return not (eggs % 12 != 0)

lyrics.find('!', lyrics.find('!') + 1)

lyrics = '''O Canada! Our home and native land! True patriot love in all thy sons command.''' Select the expression that produces the index of the second exclamation mark.

s[12] s[-1]

s = 'Call Me Maybe' Select the expression(s) that produce 'e'. s[12] s[13] s[-0] s[-1]

s[-5:] s[4:9] s[4:len(s)]

s = 'pineapple' Select the expression(s) that produce 'apple'. s[4:9] s[-5:] s[-5:-1] s[5:9] s[4:len(s)] s[5:]

(c) wish[0].lower() + wish[1:6] + wish[6].lower() + wish[7:] (d) wish.lower()

wish = 'Happy Birthday' After the code above is executed, which of the following expressions produces 'happy birthday'? (a) wish[0].lower() + wish[6].lower() (b) wish.swapcase() (c) wish[0].lower() + wish[1:6] + wish[6].lower() + wish[7:] (d) wish.lower()


Related study sets

security plus exam final cts1120

View Set

Assignment: Chapter 14 Interviewing and Following Up

View Set