last two
last two
last two
5. What is the difference between class variables and instance variables in Python?
a) Class variables are defined within methods, while instance variables are
defined outside methods.
b) Class variables are accessible only within the class, while instance
variables are accessible outside the class.
c) Class variables have a different value for each instance of a class, while
instance variables have the same value for all instances.
d) Class variables are used to define attributes, while instance variables
are used to define methods.
answer is: c) Class variables have a different value for each instance of a class,
while instance variables have the same value for all instances.
6. What's the output of this recursive function with a list?
def sum_list(lst):
if not lst:
return 0
print(sum_list([1, 2, 3, 4, 5]))
i. 15 (Correct Answer)
ii. 10
iii. 5
iv. 20
7. What's the output of this recursive function that counts digits?
def count_digits(n):
if n == 0:
return 1
print(count_digits(12345))
i. 6 (Correct Answer)
ii. 4
iii. 5
iv. Error
def counter():
count = 0
def increment():
nonlocal count
count += 1
return count
return increment
count1 = counter()
count2 = counter()
i. 4 (Correct Answer)
ii. 3
iii. 5
iv. 2
9. What's the output of this nested exception handling?
try:
try:
x=1/0
except ZeroDivisionError as e:
print(ve.__cause__.__class__.__name__)
i. ValueError
ii. ZeroDivisionError (Correct Answer)
iii. None
iv. Exception
def process():
try:
raise KeyError("First")
except KeyError as e:
try:
process()
print(te.__cause__.__cause__.__class__.__name__)
x=5
if x < 3:
elif x == 3:
else:
a) x is less than 3
b) b) x is equal to 3
c) c) x is greater than 3
d) d) No output
Answer: c
i. 15 (Correct Answer)
ii. 12
iii. 10
iv. Error
i. 0 (Correct Answer)
ii. 3
iii. 1
iv. 2
15. What's the output of this list and set comprehension?
a = [1, 2, 3, 4]
b = {i: set(a[i:] + a[:i]) - {i} for i in range(len(a))}
c = [k for k, v in b.items() if all(x > k for x in v)]
print(sorted(c))
a = 123
b = 456
print(c)
i. 1842
ii. Error (Correct Answer)
iii. 2468
iv. 2784
x = 345
y = 567
print(result)
i. 889
ii. 1427 (Correct Answer)
iii. 1024
iv. 1156
18. Which of the following is the correct way to create a set from a tuple?
`set([1, 2])`
`{1, 2}`
`set{1, 2}`
s = {1, 2, 3}
s.update([3, 4, 5])
print(s)
`{1, 2}`
`{3,4,5}`
`{1, 2, 3}`
d = {1:'a', 2:'b'}
d[3] = 'c'
print(d)
i. {1:'a', 2:'b'}
ii. Error
iii. {1:'a', 2:'b', 3:'c'} (Correct Answer)
iv. None
1. What will be the output of the following code?
class A:
def __init__(self):
self.value = 5
class B(A):
def __init__(self):
self.value = 10
super().__init__()
obj = B()
print(obj.value)
a) 5 (correct answer)
b) 10
c) None
d) AttributeError
def modify_list(lst):
for i in range(len(lst)):
lst[i] *= 2
return lst
my_list = [1, 2, 3]
print(modify_list(my_list) + my_list)
def count_down(n):
if n < 0:
return
print(n)
count_down(n - 1)
count_down(3)
a. 3 2 1 0 (Correct Answer)
b. 321
c. 0
d. Error
def count_up_to(n):
count = 1
yield count
count += 1
print(sum(count_up_to(5)))
a. 15 (Correct Answer)
b. 10
c. 5
d. 20
13. x = get_value()
14. print(x)
a. 1
b. 2 (Correct Answer)
c. None
d. Error
18. try:
19. try:
20. raise CustomError("Test")
21. except Exception as e:
22. raise ValueError from e
23. except ValueError as ve:
24. print(isinstance(ve.__cause__, CustomError))
a. 44 (Correct Answer)
b. 45
c. 30
d. Error
34. 22. Consider this nested list and dictionary operation:
35. x = [1, 2, 3, 4, 5]
36. y = {i: x[i:] + x[:i] for i in range(len(x))}
37. z = {k: [i for i in v if i > k and i < len(v)] for k, v in y.items()}
38. print(sum(len(v) for v in z.values()))
a. 10 (Correct Answer)
b. 12
c. 8
d. 15
39. 23. What's the result of this set and list manipulation?
40. a = {1, 2, 3, 4}
41. b = [list(a - {i}) for i in a]
42. c = {i: sum(b[i]) for i in range(len(b)) if i in a}
43. print(max(c.values()))
a. 8 (Correct Answer)
b. 10
c. 9
d. Error
44. 24. Consider this complex dictionary comprehension:
45. x = {i: list(range(i, i+3)) for i in range(2, 5)}
46. y = {k: [i for i in v if i not in x[k-1]] for k in x if k-1 in x}
47. print(sum(len(v) for v in y.values()))
a. 548
b. 142 (Correct Answer)
c. 712
d. 836
a. 312
b. 174 (Correct Answer)
c. 528
d. 643
56. 40. What will be the output of:
57. s1 = {1,2,3}
58. s2 = {3,4,5}
59. print(s1 ^ s2)
a. {1,2}
b. {4,5}
c. {1,2,4,5} (Correct Answer)
d. {3}
a. {'a':1, 'b':2}
b. {'b':2} (Correct Answer)
c. Error
d. None