PCEP Functions, Tuples, Dictionaries and Exceptions • Complete Question Bank
Complete PCEP Functions, Tuples, Dictionaries and Exceptions question bank — all 0 questions with answers and detailed explanations.
Refer to the exhibit.
def divide(a, b):
try:
return a / b
except ZeroDivisionError:
return float('inf')
except TypeError:
return None
result = divide(10, '5')
print(result)Refer to the exhibit.
data = {'a': 1, 'b': 2, 'c': 3}
for key, value in data.items():
if value % 2 == 0:
data.pop(key)
print(data)Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag a concept onto its matching description — or click a concept then click the description.
Outputs objects to the console
Reads a string from standard input
Returns the number of items in a container
Returns the type of an object
Converts a value to an integer
Drag a concept onto its matching description — or click a concept then click the description.
Converts all characters to uppercase
Converts all characters to lowercase
Removes leading and trailing whitespace
Splits a string into a list of substrings
Joins elements of an iterable into a single string
A developer writes a function that should return the sum of two numbers, but the code returns 0 instead. What is the most likely cause?
def add(a, b):
result = a + b
print(add(3, 4))
Consider the following code:
def foo(x, y):
return x * yresult = foo(y=2, 3)
What is the error?
What does the following code output?
try:
x = int('abc') except ValueError:
print('Invalid')Refer to the exhibit.
Error log output:
Traceback (most recent call last):
File "app.py", line 10, in <module>
result = divide(10, 0)
File "app.py", line 5, in divide
return a / b
ZeroDivisionError: division by zeroRefer to the exhibit.
def process(data):
return {item: len(item) for item in data}
print(process(['apple', 'banana', 'cherry']))Refer to the exhibit.
config = {
'host': 'localhost',
'port': 8080,
'timeout': 30
}
try:
value = config['debug']
except KeyError:
print('Key not found')def divide(a, b):
return a / b
result = divide(10, 0)d = {'a':1, 'b':2}
for k, v in d.items():
if k == 'a':
del d[k]
print('done')funcs = []
for i in range(3):
funcs.append(lambda: i)
for f in funcs:
print(f(), end=' ')What is the output of the following code?
def greet(name, greeting='Hello'):
print(greeting, name)greet('Alice')
A function returns a tuple. Which code correctly unpacks the tuple?
def min_max(numbers):
return min(numbers), max(numbers)result = min_max([3, 1, 2])
What is the output of the following code?
def test():
try:
return 1finally:
return 2 print(test())
What is the result of the following expression?
d = {'a': 1} d.get('b', 0)
Which code sorts a list of strings by their length in descending order?
lst = ['aa', 'b', 'ccc']
What is the output of the following code?
def div(a, b):
try:
return a / bexcept ZeroDivisionError: raise ValueError('Invalid division')
try:
print(div(10, 0))except ValueError as e:
print(e)
except ZeroDivisionError:
print('Zero division')What happens when you try to modify a tuple?
t = (1, 2, 3) t[0] = 0
What is the output of the following dictionary comprehension?
{x: x**2 for x in range(3)}What is the output of the following code?
def f():
try:raise ValueError('error1') except ValueError: raise TypeError('error2')
try:
f() except TypeError as e:
print(e)
except ValueError:
print('ValueError')Traceback (most recent call last):
File "app.py", line 10, in <module>
result = divide(10, 0)
File "app.py", line 5, in divide
return a / b
ZeroDivisionError: division by zerodef add_item(item, lst=[]):
lst.append(item)
return lst
print(add_item(1))
print(add_item(2, []))
print(add_item(3))def func(a, b, *args, **kwargs):
print(a, b, args, kwargs)
func(1, 2, 3, 4, x=5, y=6)def func(a, b, c=10, d=20):
return a + b + c + d
print(func(1, 2, 3, 4)) # Line A
print(func(1, 2, 3)) # Line B
print(func(1, 2)) # Line Cdef modify_dict(d):
d['key'] = 'new_value'
d = {'another': 'dict'}
my_dict = {'key': 'old_value'}
modify_dict(my_dict)
print(my_dict)def exception_test():
try:
x = 1 / 0
except ZeroDivisionError:
print('A')
except:
print('B')
finally:
print('C')
print('D')
exception_test()def outer():
x = 10
def inner():
nonlocal x
x = 20
print(x)
inner()
print(x)
outer()