Logo
Flashcard >final exam #3
Add Flashcard
1/14

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]

1 2 3

TypeError

6

6

profile imgdavidmacago posted 2 years ago

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]

1 2 3

TypeError

6

6

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 beta gamma

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

alpha beta gamma

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"

AssertionError: Age cannot be negative

Returns "Age is valid"

Returns "Age cannot be negative"

Nothing, the function executes normally

AssertionError: Age cannot be negative

Which code snippet correctly uses assert to ensure the first argument is a list?

def foo(x): 
   assert isinstance(x, list)
def foo(x): 
   assert type(x) != list
def foo(x): 
   if type(x) != list: 
      raise TypeError
def foo(x): 
   if not isinstance(x, list): 
      raise AssertionError
def foo(x): 
   assert isinstance(x, list)

What is the output of this code?

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

[6, 7, 8]

Error

[1, 2, 3, 5]

[1, 2, 3]

[1, 2, 3, 5]

What is the output of this code?

b = 10
def modify_b():
    global b
    b = 5
    return b
b = modify_b() + 3
print(b)

3

8

5

10

8

What is the output of this code?

def add(x, y):
    return x + y
total = add(10, 20)
total = add(total, 5)

30

35

15

25

35

Which call will correctly find the maximum value in the list

nums = [5, 10, 15, 20, 25]

using the following function?

def max_value(*args): 
      return max(args)

max_value(*nums)

max_value([*nums])

max_value(nums)

max_value(nums[0])

max_value(*nums)

What is the output of this code?

x = [1, 2, 3]
y = [1, 2, 3]

print(x == y, x is y) 

False, False

True, True

True, False

False, True

True, False

What is the output of this code?

data = {'x': {'y': 10, 'z': 20}, 'w': 30}

print(data['x']['z'])

20

{'y': 10, 'z': 20}

30

10

20

What is the output of this code?

def subtract(a, b): 
   return a - b

print(subtract(b=5, a=10))

10

5

-5

15

5

What is the output of this code?

for i in range(1, 6):
    if i == 3:
        break
    print(i)

1,2,3,4,5

1,2,4,5

1,2,3

1,2

1,2

What is the output of this code?

for i in range(2, 9, 3): 
   print(i)

2, 4, 6, 8

2, 5, 7

2, 5

2, 5, 8

2, 5, 8

What will be printed from this code?

for number in range(10):
    if number % 2 == 0:
        continue
    print(number)

No numbers

All numbers from 0 to 9

All even numbers from 0 to 9

All odd numbers from 0 to 9

All odd numbers from 0 to 9

YouTube LogoFind us on YouTubePatreon LogoSupport us on Patreon

Recommended Books

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