Examen Final
Examen Final
Examen Final
its value
its position within the argument list
the argument’s name specified along with its value
Explanation: Positional arguments can be called by their position in a function call,
and therefore must be included in the correct order.
3. Which of the following sentences are true about the code? (Select two
answers)
nums = [1, 2, 3]
vals = nums
nums and vals are different lists
nums has the same length as vals
nums and vals are different names of the same list
vals is longer than nums
Explanation:
Assigning nums to vals creates a situation in which the same list (i.e. [1, 2, 3]) has two
different names. Giving a new name to an existing list, in our case vals to nums, is
called aliasing. And since nums and vals are two different names that refer to the
same object, they also have the same length (3).
4. An operator able to check whether two values are not equal is coded as:
=/=
!=
<>
not
Explanation:
The != operator checks whether two values are not equal. For example, the following
expression 1 != 2 will evaluate as True.
5. The following snippet:
def function_1(a):
return None
def function_2(a):
return function_1(a) * function_1(a)
print(function_2(2))
will output 4
will cause a runtime error
will output 16
will output 2
Explanation:
The None object has no arithmetic operators defined – the program will raise
the TypeError exception, because the NoneType type cannot be the * operator’s
operand.
6. The result of the following division:
1 // 2
cannot be predicted
is equal to 0.0
is equal to 0.5
is equal to 0
Explanation:
The // operator divides two numbers and rounds the result down to the nearest integer
number. Since 1 divided by 2 is 0.5, the resulting number is rounded down to 0.
7. The following snippet:
def func(a, b):
return b ** a
print(func(b=2, 2))
will output 2
is erroneous
will output 4
will output None
Explanation:
The program will raise the SyntaxError, because the positional argument in the
function call (2) follows the keyword argument (b=2). Changing the order of the
arguments in the func() function invocation (i.e. func(2, b=2)) fixes the issue, in which
case the program would output 4 as the result.
8. What value will be assigned to the x variable?
z=0
y = 10
x = y < z and z > y or y < z and z < y
0
True
False
1
Explanation:
Let’s analyze the example:
0 is assigned to z, and 10 is assigned to y,
the result of the following evaluation y < z and z > y or y < z and
z < y is assigned to x:
y < z evaluates to False,
z > y evaluates to False,
y < z evaluates to False again,
and z < y evaluates to True.
And now: False and False is False; False or False is False; and finally
False and True is False.
9. Which of the following variable names are illegal and will cause the
SyntaxError exception? (Select two answers)
for
print
in
In
Explanation:
The in and for names are Python reserved words (keywords). These cannot be used
as variable names. Note that the name print is not a Python reserved word and can be
used as a variable name, in which case it will shadow the Python built-in
function, print().
10. What is the output of the following snippet?
my_list = [x * x for x in range(5)]
def fun(lst):
del lst[lst[2]]
return lst
print(fun(my_list))
[0, 1, 4, 16]
[1, 4, 9, 16]
[0, 1, 4, 9]
[0, 1, 9, 16]
Explanation:
Let’s analyze the example:
Line 1: The list comprehension is used to create the following list: [0, 1, 4, 9,
16], and assign it to the my_list variable,
Lines 4-6: The fun() function is defined, which takes a list (lst) as its
argument. In the body of the function, the del instruction deletes a specified
element of the list, and returns the updated list,
Line 9: The print() function takes the fun() function as its argument, and
prints the returned value to the screen. The fun() function takes
the my_list list as its argument and processes it – the del instruction deletes
the fourth element of the list (my_list[2] is 4).
11. What is the output of the following piece of code?
x=1
y=2
x, y, z = x, x, y
z, y, z = x, y, z
print(x, y, z)
212
122
121
112
Explanation:
Let’s analyze the example:
x = 1 and y = 2
as a result of the first multiple variable assignment (Line 3), x = 1, y = 1,
and z = 2
as a result of the second multiple variable assignment (Line 4), z = x = 1, y
= y = 1, and z = z = 2 (because the original assignment from the previous
line now overwrites the first z assignment from the current line)
the result printed to the screen is therefore 1 1 2