1 - What is the output of the following snippet?
tup = (1, 2, 4, 8)
tup = tup[-2:-1]
tup = tup[-1]
print(tup)
a) 44
b) 4
c) (4)
c) (4,)
Solution: b
2 - Assuming that my_tuple is a correctly crated tuple, the fact that tuples are immutable means that the following instruction:
my_tuple[1] = my_tuple[1] + my_tuple[0]
a) may be illegal if the tuple contains strings
b) is illegal
c) can be executed if and only if the tuple contains at least two elements
d) is full correct
Solution: b
3 - Which of the following lines correctly. invoke the function defined below? (Select two answers)
def fun(a, b, c=0):
#Body of the function.
a) fun()
b) fun(0, 1, 2)
c) fun(b=0, a=0)
d) fun(b=1)
Solution: b, c
4 - The meaning of a positional argument is determined by:
a) its position within the argument list
b) its connection with existing variables
c) its value
d) the argument’s name specified along with its value
Solution: a
5 - What will happen when you attempt to run the following code?
print(Hello, World)
a) The code will raise the AttributeError exception.
b) The code will raise the systemError exception.
c) The code will raise the ValueError exception.
d) The code will print Hello World to the console.
e) The code will raise the TyepError exception.
Solution: b
6 - The following snippet:
def func(a, b):
reeturn b ** a
print(func(b=2, 2))
a) will output 4
b) will output None
c) will output 2
d) is erroneous
Solution: d
7 - The following snippet:
def function_1(a):
return None
def function_2(a):
return function_1(a) * functin_1(a)
print(function_2(2))
a) will output 16
b) will crate a runtime error
c) will output 4
d) will output 2
Solution: b
8 - What is the output of the following snippet?
def fun(x):
if c % 2 == 0:
return 1
else:
return 2
print(fun(fun(2)))
a) 2None
b) 1
c) the code will cause a runtime error
d) 2
Solution: d
9 - What is the output of the following piece of code?
print("a", "b", "c", sep="sep")
a) a b c
b) abc
c) asepbsepcsep
d) asepbsepc
Solution: d
10 - WWhich of the following sentences are true about the code? (Select two answers)
nums = [1, 2, 3]
vals = nums
a) nums and vals are different names of the same list
b) vals is longer tha nums
c) nums and vals are different lists
d) nums has the same length as vals
Solution: a, d
11 - What is the output of the following snippet?
my_list = [1, 2]
for v in range(2):
my_list.insert(-1, my_list[v])
print(my_list)
a) [1, 2, 2, 2]
b) [1, 2, 1, 2]
c) [2, 1, 1, 2]
d) [1, 1, 1, 2]
Solution: d
12 - What is the output of the following snippet?
dct = {}
dct['1'] = (1, 2)
dct['2'] = (2, 1)
for x in dct.keys():
print(dct[x][1], end="")
a) 21
b) (2,1)
c) 12
d) (1,2)
Solution: a
13 - What is the output of the following piece of code?
x = 1 // 5 + 1 / 5
print(x)
a) 0.0
b) 0
c) 0.4
d) 0.2
Solution: d
14 - 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)
a) 2 1 2
b) 1 2 2
c) 1 1 2
d) 1 2 1
Solution: c
15 - The result of the following division:
1 // 2
a) is equal to 0.5
b) is equal to 0
c) is equal to 0.0
d) cannot be predicted
Solution: b
16 - What is the output of the following snippet?
def fun(inp =2, out =3):
return inp * out
print(fun(out =2))
a) 4
b) 6
c) 2
d) the snippet is erroneous and will cause SyntaxError
Solution: a
17 - What is the expected behavior of the following program?
foo = (1, 2, 3)
foo.index(0)
a) The program will cause a ValueError exception.
b) The program will output 1 to the screen.
c) The program will cause a SyntaxError exception.
d) The program will cause a TypeError exception.
e) The program will cause an AttributeError exception.
Solution: a
18 - What is the output of the following snippet?
def fun(x, y):
if x == y:
return x
else:
return fun(x, y-1)
print(fun(0,3))
a) 1
b) 2
c) the snippet will cause a runtime error
d) 0
Solution: d
19 - How many element does the lst list contain?
lst = [i for i in range(-1, -2)]
a) two
b) three
c) zero
d) one
Solution: c
20 - What is the output of the following piece of code if the user enters two lines containing 2 and 4 respectively?
x = float(input())
y = float(input())
print(y **(1 / x))
a) 4.0
b) 2.0
c) 0.0
d) 1.0
Solution: b
21 - What is the output of the following code if the user enters a 0 ?
try:
value = input("Enter a value: ")
print(int(value) / len(value))
except ValueError:
print("Bad input...")
except ZeroDivisionError:
print("Very bad input...")
except TypeErrorq:
print("Very very bad input...")
except:
print("Booo!")
a) Very bad input...
b) Very very bad input...
c) Bad input...
d) Booo!
e) 0.0
f) 1.0
Solution: 0.0
22 - What is the output of the following snippet?
dct = {'one': 'two', 'three': 'one', 'two': 'three'}
v = dct['three']
for k in range(len(dct)):
v = dct[v]
print(v)
a) two
b) ('one', 'two', 'three')
c) three
d) one
Solution: d
23 - What value will be assignment to the x variable?
z = 0
y = 0
x = y < z and z > y or y > z and z < y
a) False
b) 0
c) 1
d) True
Solution: d
24 - What is the output of the following piece of the code if the user enter two lines containing 3 and 6 respectively?
y = input()
x = input()
print(x + y)
a) 36
b) 3
c) 63
d) 6
Solution: c
25 - Which of the following variable names are illegal and will cause the SystemError exception? (Select two answers)
a) print
b) in
c) for
d) in
Solution: c, d
26 - What is the expected behavior of the following program?
try:
print(5/0)
break:
except:
print("Sorry, something went wrong...")
except(ValueError, ZeroDivisionError):
print("Too bad...")
a) The program will cause a ValueError exception and output the following message Too bad...
b) The program will cause a SyntaxError exception
c) The program will cause a ValueError exception and output a default error message.
d) The program will raise an exception handle by the first except block.
e) The program will cause a ZeroDivisionError exception and output the following message: Too bad...
f) The program will cause a ZeroDivisionError exception and output a default error message.
Solution: b
27 - Take a look at the snippet and choose the true statement:
nums = [1, 2, 3]
vals = nums
del vals[:]
a) the snippet will cause a runtime error
b) vals is longer than nums
c) nums is longer than valsbr>
d) nums and vals have the same length
Solution: d
28 - 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))
a) [0 , 1, 4, 9]
b) [0, 1, 4, 16]
c) [0, 1, 9, 16]
d) [1, 4, 9, 16]
Solution: a
29 - What is the output of the following snippet?
dd = {"1": "0", "0": "1"}
for x in dd.vals():
print(x, end="")
a) 0 1
b) 1 0
c) 0 0
d) the code is erroneous(the dict object has no vals() method)
Solution: d
30 - What is the output of the following piece of code if the user enters two lines containing 3 and 2 respectively?
x = int(input())
y = int(input())
x = x % y
y = y % x
print(y)
a) 1
b) 3
c) 2
d) 0
Solution: d
31 - Which if the following snippets shows the correct way of handing multiple excepting in a single except clause?
a) except TypeError, ValueError, ZeroDivisinError:
# Some code.
b) except: (TypeError, ValueError, ZeroDivisinError)
# Some code
c) except TypeError, ValueError, ZeroDivisinError
# Some code.
d) except: TypeError, ValueError, ZeroDivisinError
# Some code.
e) except: (TypeError, ValueError, ZeroDivisinError):
# Some code.
f) except (TypeError, ValueError, ZeroDivisinError)
# Some code.
Solution: e
32 - An operator able to check two values are not equal is code as:
a) =/=
b) !=
c) <>
d) not ==
Solution: b
33 - What will be the output of the following snippet?
a = 1
b = 0
a = a ^ b
b = a ^ b
a = a ^ b
print (a, b)
a) 1 1
b) 0 0
c) 1 0
d) 0 1
Solution: d
34 - How many stars (*) will the following snippet send to the console?
i =0
while i < i + 2 :
i += 1
print("*")
else:
print("*")
a) zero
b) the snippet will enter an infinite loop, printing one star per line
c) one
d) two
Solution: b
35 - How many hashes (*) will the following snippet sent to the console?
lst = [[x for x in range(3)] for y in range(3)]
for r in range(3):
for c in rang(3):
if lst[r][c] % 2 != 0:
print("#")
a) nine
b) zero
c) three
d) six
Solution: c