ITP-150 Python Exam 2(Electric Boogaloo)

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

What will be output for print(f'{1.6180339:.5f}')?

'1.61803'

What is the value of new_title? title = 'Python for Beginners' tokens = title.split(' ') if 'Chapter 1' not in tokens: tokens.append('Chapter 1') new_title = ' '.join(tokens)

'Python for Beginners Chapter 1'

For print(f'{"Mike":XXX}{1:YYY}'), which XXX and YYY will generate 'Mike=1' as the value of format_print?

*>4, =>2

Which input value causes "Goodbye" to be output next? x = int(input()) while x >= 0: # Do something x = int(input()) print('Goodbye')

-1

What sequence is generated by range(4)?

0 1 2 3

What is output? mydict = {x: x*x for x in range(0, 5)} for data in mydict.values(): print(data, end=' ')

0 1 4 9 16

What is the output? for j in range(2): for k in range(4): if (k == 2): break print(f'{j}{k}', end=' ')

00 01 10 11

Given the following function. To change the function to return the product instead of the sum, how many lines of code need to be changed?

1

Given the following function. To change the function to return the product instead of the sum, how many lines of code need to be changed? def calculate(a, b): return a + b print(calculate(3, 4)) print(calculate(5, 2)) print(calculate(6, 7))

1

dict1 = {} dict1['a'] = 4 dict1['b'] = 5 dict1['c'] = 2 dict1['d'] = 1 dict1['e'] = 3 mylist = list(dict1.values()) sorted_mylist = sorted(mylist) for x in sorted_mylist: print(x, end=' ')

1 2 3 4 5

What is a valid output? dict1 = {} dict1['Kilometer'] = 'Meter' dict1['Litre'] = 'Millilitre' dict1['Kilojoule'] = 'Joule' dict1['Ton'] = 'Kilograms' dict3 = {} for keys, values in dict1.items(): dict3[keys] = values val = dict3.pop('Ton') for keys, values in dict3.items(): print(f'1 {keys} = 1000 {values}', end=' ') print('or', end=' ') print(f'1 {keys} is equal to 1000 {dict3[keys]}')

1 Kilometer = 1000 Meter or 1 Kilometer is equal to 1000 Meter 1 Litre = 1000 Millilitre or 1 Litre is equal to 1000 Millilitre 1 Kilojoule = 1000 Joule or 1 Kilojoule is equal to 1000 Joule

new_list = [10, 20, 30, 40] for i in new_list[:]: print(i) new_value = new_list.pop(0)

10 20 30 40

What is output? num = 10; while num <= 15: print(num, end=' ') if num == 12: break num += 1

10 11 12

What is the output? def find_sqr(a): t = a * a return t square = find_sqr(10) print(square)

100

What is the output? id_list = [13, 984, 231, 140] for id in id_list: if id != 231: print(id, end=' ') else: print('Done')

13 984 140 Done

What is the ending value of x? x = 0 i = 5 while i > 1: x = x + i i = i - 1

14

What is the output? x = 18 while x % 3 == 0: print(x, end=' ') x = x // 3

18 6

What is the length of the dictionary my_dict = {'Country':'India', 'State': {'City':'Delhi', 'Temperature':40}} ?

2

What is the output? LB_PER_KG = 2.2 def kgs_to_lbs(kilograms): pounds = kilograms * LB_PER_KG return pounds pounds = kgs_to_lbs(10) print(pounds)

22

How many times will the body of the loop execute? my_list = [6, 2, 8, -1, 12, 15, -7] x = Get first my_list value While x is not negative: put "Positive number!" to output x = Get next my_list value Put "Done" to output

3

What is output? my_books = 'Books 105' index = my_books.find('',3) print(index)

3

How many times will the print statement execute? for i in range(10): for j in range(3): print(f'{i}. {j}')

30

How many times will the print statement execute? for i in range(1, 3): for j in range(8, 12, 2): print(f'{i}. {j}')

4

What is the ending value of count? my_list = [3, -4, 0, -1, 2, 1, 8] n = 0 count = 0 While n < length of my_list: If my_list[n] > 0 count = count + 1 n = n + 1

4

How many times does the following loop iterate? i = 5 while i < 10: print(i) i = i + 1

5

How many times does the following loop iterate? i = 0 while i <= 100: print(i) i = i + 2

51

What initial value of x will cause an infinite loop? x = int(input()) while x != 0: x = x - 2 print(x)

7

What is the ending value of z ? z = 0 a = 5 while a > 0: a = a - 1 if a == 2: continue z = z + a

8

What XXX and YYY will left align the column values print(f'{"Product":XXX}{"Price(cash)":YYY}') print('-' * 24) print(f'{"Banana":XXX}{1.02:YYY}') print(f'{"Apple":XXX}{5.08:YYY}')

<16, <8

Which input for variable c causes "Done" to be output next? c = 'y' while c == 'y': # Do something print('Enter y to continue, n to quit: ', end=' ') c = input() print('Done');

Any value other than 'y'

What is the output? def print_app(app_id, plan='basic', term=30): print(f'App:{app_id} ({plan} plan, {term} days)') print_app(10032, term=14)

App:10032 (basic plan, 14 days)

Which of the following loops is best implemented with a while loop?

Asking the user to enter positive integers, exiting by entering -1.

Standard Python functions such as int(), range() etc. are part of the _____ scope.

Built-in

What is output? my_dict = {'AA':3, 'BB': 2, 'CC':1, 'DD':0} v1 = list(my_dict.keys())[list(my_dict.values()).index(1)] v2 = {v:k for k, v in my_dict.items()}[0] print(v1,v2)

CC DD

What is the output? def print_fever_check(temperature): NORMAL_TEMP = 98.6 CUTOFF_TEMP = 95 degrees_of_fever = 0 if (temperature > NORMAL_TEMP): degrees_of_fever = temperature - NORMAL_TEMP print(f'You have {degrees_of_fever:f} degrees of fever.') elif (temperature < CUTOFF_TEMP): degrees_of_fever = CUTOFF_TEMP - temperature print(f'Your temperature is {degrees_of_fever:f} below 95.') body_temperature = 96.0 print('Checking for fever...') print_fever_check(body_temperature)

Checking for fever...

Which of the following loops is best implemented with a for loop? Counting the number of negative values in a list of integers.

Counting the number of negative values in a list of integers.

In Python, a namespace is which type of data structure?

Dictionary

What is output? my_string = 'Greetings!' print(my_string == 'greetings!') print('tin' in my_string) print('Hello' > my_string)

False True True

Which of the following is an advantage of using function stubs?

Helps with incremental programming

What are the values of sys.argv[1] and type(sys.argv[2]) for the command-line input > python prog.py June 16 ?

June, <class 'str'>

Which line of the function has an error? def compute_sum_of_squares(num1, num2): # Line 1 sum = (num1 * num1) + (num2 * num2) # Line 2 return # Line 3

Line 3

What is the output? def print_message1(): print('Message #1') def print_message2(): print('Message #2') print_message1 = print_message2 print_message2 = print_message1 print_message1() print_message2()

Message #2 Message #2

What is the output? def is_even(num): if num % 2 == 0: even = True else: even = False is_even(7) print(even)

No output: An error occurs due to unknown variable even

Which line in the function print_greeting() must be changed if the user wishes to print the greeting three times with three different names?

None. To print the greeting with three different names, the main program must call print_greeting() three times with three different arguments.

What is output? new_string = 'One giant leap for mankind' print(new_string[0:6])

One gi

What is the output? def print_water_temp_for_coffee(temp): if temp < 195: print('Too cold.') elif (temp >= 195) and (temp <= 205): print('Perfect temperature.') elif (temp > 205): print('Too hot.') print_water_temp_for_coffee(205) print_water_temp_for_coffee(190)

Perfect temperature. Too cold.

What is output? new_string = 'Python' print(new_string[0:-1:2])

Pto

What is output? new_string = 'Python' my_index = 0 while my_index != len(new_string)/2: print(new_string[my_index:int(len(new_string)/2)]) my_index += 1

Pyt yt t

What is output? my_poem = 'Roses are red; Violets are blue' new_separator = '.' new_poem = my_poem.split(';') print(new_separator.join(new_poem))

Roses are red. Violets are blue

What is the value of sys.argv[1]+len(sys.argv[1]) for the command-line input > python prog.py 121 ?

Runtime error

Which is an essential feature of a while loop having the following form? while loop_expression: loop_body

The loop_expression should be affected by the loop_body

What is the result when the program is executed? def add(x, y): return x + y print('Begin test') s = add('hello', 5) print(s) print('End test')

The program outputs 'Begin test', then an error is generated, and the program exits.

Which of the following is not a reason to use functions?

To make the code run faster

What is output? my_string = 'The area postal code is 99501' print(my_string[-5:].isdigit()) print(my_string[:3].isupper()) print(my_string[:3].islower())

True False False

What is error variance? cone_vol = cone_volume(2, 4) print(cone_vol) def compute_square(r): return r * r def cone_volume(r, h): return (0.33) * 3.14 * compute_square(r) * h

Undefined function cone_volume()

Complete the following code to get 'Email me at [email protected] or call at 333 222 1111' as output.

XXX: email.split(' ')[1], YYY: ' '.join((phone.split(': ' )[1]).split('-'))

What should XXX and YYY be so that the final output shows how many negative values are input? n = 0 val = Get next input While val is not 0 If XXX YYY val = Get next input put n to output

XXX: val < 0, YYY: n = n + 1

What is the output? names = ['Bob', 'Jill', 'Xu'] ages = [24, 18, 33] for index in [2, 0, 1]: print(f'{names[index]}:{ages[index]}')

Xu:33 Bob:24 Jill:18

What is output? dict = {1: 'X', 2: 'Y', 3: 'Z'} print(dict.get(2, 'A'))

Y

What is output? my_list = [['cat in a hat'], ['Dr', 'Seuss']] print(my_list[1][1:])

['Seuss']

What is output? my_string = 'What is your name?' print(my_string.split('?'))

['What is your name', '']

What is the value of new_list? my_list = [['hey', 'hello', 'hi'], 'good morning'] new_list = [i + '!' for i in my_list[0]]

['hey!', 'hello!', 'hi!']

What is output? new_list = ['python', 'development'] new_list.append('in progress') print(new_list)

['python', 'development', 'in progress']

What is output? my_var = ['python'] my_list = my_var[0].split() my_list += 'world' del my_list[1] print(my_list)

['python', 'o', 'r', 'l', 'd']

What is the value of new_list? my_list = [1, 2, 3, 4] new_list = [i**2 for i in my_list]

[1, 4, 9, 16]

What is output? new_list = [10, 'ABC', '123', 25] my_list = new_list[:] new_list[2] = 16 print(new_list) print(my_list)

[10, 'ABC', 16, 25] [10, 'ABC', '123', 25]

What is the output? def reset(data): data[1] = 34 print(data) data = [15, 0, 47, 12, 0] reset(data) print(data)

[15, 34, 47, 12, 0] [15, 34, 47, 12, 0]

What is output? my_list = [20, 25, 'hello'] new_list = my_list[:] print(new_list[0:2]) print(my_list[-1])

[20, 25] hello

What is output? new_list =[10, 20, 30, 40] for i in range(len(new_list)): if new_list[i] != new_list[-1]: new_list[i] *= 2 print(new_list)

[20, 40, 60, 40]

What is output? b1 = [7, 5, 9, 6] b1 = sorted(b1) b2 = b1 b2.append(2) print(b1, b2)

[5, 6, 7, 9, 2] [5, 6, 7, 9, 2]

What is output? new_list = [['hello', 'word'], 'good morning', 'have a nice day'] print(new_list[0:2])

[['hello', 'word'], 'good morning']

What is output? b1 = [[7, 5, 9], [3, 2, 1]] b1.sort() b2 = b1[:] print(b1, b2) for elem in b2: elem.sort() print(b1, b2)

[[3, 2, 1], [7, 5, 9]] [[3, 2, 1], [7, 5, 9]] [[1, 2, 3], [5, 7, 9]] [[1, 2, 3], [5, 7, 9]]

What is the value of kv_List? kv_List = [{'k': {'v': 2}}, {'k': {'v': 15}}, {'k': {'v': 8}}] kv_List.sort(key=lambda e: e['k']['v'], reverse=True)

[{'k': {'v': 15}}, {'k': {'v': 8}}, {'k': {'v': 2}}]

Which correctly calls the add() function? def add(a, b, c): return a + b + c

add(2, 4, 6)

What statement replaces XXX, causing the program to print the word 'hello'? def print_hello(): print('hello') XXX alternate()

alternate = print_hello

In the following code, the variable val is the function call's _____. def calc_square_area(size): area = size * size return area val = float(input('Enter size of square: ')) square_area = calc_square_area(val) print(f'A square of size {val} has area {square_area}')

argument

Complete the following code to print the average of the list. new_list = [0, 1, 2, 3, 4] XXX print(f'The average is {avg}')

avg = sum(new_list)/len(new_list)

What is the output? c1 = 'c' while c1 > 'a': for i in range(3): print(f'{c1}{i}', end=' ') c1 = chr(ord(c1) - 1)

c0 c1 c2 b0 b1 b2

Which variable can be used in place of XXX in the following code? def fahrenheit_to_celsius(f): fraction = 5 / 9 c = (f - 32) * fraction return c degrees = float(input('Enter degrees in Fahrenheit: ')) print(fahrenheit_to_celsius(XXX))

degrees

How do you obtain a list of tuples of key-value pairs in a dictionary dict?

dict.items()

Which of the following code blocks will print both keys and values?

dict1 = dict(number = 10, color = 'Red', word = 'Chair') for keys, values in dict1.items(): print(keys, values)

Which term describes how Python assigns the type of a variable?

dynamic typing

Which statement causes the face with 'x' characters for eyes to be printed?

face(print_x_eyes)

What defines the minimum number of characters that must be inserted into the string?

field width

Which XXX will display one more than half of the smallest value of w1, w2, and w3?

find_min_value(w1, w2, w3)/2 + 1

Consider the lists my_list = [['The', 'Magic'] , ['Rhonda', 'Byrne']] and new_list = [] . Select the option that will produce 'The Magic : Rhonda Byrne' as the output.

for i, j in enumerate(my_list): for item in my_list[i]: new_list.append(item) print(f'{new_list[0]} {new_list[1]}: {new_list [2]} {new_list[3]}')

The code that includes the keyword "def" is called a _____.

function definition

What is output? my_list = ['hello', 'good', 'morning'] print(my_list[1][:3])

goo

The following program prints the number of integers in my_list that are greater than the previous integer in the list. Which choice fills in the blank to complete the for loop? my_list = [ 3, 2, 7, 8, 6, 9 ] count = 0 for _____: if my_list[i] > my_list[i-1]: count = count + 1 print(count)

i in range(1, len(my_list))

Which XXX/YYY combination will create a rectangle of '*' characters, with 5 rows, and each row containing 10 '*' characters? for XXX: for YYY: print('*', end='') print()

i in range(5) / j in range(10)

_____ objects have fixed values that can't be changed.

immutable

Consider students = {'Semester_1' : {'Ryan':20, 'Mercy':25}, 'Semester_2' : {'Justin':21, 'Andrea':26}} . What command should be used to obtain the count of entries?

len(students)

def gen_command(application, **kwargs): command = application for key in kwargs: value = kwargs[key] command += f' --{key}={value}' return command print(gen_command('ls', color='always', sort='size', format='long'))

ls --color=always --sort=size --format=long

Identify the error in the program given below. my_dict = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5} _popped = my_dict.pop('B', 6) print(f'Value popped: {_popped}') my_dict['F'] = 0 _popped = my_dict.pop('F', 0) _popped = my_dict.pop('B') print(f'Value popped: {_popped}')

my_dict.pop('B') causes error

Select the option to get the value of temperature from the dictionary my_dict = {'Country':'India', 'State': {'City':'Delhi', 'Temperature':40}}

my_dict['State']['Temperature']

Choose the option that gives ['ABC', 'ABC'] as the output.

my_list = ['ABC', 'DEF'] my_list[len(my_list)-1] = 'ABC' print(my_list)

Choose the option in which the value of new_list is ['greetings'].

my_list = ['hello', 'greetings', 'hi', 'hey'] new_list = [i for i in my_list if len(i)>5]

Which of the following code blocks will give 3 as the output?

my_list = [10, 10, 2, 5, 10] print(my_list.index(10) + my_list.count(10))

Choose the correct syntax to complete the code below in order to print [1, 2, 3, 4, 5] as the output. my_list = [3, 2, 5] XXX print(my_list)

my_list.extend([1, 4]) my_list.sort()

Choose the correct syntax to complete the code below in order to print [1, 2, 3, 4, 5] as the output. my_list = [3, 2, 5] XXX print(my_list)

my_list.extend([1, 4]) my_list.sort()

Given a list my_list = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] , how would you access the value 7?

my_list[2][1]

Consider the list my_list = ['www', 'python', 'org']. Choose the option that returns 'www.python.org' as the value to my_string.

my_string = ('.').join(my_list)

Which XXX / ZZZ outputs every name/grade pair in the dictionary, as in: Jennifer: A? grades = { 'Jennifer' : 'A', 'Ximin' : 'C', 'Julio' : 'B', 'Jason' : 'C' } for XXX: print(ZZZ)

name in grades / name + ': ' + grades[name]

Which of the following code blocks produces the output given below?['odd', 'even', 'odd', 'even']

new_list = [3, 4, 5, 6] for i in range(len(new_list)): if new_list[i] % 2 == 0: new_list[i] = 'even' else: new_list[i] = 'odd' print(new_list)

Consider my_string = 'roy came third in the running race'. Which option will return 'Roy came 3rd in the running race' as the value of new_string?

new_string = my_string.capitalize() new_string = new_string.replace('thi', '3')

Consider the string new_string = 'Berlin is the capital of Germany'. Which statement will return 'capital of Germany' as a substring?

new_string[-18:]

Consider the string new_string = 'Code development in Python'. Which statement will return 'velop' as a substring?

new_string[-19:12]

In the following code, the variable size is the function's _____.

parameter

Consider the list my_list = [1880, '990EZ', 'Tax Forms'] . Which statement gives '990EZ' as the output?

print(my_list[1])

Which of the following statements returns '< class 'list'>, 5' as the output for the command-line input <code>&gt; python prog.py 1 January 2020</code>?

print(type(sys.argv), int(sys.argv[1])+len(sys.argv))

Which XXX causes the program to output the message "Hello!"?

print_message()

Which function call would cause an output error? def print_product(product_name, product_id, cost): print(f'{product_name} (id: #{product_id}) - ${cost:.2f}')

print_product('Speakers', 32.99, 21224)

Which function call will produce an error? def purchase(user_name, id_number=-1, item_name='None', quantity=0): # ... process a user's purchase as required ...

purchase(item_name='Orange', 10)

Which function call will produce an error? def purchase(user_name, id_number=-1, item_name='None',quantity=0): # ... process a user's purchase as required ...

purchase(item_name='Orange', 10)

Which choice fills in the blank so that the output prints one line for each item in sports_list, as in: 1. Hockey?

range(len(sports_list))

Which XXX completes the find_smallest() function? def find_smallest(a, b, c): if a <= b and a <= c: smallest = a elif b <= a and b <= c: smallest = b elif c <= a and c <= b: smallest = c XXX result = find_smallest(7, 4, 8) print(result)

return smallest

What is the missing function name so that the output is: Cairo New York Paris Sydney? cities = ['Sydney', 'Paris', 'New York', 'Cairo'] for c in _____(cities): print(c, end=' ')

reversed

The process of searching namespaces for a name is called _____.

scope resolution

What is a possible output? rentals = { 'skis' : 20.00, 'boots' : 10.00, 'skates' : 4.00 } for x in rentals: print(x, end=' ')

skis boots skates

For the given pseudocode, which XXX and YYY will output the sum of the input integers (stopping when -1 is input)? Choices are in the form XXX / YYY.

sum = 0 / sum = sum + val

Fill in the blank so that the output is a count of how many negative values are in temperatures?

t < 0

Which statement uses the square() function to assign the variable t with 16? Note that 16 = 2⁴ = (2²)² def calc_square(x): return x * x

t = calc_square(calc_square(2))

Which of the following methods will change the string 'Python Programming' into 'PYTHON PROGRAMMING'?

upper()

What is the value of rgb_a? colors = {1: 'red', 2: 'green', 3: 'blue', 4: 'yellow'} popped_item = colors.pop(4) colors.update({ 4: 'alpha'}) rgb_a = {} rgb_a['items'] = colors

{ 'items': { 1: 'red', 2: 'green', 3: 'blue', 4: 'alpha'}}

If factor = 24, which format will generate '**24', right aligned in its field?

{factor:*>4}


Set pelajaran terkait

Exam 4: Lecture Clicker Questions

View Set

Ch. 5: Employee and Labor Relations pt 1

View Set

Campbell Biology Chapter 12 Test Preparation

View Set

Drivers Ed.com Final Test (Lesson 28)

View Set