Review

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What will the output be after running the following code snippet? lst = ["oranges", "bananas", ""] lst.remove("oranges") print(lst)

["bananas", ""]

What list will be referenced by the variable list strip after the following code executes? my_string = '03/07/2018' list_strip = my_string.split(' / ')

['03', '07', '2018']

What will be output after running the following code? num = [1,2,3,4,5,6,7] print (nums [::-1])

[0,1,2,3,4,5,6]

The strip () method returns a copy of the string with all the leading whitespace characters removed but does not remove trailing whitespace characters.

False

What will be the value of the variable list after the following code executes? list = [1, 2, 3, 4] list [2] = 10

[1,2,3,4]

what will be the output of the following code? text="Mary bought dozen apples" print('oranges' in text)

False

What will be displayed after the following code executes? mystr = 'yes' yourstr = ' no' mystr += yourstr * 2 print(mystr)

yesnono

What will be assigned to the variable some _nums after the following code executes? special = '0123456789' some_nums = special [0:10:2]

'02468'

What will be the output after the following code is executed and the user enters 15 and - 3 at the first two prompts? def main (): try: total = int (input ("Enter total cost of items? ")) num_items = int(input("Number of items ")) print (total / num_items) except ZeroDivisionError: print('ERROR: cannot have O items') except ValueError: print(ERROR: number of items cannot be negative') main()

-5

What are the valid indexes for the string 'New York'?

0 through 7

What will the following program display? def main(): x = 10 y = 5 print(x, y, end=' ') change_us(x, y) print(x, y, end=' ') def change_us(x, y): x = 0 y= 0 print(x, y, end=' ') main()

10 5 0 1 0 5

What is the output of the following code: lst = [0,1,2]*4 print (len(lst))

12

What will be displayed after the following code is executed? def pass_it(x, y): z=x*y result = get_result (z) return(result) def get result (number): z = number + 2 return(z) num1 = 4 num2 = 3 answer = pass_it(num1, num2) print(answer)

14

What will the output be after running the following code snippet? def tripler(num): return num * 6 def doubler(num): return num *2 print(tripler(3))

18

What will the output be after running the following code snippet? def myfun(num): if num >= 5: return num - 1 else: return num print(myfun(4))

4

What will be the output of the following code? greeting = "Good Evening" for char in greeting: if char == 'o': break print(char) else: print("Good Morning"')

G

What will be the output after the following code is executed? def pass_it(x, y): z =x, ", " , y num1 = 4 num2 = 8 answer = pass_it (num1, num2) print (answer)

None

What is the output of the following snippet of code: def func(num): while num > 0: num = num - 1 num=3 func(num)

Nothing is printed

What will the return value of the string method lstrip ()?

The string with all leading whitespaces removed

Different functions can have local variables with the same names.

True

Indexing works with both strings and lists

True

Given that the customer file references a file object, and the file was opened using the 'w' mode specifier, how would you write the string 'Mary Smith' to the file?

customer. write ('Mary Smith')

Which method would you use to determine whether a certain substring is present in a string?

find(substring)

What will the output be after executing the following code snippet? language = "Python" print (language [-1])

n

Which of the following will assign a random integer in the range of 1 through 50 to the variable number?

number = random.randit (1, 50 )

The _______ keyword is ignored by the Python interpreter and can be used as a placeholder for code that will be written later.

pass

What will be assigned to the variable _string after the following code executes? special = '1357 Country Ln.' s_string = special [4:]

'Country Ln. '

Which mode specifier will erase the contents of a file if it already exists and create the file if it does NOT already exist?

'w'

What will be assigned to the string variable pattern after the following code executes? i= 3 pattern = 'z' * (5 * i)

'zzzzzzzzzzzzzzz'

What will the output be after executing the following code? Tupl = ['List', 'Tuple'] print(tuple (Tupl))

('List', 'Tuple')

What will be output of the following program? a = 'Python' i = 0 while i < len(a): i+= 1 print(i)

6

What will the output be after calling the following function? def sum(a,b): return a * b return a + b print (sum(20,3))

60

What will display after the following code is executed? def main(): print(magic(3)) def magic(num): answer = num + 12/3 return answer main()

7

What will be the output after the following code is executed? def pass_it(x, y): z=y**x return(z) num2 = 3 num1 = 2 answer = pass_it(num1, num2) print(answer)

9

What is the output of the following code? num = 'a', print (type(num))

<class 'tuple'>

What will the output be, if we run the following code? weekdays = ("Mon", "Tue", "Wed", "Thu") weekdays.append("Fri") print (weekdays)

AttributeError: 'tuple' object has no attribute 'append'

What will be the output after the following code is executed and the user enters 10 and 0 at the first two prompts? def main(): try: total = int(input("Enter total cost of items? ")) num items = int(input ("Number of items ")) average = total / num items except ZeroDivisionError: print ('ERROR: cannot have 0 items") except ValueError: print ('ERROR: number of items cannot be negative') main ()

ERROR: cannot have 0 items

A function definition specifies what a function does and causes the function to execute.

False

The following code will display 'yes + no': mystr = 'yes' yourstr = 'no' mystr += yourstr print(mystr)

False

The following expression is valid: string[i] = 'i'

False

The index of the first element in a list is 1, the index of the second element is 2, and so forth.

False

What will display after the following code executes? password = 'ILOVEPYTHON' if password.isalpha(): print ('Invalid, must contain one number. ') elif password.isdigit(): print('Invalid, must have one non-numeric character. ') elif password.isupper (): print ('Invalid, cannot be all uppercase characters. ') else: print('Your password is secure!')

Invalid, must contain one number

What will be the output after the following code is executed? def pass_it(x, y) Z = x + v return(z) name2 = "Julian" namel = "Smith" fullname = pass_it (name2, name1) print (fullname)

JulianSmith

In slicing, if the end index is not specified, Python will use the length of the list instead.

True

Negative indexes to access list elements are allowed in Python.

True

One reason not to use global variables is that it makes a program hard to debug

True

Strings can be written directly to a file with the write method but numbers must be converted to strings before the can be written.

True

What will the output be after running the following code snippet? lst1 = [1, 2, 1, 2] lst2= [1,2] *2 print (lst1 == lst2)

True

What will the output be after running the following code? def oddoreven(num): if (num % 2 == 1): print('YES') else: print("NO") oddoreven(9)

YES

What values will list2 contain after the following code executes? list1 = [1, 10, 3, 6] list2 = [item * 2 for item in list1 if item >5]

[20,12]

What will be the output after executing the following code? def put(x): return [5] val = [0, 1, 2, 3, 4] y = put(val): print(y)

[5]

What will the output be, if we run the following code? tup1 = ('a', 'b') tup2 = ('c', 'd') for x in tup1: for y in tup2: print(x,y)

a c a d b c b d

Which statement can be used to handle some of the runtime errors in a program?

a try /except statement

Which of the following is the correct way to open a file named users.txt in 'r' mode?

infile = open ('users.txt', 'r')

The primary difference between a tuple and a list is that

once a tuple is created, it cannot be changed


Set pelajaran terkait

Ch. 9 The Central Nervous System

View Set

Bacterial Skin Infections: Impetigo, Furuncles and Carbuncles

View Set

فيزياء-الفصل ثاني + ثالث

View Set

Module 2 - OOP Core Concepts and Recursion

View Set

Understanding Animal Biology: Unit 9

View Set

PED Final Exam Course point ?'s Unit 2

View Set