Python test 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

What will be the value of the variable list2 after the following code executes? list1 = [1, 2, 3] list2 = [] for element in list1: list2,append(element) list1 = [4,5,6]

[1, 2, 3] The 'for' loop iterates over the elements of 'list1' and for each element, it appends the element to 'list2' using the append method.

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

[1,2,3,10]

The pass keyword is used to pass arguments to a function

False

The remove method removes all occurrences of an item from a list.

False

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'

Which mode specifier will open a file but not let you change the file or write to it?

'r'

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

('Python' , 'Tuple') (should have parentheses and ' ' for string)

What will be the output of the following code? tup1=(3,5,7) tup2=(4,6) tup1+=tup2 print(tup1)

(3, 5, 7, 4, 6) Tuples are immutable but you can concatenate tuples to create a new one

What will the output be if we run the following code? lst1 = [0,1] lst2= [1,0] for x in lst1: for y in lst2: print(x,y)

0 1 0 0 1 1 1 0

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

1 3.4 0 0 1 3.4

What will the output be after running the following code? def swap(x,y): z=x; x=y; y=z; def main(): x=5 y=10 swap(x,y) print(x,y, end= ' ') main()

10 5 (Bc the function 'swap(x,y)' swaps the values of the input parameters 'x' and 'y' using a temporary variable 'z'. The function does not return any value, so the original values of 'x' and 'y' in the 'main()' function are not affected.)

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

12 (You multiply the elements which is 3 by 4 times= 12)

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

3

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

6 (the 2nd return statement a+b will not be executed bc the first return statement will terminate the function and return its value)

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

<class 'tuple'> (The comma after 2 is necessary to show that the value is a 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' -Tuples cannot be modified. The parentheses and " " indicate it is a tuple.

What will be the output after the following code is executed and the user enters 75 and 0 as the first 2 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

A local variable can be accessed from anywhere in the program

False

Similar to other languages, in Python the number of values a function can return is limited to one

False

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

False

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

NO ('num' checks whether the input is divisible by 2 using the % operator. If the remainder of 'num' divided by 2 is 0, then the function prints 'YES', otherwise it prints 'NO')

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

None (because there is no return after passing)

islower()

Returns true if all of the alphabetic letters in the string are lowercase, and the string contains at least one alphabetic letter. Returns false otherwise

isupper()

Returns true if all of the alphabetic letters in the string are uppercase, and the string contains at least one alphabetic letter. Returns false otherwise.

isalpha()

Returns true if the string contains only alphabetic letters and is at least onecharacter in length. Returns false otherwise.

isalnum()

Returns true if the string contains only alphabetic letters or digits and is at least onecharacter in length. Returns false otherwise.

isdigit()

Returns true if the string contains only numeric digits and is at least one character in length. Returns false otherwise.

isspace()

Returns true if the string contains only whitespace characters and is at least one character in length. Returns false otherwise. (Whitespace characters are spaces, newlines (\n), and tabs (\t).

What does the following statement mean? get_num()

The function get_num() is expected to return a value for num1 and for num2 (get_num() returns 2 values)

An exception handler is a piece of code that is written using the try/except statement.

True

Different functions can have local variables with the same names

True

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

True

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

True

Python allows you to pass multiple arguments to a function

True

Python function names follow the same rules as those for naming variables

True

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

True

The index -1 identifies the last element in a list.

True

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

[ 1, 2, 1, 2, 1, 2]

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 ] (first check if number is less than 5 THEN multiply)

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

[4]

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

[6, 5, 4, 3, 2, 1] the slicing syntax [::-1] reverses the order of the elements

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 of the following statements causes the interpreter to load the contents of the random module into memory?

import random

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

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

A ______ variable is created inside a function

local

Examples of splitting a string

my_string = 'One two three four' word_list = my_string.split() word_list ['One', 'two', 'three', 'four'] my_string = '1/2/3/4/5' number_list = my_string.split('/') number_list ['1', '2', '3', '4', '5']

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

nothing is printed

What will be the output after the following code is executed and the user enters 75 and -5 as the first 2 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()

nothing is printed

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

number=random.randint(1,50)

The primary difference between a tuple and a list is that

once a tuple is created, it cannot be changed

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

pass

Which method could be used to strip specific characters from the end of a string?

rstrip

Which method could be used to convert a numeric value to a string?

str

Which method can be used to convert a list to a tuple?

tuple


Conjuntos de estudio relacionados

Capitulo 14 Barberia Corte de Cabello Y Peinado Para Hombres

View Set

Life and Health Insurance: Chapter 3

View Set

NUR 240 PrepU Chapter 47: Assessment of Kidney and Urinary Function

View Set

Business Skills for Technical Professionals, Chapter 7, Minimizing Stress and Avoiding Burnout, Chapter 9, Solving and Preventing Incidents and Problems - Chapter 6, Handling Difficult Customer Situations - Chapter 5

View Set

ATI Scope and Standards of Practice

View Set