Which code snippet correctly uses assert to ensure the first argument is a list?
def foo(x):
if type(x) != list:
raise TypeError
def foo(x):
assert isinstance(x, list)
def foo(x):
if not isinstance(x, list):
raise AssertionError
def foo(x):
assert type(x) != list
def foo(x):
assert isinstance(x, list)
davidmacago posted a year ago
Which code snippet correctly uses assert to ensure the first argument is a list?
def foo(x):
if type(x) != list:
raise TypeError
def foo(x):
assert isinstance(x, list)
def foo(x):
if not isinstance(x, list):
raise AssertionError
def foo(x):
assert type(x) != list
def foo(x):
assert isinstance(x, list)
What is the output of this code?
def multiply(x, y, z):
return x * y * z * 2
nums = [2, 3, 4]
print(multiply(*nums))
[2, 3, 4], [2, 3, 4]
24
48
[4, 6, 8]
48
Which function correctly sums up all the keyword argument values passed to it?
def sum_values(**kwargs):
return sum(kwargs.values())
sum_values(a=1, b=2, c=3) returns 6
sum_values(a=1, b=2, c=3) returns {'a': 1, 'b': 2, 'c': 3}
sum_values() returns 0
sum_values(1, 2, 3) returns 6
sum_values(a=1, b=2, c=3) returns 6
What is the output of this code?
def fn1(lst):
lst[0] = 10
my_list = [1, 2, 3]
fn1(my_list)
print(my_list)
Error
None
[1, 2, 3]
[10, 2, 3]
[10, 2, 3]
What is the output of this code?
def nextnumber(x):
x += 1
print(x)
num = 5
nextnumber(num)
print(num)
5 5
6 6
6 5
5 6
6 5
What will be the output of the following code?
def greet(name, msg="Hello"):
return f"{msg}, {name}!"
print(greet("Alice"))
print(greet("Bob", "Good morning"))
SyntaxError
Hello, Alice! and Good morning, Bob!
Hello, Alice! and Hello, Bob!
Good morning, Alice! and Good morning, Bob!
Hello, Alice! and Good morning, Bob!
What will be the output of this function when check_prime(11) is called?
def check_prime(num):
if num < 2:
return False
for i in range(2, num):
if num % i == 0:
return False
return True
True
None
False
Error
True
Why are functions with well-defined responsibilities preferable in programming?
They make debugging more challenging.
They allow for easier modification and updating of specific parts of the code.
They make the code slower but more organized.
They require less documentation
They allow for easier modification and updating of specific parts of the code.
What is the primary purpose of a docstring in a Python function?
To improve the speed of the function.
To declare variable types used in the function.
To compile the code more efficiently.
To provide a detailed description of the function’s purpose and behavior.
To provide a detailed description of the function’s purpose and behavior.
What is the output of this code?
def print_info(a, b, c=3):
print(a, b, c)
print_info(1, c=2, b=3)
1 3 2
Error
1 2 3
1 3 3
1 3 2
What is the output of this code?
x = 400
def foo():
x = 300
def bar():
x = 200
print(x)
bar()
print(x)
foo()
print(x)
300 400 200
300 200 400
200 300 400
400 300 200
200 300 400
Given the code below, what will be printed by the last line?
y = 20
def change_value():
global y
y = 15
return y
print(change_value())
print(y)
15 20
20 20
15 15
20 15
15 15
Given the function definition and subsequent calls, what is the final value of result?
def multiply(a, b):
return a * b
result = multiply(3, 5)
result = multiply(result, 2)
10
15
60
30
30
Given the function max_value and the list nums, which call will correctly find the maximum value in the list?
def max_value(*args):
return max(args)
nums = [5, 10, 15, 20, 25]
max_value(nums[0], nums[1], nums[2], nums[3], nums[4])
max_value(*nums)
max_value(nums)
max_value([*nums])
max_value(*nums)
Given the following function and dictionary, which code snippet correctly calls the function?
def update_record(name, email, status):
return f"Updated {name} with {email}, status {status}."
record = {"name": "Bob", "email": "bob@example.com", "status": "Active"}
update_record(name=record["name"], email=record["email"], status=record["status"])
update_record(record)
update_record(**record)
update_record(*record)
update_record(**record) is the best answer.
update_record(name=record["name"], email=record["email"], status=record["status"]) is another possible answer but not preferred.