Logo
Flashcard >cs test 4
Add Flashcard
1/15

Consider the following code snippet. What will be printed by the final print statement?

b = 10

def modify_b():
    global b
    b = 5
    return b

b = modify_b() + 3
print(b)

13

8

10

5

8

profile imgdavidmacago posted a year ago

Consider the following code snippet. What will be printed by the final print statement?

b = 10

def modify_b():
    global b
    b = 5
    return b

b = modify_b() + 3
print(b)

13

8

10

5

8

Which function call is logically incorrect given the function definition?

def make_coffee(size="Medium", type="Cappuccino"):
    return f"{size} {type}"

make_coffee("Large")

make_coffee("Latte", "Small")

make_coffee(type="Espresso")

make_coffee()

make_coffee("Latte", "Small")

What does the following function do when [1, 4, 2, 0, 5] is passed to it?

def check_zeros(numbers):
    for num in numbers:
        if num == 0:
            return "Contains zero"
    return "No zeros"

Continues to check all numbers after finding a 0

Returns "No zeros"

Prints [1, 4, 2]

Returns "Contains zero"

Returns "Contains zero"

Consider the function add and its usage below. What is the final output when print(total) is executed?

def add(x, y):
    return x + y

total = add(10, 20)
total = add(total, 5)

15

30

35

25

35

What will be the output of the following code snippet?

def print_kwargs(**kwargs):
    for key in kwargs:
        print(key)

print_kwargs(alpha=1, beta=2, gamma=3)

SyntaxError

1 2 3

('alpha', 1), ('beta', 2), ('gamma', 3)

alpha beta gamma

alpha beta gamma

What is the output of this code?

def numplustwo(x):
    x += 2
    
y = 2
numplustwo(y)
print(y)

4

None

Error

2

2

What will be the output of the following code that uses list unpacking for positional arguments?

def calculate_sum(a, b, c):
    return a + b + c

numbers = [1, 2, 3]
print(calculate_sum(*numbers))

[1, 2, 3]

6

1 2 3

TypeError

6

What is the output of this code?

def dest(lst):
    lst += [5]
listx = [1, 2, 3]
dest(listx)
print(listx)

[1, 2, 3, 5]

[6, 7, 8]

[1, 2, 3]

Error

[1, 2, 3, 5]

What is the output of this code?

def sum(x, y, z):
    return x + y + z
print(sum(*range(7,10)))

[7, 8, 9]

Error

24

[7, 8, 9, 10]

24

Given the function create_profile and the dictionary user_data, which method of calling the function is incorrect?

def create_profile(username, email, subscription_status):
    return f"Profile: {username}, Email: {email}, Status: {subscription_status}"

user_data = {"username": "johndoe", "email": "johndoe@example.com", "subscription_status": "Active"}

create_profile(username="johndoe", email="johndoe@example.com", subscription_status="Active")

create_profile(user_data)

create_profile(**user_data)

create_profile(username=user_data["username"], email=user_data["email"], subscription_status=user_data["subscription_status"])

create_profile(user_data)

What will happen when the function validate_age is called with validate_age(-5)?

def validate_age(age):
    assert age > 0, "Age cannot be negative"
    return "Age is valid"

Nothing, the function executes normally

AssertionError: Age cannot be negative

Returns "Age cannot be negative"

Returns "Age is valid"

AssertionError: Age cannot be negative

Which of the following best demonstrates the benefit of functions in reducing code redundancy?

Avoiding the use of functions to keep the program straightforward.

Using a loop instead of a function to repeat an action.

Creating a function to handle common tasks that are repeated in different parts of the program.

Writing unique code for every action, even if the actions are similar.

Creating a function to handle common tasks that are repeated in different parts of the program.

What is the conventional format for a docstring in Python?

A string literal enclosed in triple quotes (""") immediately following the function definition.

A comment line starting with #.

A single line of code at the start of the function.

An external text file linked to the function.

A string literal enclosed in triple quotes (""") immediately following the function definition.

What is the output of this code?

def subtract(a, b):
    return a - b
print(subtract(b=5, a=10))

None

-5

Error

5

5

What is the output of this code?

x = 200 

def foo():
   x = 300 
   def bar():
      x = 400 
      print(x)
   print(x)

foo() 
print(x) 
bar()

300 200 400

300 400 200

400 300 200

200 400 300

300 200 400

Recommended Books

Reading books is a great way to learn. Here are some of the books we recommend.