last two

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

1. What is the purpose of the `__init__` method in Python classes?

a) It is a special method used to create a new instance of a class.


b) It is used to define class attributes.
c) It is a built-in method that Python automatically calls.
d) It is used to delete an instance of a class.
Correct answer is: a) It is a special method used to create a new instance of a
class.

2. What is the purpose of the `self` parameter in Python class methods?


a) It represents the superclass of a class.
b) It represents the current instance of a class.
c) It is a keyword used to define class attributes.
d) It is a built-in function in Python.
Correct answer is: b) It represents the current instance of a class.

3. Which of the following best describes polymorphism in Python?


a) It allows a class to inherit from multiple superclasses.
a) It allows a class to hide its implementation details.
c) It allows different classes to implement the same method name.
d) It allows a class to access variables from a module.
Correct answer is: c) It allows different classes to implement the same
method name..

4. What is method overriding in Python?


a) It allows a subclass to inherit attributes from a superclass.
b) It allows a subclass to implement its own version of a method inherited
from a superclass.
c) It allows a subclass to access the private attributes of a superclass.
d) It allows a subclass to hide its implementation details.
Correct answer is: b) It allows a subclass to implement its own version of a
method inherited from a superclass.

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

return lst[0] + sum_list(lst[1:])

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

return 1 + count_digits(n // 10)

print(count_digits(12345))

i. 6 (Correct Answer)
ii. 4
iii. 5
iv. Error

8. Consider this function that uses a closure to maintain state:

def counter():

count = 0

def increment():

nonlocal count

count += 1
return count

return increment

count1 = counter()

count2 = counter()

print(count1() + count1() + count2())

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:

raise ValueError("Inner error") from e

except ValueError as ve:

print(ve.__cause__.__class__.__name__)

i. ValueError
ii. ZeroDivisionError (Correct Answer)
iii. None
iv. Exception

10.Consider this exception chaining:

def process():

try:

raise KeyError("First")

except KeyError as e:

try:

raise ValueError("Second") from e

except ValueError as ve:

raise TypeError("Third") from ve


try:

process()

except TypeError as te:

print(te.__cause__.__cause__.__class__.__name__)

i. KeyError (Correct Answer)


ii. ValueError
iii. TypeError
iv. None
11.Q5. What will be the output of the following code snippet?

x=5

if x < 3:

print("x is less than 3")

elif x == 3:

print("x is equal to 3")

else:

print("x is greater than 3")

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

12. Consider this nested set and list manipulation:


a = [[1, 2], [3, 4], [5, 6]]
b = {i: {sum(row[:i+1]) for row in a} for i in range(len(a[0]))}
c = [max(v) - min(v) for k, v in b.items() if len(v) > k]
print(sorted(c))

i. [4, 8] (Correct Answer)


ii. [4]
iii. [2, 4]
iv. Error

13. What's the result of this complex slicing operation?


x = [1, 2, 3, 4, 5]
y = {i: x[i:] + x[:i] for i in range(len(x))}
z = {k: sum(v[:k+1]) for k, v in y.items() if k in v}
print(max(z.values()))

i. 15 (Correct Answer)
ii. 12
iii. 10
iv. Error

14. Consider this nested dictionary and set operation:


x = {i: set(range(i, i+3)) for i in range(2, 5)}
y = {k: v & set(range(k+1)) for k, v in x.items()}
z = sum(len(v) for k, v in y.items() if max(v or [0]) < k)
print(z)

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))

i. [0, 1] (Correct Answer)


ii. [0]
iii. [1]
iv. [0, 1, 2]

16.What's the result of this operator precedence chain?

a = 123

b = 456

c = (a ^ b) >> 4 + (a & b) << 2 - (a | b) % 16

print(c)
i. 1842
ii. Error (Correct Answer)
iii. 2468
iv. 2784

17.Consider this complex shift operation:

x = 345

y = 567

result = ((x & 0xFF) << 4) | ((y & 0xFF) >> 4)

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])`

`set((1, 2))` (Correct Answer)

`{1, 2}`

`set{1, 2}`

19. What will be the output of:

s = {1, 2, 3}

s.update([3, 4, 5])

print(s)

`{1, 2}`

`{3,4,5}`

`{1, 2, 3}`

`{1, 2, 3, 4, 5}` (Correct Answer)

20. What will be the output:

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

2. Which of the following is true about the `__call__` method in Python?


a) It allows an instance of a class to be called as a function
(correct answer).
b) It is used to create class methods.
c) It is automatically called when an object is created.
d) It cannot take parameters.

3. What is the purpose of the __str__ method in a class?


a) To define a string representation of an object (correct answer).
b) To define a numeric representation of an object.
c) To create a copy of an object.

4. Consider this function that modifies a list:

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)

a. [2, 4, 6, 2, 4, 6] (Correct Answer)


b. [1, 2, 3, 2, 4, 6]
c. [2, 4, 6]
d. Error

5. What's the output of this recursive function with a base case?

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

6. Consider this function that uses a generator:

def count_up_to(n):

count = 1

while count <= n:

yield count

count += 1

print(sum(count_up_to(5)))

a. 15 (Correct Answer)
b. 10
c. 5
d. 20

7. 4. Evaluate this finally block behavior:


8. def get_value():
9. try:
10. return 1
11. finally:
12. return 2

13. x = get_value()
14. print(x)

a. 1
b. 2 (Correct Answer)
c. None
d. Error

15. 5. What's the output of this exception inheritance?


16. class CustomError(Exception):
17. pass

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. True (Correct Answer)


b. False
c. None
d. Error

1. What's the output of this nested conditional expression?


25. x = 234
26. y = 567
27. result = "High" if x > 200 else "Low" if y < 300 else "Mid" if x + y > 1000 else
"None"
28. print(result)
29. 21. What's the output of this complex matrix operation?
30. matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
31. diags = {i: [matrix[j][abs(i-j)] for j in range(len(matrix))] for i in range(len(matrix))}
32. result = {k: sum(v) for k, v in diags.items() if min(v) > k}
33. print(sum(result.values()))

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. Error (Correct Answer)


b. 4
c. 5
d. 6

1. What's the output of this bitwise operation chain?


48. x = 156
49. y = 234
50. z = (x << 2) & (y >> 1) ^ (x | y)
51. print(z)

a. 548
b. 142 (Correct Answer)
c. 712
d. 836

2. Evaluate this compound arithmetic-bitwise expression:


52. num1 = 245
53. num2 = 178
54. result = (num1 % 16) << 3 | (num2 // 8) ^ (num1 & num2)
55. print(result)

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}

60. 41. What is the type of empty set?


a. list
b. tuple
c. set (Correct Answer)
d. dictionary

61. 29. How to check if dictionary is empty?


a. `if len(d) == 0:` (Correct Answer)
b. `if d == None:`
c. `if d.empty():`
d. None
62. 30. What will be the output of:

63. d = {'a':1, 'b':2}


64. del d['a']
65. print(d)

a. {'a':1, 'b':2}
b. {'b':2} (Correct Answer)
c. Error
d. None

66. 31. What is the output of `{'a': 1, 'b': 2}.get('c', 3)`?


a. `None`
b. `'c'`
c. `3` (Correct Answer)
d. KeyError

You might also like