0% found this document useful (0 votes)
6 views29 pages

Python - DSA Interview

The document consists of a series of programming questions and answers related to algorithms, data structures, and Python programming concepts. Each question is followed by the correct answer and an explanation of the reasoning behind it. Topics covered include decomposition, algorithms, data types, loops, functions, and time complexity.

Uploaded by

garvitsharma666
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views29 pages

Python - DSA Interview

The document consists of a series of programming questions and answers related to algorithms, data structures, and Python programming concepts. Each question is followed by the correct answer and an explanation of the reasoning behind it. Topics covered include decomposition, algorithms, data types, loops, functions, and time complexity.

Uploaded by

garvitsharma666
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tab 2

1. Which concept helps solve complex problems by dividing them into smaller parts?

A. Debugging​
B. Decomposition​
C. Compilation​
D. Abstraction

Answer: B. Decomposition​
Explanation: Decomposition means breaking a big problem into smaller, easier parts.

2. A correct algorithm must always be:

A. Infinite​
B. Finite and well-defined​
C. Random​
D. Hardware dependent

Answer: B. Finite and well-defined​


Explanation: An algorithm must finish in limited steps and every step must be clear.

3. In IPO model, data processing occurs during:

A. Input​
B. Output​
C. Process​
D. Storage

Answer: C. Process​
Explanation: Processing is the stage where calculations or logic are applied to input.

4. Output of code
print(10 + 5 * 2)
A. 30​
B. 20​
C. 25​
D. 15

Answer: B. 20​
Explanation: Multiplication happens first (5×2=10), then addition (10+10).

5. Output
a=5
b=2
print(a // b)

A. 2.5​
B. 3​
C. 2​
D. 1

Answer: C. 2​
Explanation: // gives integer (floor) division result.

6. Output
x=3
y=7
print(x > y)

A. True​
B. False​
C. Error​
D. None

Answer: B. False​
Explanation: 3 is smaller than 7, so condition is false.
7. Output
print(type("AI"))

A. int​
B. str​
C. bool​
D. float

Answer: B. str​
Explanation: Text inside quotes is treated as a string.

8. Which is a valid variable name?

A. 2value​
B. value_2​
C. value-2​
D. @value

Answer: B. value_2​
Explanation: Variable names can contain letters, digits, and underscores but not
symbols or starting digits.

9. Which data type stores True/False?

A. int​
B. float​
C. bool​
D. str

Answer: C. bool​
Explanation: Boolean data type stores logical values True or False.
10. Result of this conversion
int("10")

A. "10"​
B. 10​
C. Error​
D. float

Answer: B. 10​
Explanation: String "10" is converted into integer 10.

11. Output
print(2 ** 3 ** 2)

A. 64​
B. 512​
C. 36​
D. 256

Answer: B. 512​
Explanation: Exponent operator works right to left → 3²=9, then 2⁹=512.

12. Which operator checks inequality?

A. ==​
B. =​
C. !=​
D. <=

Answer: C. !=​
Explanation: != checks whether two values are not equal.
13. Output
x = 15
if x > 10:
print("A")
elif x > 5:
print("B")
else:
print("C")

A. A​
B. B​
C. C​
D. Error

Answer: A. A​
Explanation: First condition is true, so "A" is printed.

14. Nested if is used when:

A. Multiple loops needed​


B. Condition inside another condition​
C. Functions inside loop​
D. Arrays used

Answer: B. Condition inside another condition​


Explanation: Nested if means an if statement inside another if.

15. Output
for i in range(1,4):
print(i)

A. 1 2 3​
B. 0 1 2​
C. 1 2 3 4​
D. 0 1 2 3

Answer: A. 1 2 3​
Explanation: range(1,4) generates 1,2,3 (4 excluded).

16. Output
for i in range(5):
if i == 3:
break
print(i)

A. 0 1 2​
B. 0 1 2 3​
C. 1 2 3​
D. 0 1

Answer: A. 0 1 2​
Explanation: Loop stops when i == 3 due to break.

17. Output
i=0
while i < 3:
print(i)
i += 1

A. 0 1 2​
B. 1 2 3​
C. Infinite​
D. Error

Answer: A. 0 1 2​
Explanation: Loop runs until i becomes 3.
18. continue statement:

A. Stops program​
B. Skips iteration​
C. Ends loop​
D. Repeats loop

Answer: B. Skips iteration​


Explanation: continue skips current iteration and moves to next loop cycle.

19. Output
def add(a,b):
return a+b
print(add(2,3))

A. 23​
B. 5​
C. Error​
D. None

Answer: B. 5​
Explanation: Function returns sum of 2 and 3.

20. Output
def test():
print("Hello")
test()

A. Hello​
B. Error​
C. None​
D. test
Answer: A. Hello​
Explanation: Function prints "Hello" when called.

21. Scope of variable inside function is:

A. Global​
B. Local​
C. Static​
D. Public

Answer: B. Local​
Explanation: Variables inside functions are accessible only within that function.

22. Output
s = "Python"
print(s[3])

A. h​
B. t​
C. o​
D. n

Answer: B. t​
Explanation: Indexing starts from 0 → P(0) y(1) t(2) h(3)? Wait correct indexing: P0 y1

⚠️
t2 h3 → Actually correct output is h, but your given answer is B (t)​
Correction: Correct answer should be A. h

23. Output
s = "AI"
print(s*2)
A. AI AI​
B. AIAI​
C. AI2​
D. Error

Answer: B. AIAI​
Explanation: String repetition repeats content.

24. Strings are:

A. Mutable​
B. Immutable​
C. Dynamic​
D. Flexible

Answer: B. Immutable​
Explanation: String values cannot be changed after creation.

25. Output
a = [1,2,3]
[Link](4)
print(a)

A. [1,2,3]​
B. [1,2,3,4]​
C. Error​
D. [4,1,2,3]

Answer: B. [1,2,3,4]​
Explanation: append() adds element at end of list.

26. Output
a = [10,20,30]
print(a[-1])
A. 10​
B. 20​
C. 30​
D. Error

Answer: C. 30​
Explanation: -1 accesses the last element of the list.

27. Lists are:

A. Mutable​
B. Immutable​
C. Fixed​
D. Static

Answer: A. Mutable​
Explanation: List elements can be changed after creation.

28. Output
t = (1,2,3)
print(t[1])

A. 1​
B. 2​
C. 3​
D. Error

Answer: B. 2​
Explanation: Tuple indexing starts from 0.

29. Tuple modification results in:


A. Update​
B. Error​
C. Replace​
D. None

Answer: B. Error​
Explanation: Tuples are immutable and cannot be modified.

30. Output
d = {"a":1,"b":2}
print(d["b"])

A. 1​
B. 2​
C. b​
D. Error

Answer: B. 2​
Explanation: Value of key "b" is 2.

31. Dictionary stores:

A. Index-value​
B. Key-value​
C. Only numbers​
D. Only text

Answer: B. Key-value​
Explanation: Dictionary maps keys to values.

32. Which mode overwrites file?


A. r​
B. w​
C. a​
D. x

Answer: B. w​
Explanation: Write mode clears existing content before writing.

33. Correct way to open file:

A. open("[Link]")​
B. open("[Link]","r")​
C. [Link]()​
D. [Link]()

Answer: B. open("[Link]","r")​
Explanation: File name and mode are passed to open().

34. Linear search complexity:

A. O(1)​
B. O(n)​
C. O(log n)​
D. O(n²)

Answer: B. O(n)​
Explanation: In worst case, all elements are checked.

35. Binary search works only on:

A. Sorted array​
B. Unsorted array​
C. Graph​
D. Stack
Answer: A. Sorted array​
Explanation: Binary search requires sorted data.

36. Best complexity possible:

A. O(n²)​
B. O(n)​
C. O(1)​
D. O(log n)

Answer: C. O(1)​
Explanation: Constant time is the fastest complexity.

37. Array elements stored in:

A. Random memory​
B. Contiguous memory​
C. Cache​
D. Stack

Answer: B. Contiguous memory​


Explanation: Array elements occupy continuous memory locations.

38. Traversal means:

A. Updating​
B. Visiting each element​
C. Sorting​
D. Deleting

Answer: B. Visiting each element​


Explanation: Traversal accesses elements one by one.
39. Output
arr = [1,2,3]
print(arr*2)

A. [1,2,3,1,2,3]​
B. [2,4,6]​
C. Error​
D. [1,2,3,2]

Answer: A. [1,2,3,1,2,3]​
Explanation: List repetition duplicates elements.

40. Output
x = [1,2]
y=x
[Link](3)
print(x)

A. [1,2]​
B. [1,2,3]​
C. Error​
D. [3,1,2]

Answer: B. [1,2,3]​
Explanation: x and y refer to same list object.

41. Output
print(bool(0))

A. True​
B. False​
C. Error​
D. None

Answer: B. False​
Explanation: Zero is treated as False.

42. Output
print("5" + "5")

A. 10​
B. 55​
C. Error​
D. 5+5

Answer: B. 55​
Explanation: Strings are concatenated.

43. Output
print(list(range(3)))

A. [1,2,3]​
B. [0,1,2]​
C. [0,1,2,3]​
D. [1,2]

Answer: B. [0,1,2]​
Explanation: range(3) generates 0 to 2.

44. Time complexity of loop


for i in range(n):
print(i)
A. O(1)​
B. O(log n)​
C. O(n)​
D. O(n²)

Answer: C. O(n)​
Explanation: Loop runs n times.

45. Nested loop complexity:

A. O(n)​
B. O(log n)​
C. O(n²)​
D. O(1)

Answer: C. O(n²)​
Explanation: One loop inside another multiplies operations.

46. Best search for sorted data:

A. Linear Search​
B. Binary Search​
C. DFS​
D. BFS

Answer: B. Binary Search​


Explanation: Binary search reduces search space by half.

47. Output
a = {1,2,3}
print(len(a))
A. 2​
B. 3​
C. 4​
D. Error

Answer: B. 3​
Explanation: Set contains three unique elements.

48. Output
print(10 % 3)

A. 1​
B. 3​
C. 0​
D. 2

Answer: A. 1​
Explanation: Modulus gives remainder.

49. Output
x = "DSA"
print(x[::-1])

A. ASD​
B. DSA​
C. SDA​
D. Error

Answer: A. ASD​
Explanation: [::-1] reverses the string.

50. Main thing interviewer checks in DSA round:


A. Syntax memorization​
B. Problem solving approach​
C. Typing speed​
D. IDE knowledge

Answer: B. Problem solving approach​


Explanation: Logic and thinking matter more than syntax.

51. What is the main property of a good algorithm?

A. Infinite steps​
B. Finite and unambiguous steps​
C. Uses recursion always​
D. Only works on arrays

Answer: B. Finite and unambiguous steps​


Explanation: Algorithm must end and be clear.

52. Which paradigm solves problems by dividing them into subproblems?

A. Greedy​
B. Divide and Conquer​
C. Dynamic Programming​
D. Brute Force

Answer: B. Divide and Conquer​


Explanation: Problem is divided, solved, then combined.

53. Which technique stores results of subproblems?

A. Recursion​
B. Dynamic Programming​
C. Greedy​
D. Backtracking
Answer: B. Dynamic Programming​
Explanation: DP avoids recomputation using storage.

54. Time complexity of accessing element in array by index:

A. O(n)​
B. O(log n)​
C. O(1)​
D. O(n²)

Answer: C. O(1)​
Explanation: Direct memory access.

55. Worst case complexity of quick sort:

A. O(n log n)​


B. O(n²)​
C. O(log n)​
D. O(n)

Answer: B. O(n²)​
Explanation: Occurs with poor pivot selection.

56. Output
print(3 * 1 ** 3)

A. 3​
B. 1​
C. 9​
D. Error

Answer: A. 3​
Explanation: Exponent first → 1³=1, then 3×1.
57. Output
print(bool("False"))

A. False​
B. True​
C. Error​
D. None

Answer: B. True​
Explanation: Non-empty strings are True.

58. Output
a = [1,2,3]
print(a[1:])

A. [1,2]​
B. [2,3]​
C. [1,2,3]​
D. Error

Answer: B. [2,3]​
Explanation: Slice starts from index 1.

59. Output
print(type([]))

A. list​
B. dict​
C. tuple​
D. set
Answer: A. list​
Explanation: [] represents a list.

60. Output
print(10/2)

A. 5​
B. 5.0​
C. 4​
D. Error

Answer: B. 5.0​
Explanation: / always returns float.

61. Complexity of binary search:

A. O(n)​
B. O(log n)​
C. O(n log n)​
D. O(1)

Answer: B. O(log n)​


Explanation: Search space halves each step.

62. Complexity of nested loop (n × n):

A. O(n)​
B. O(n²)​
C. O(log n)​
D. O(n log n)

Answer: B. O(n²)​
Explanation: Two loops multiply operations.
63. Best case of insertion sort:

A. O(n)​
B. O(n²)​
C. O(log n)​
D. O(1)

Answer: A. O(n)​
Explanation: Already sorted array.

64. Space complexity of merge sort:

A. O(1)​
B. O(n)​
C. O(log n)​
D. O(n²)

Answer: B. O(n)​
Explanation: Extra array is used.

65. Which complexity grows fastest?

A. O(n)​
B. O(n log n)​
C. O(n²)​
D. O(log n)

Answer: C. O(n²)​
Explanation: Quadratic growth is fastest here.

66. Output
x = [1,2,3]
print(len(x))
A. 2​
B. 3​
C. 4​
D. Error

Answer: B. 3​
Explanation: List has three elements.

67. Output
print("AI" * 3)

A. AI3​
B. AIAIAI​
C. AI AI AI​
D. Error

Answer: B. AIAIAI​
Explanation: String repetition.

68. Output
print(5 == 5.0)

A. True​
B. False​
C. Error​
D. None

Answer: A. True​
Explanation: Values are equal despite type difference.
69. Output
print([1,2] + [3,4])

A. [1,2,3,4]​
B. [4,6]​
C. Error​
D. [1,2][3,4]

Answer: A. [1,2,3,4]​
Explanation: Lists are concatenated.

70. Output
print(min([3,1,4,2]))

A. 1​
B. 2​
C. 3​
D. 4

Answer: A. 1​
Explanation: Minimum value is returned.

71. Linear search works best for:

A. Sorted arrays only​


B. Small datasets​
C. Graphs​
D. Trees

Answer: B. Small datasets​


Explanation: Simple and low overhead.
72. Stable sorting algorithm:

A. Quick Sort​
B. Merge Sort​
C. Heap Sort​
D. Selection Sort

Answer: B. Merge Sort​


Explanation: Maintains relative order of equal elements.

73. Average complexity of quick sort:

A. O(n log n)​


B. O(n²)​
C. O(n)​
D. O(log n)

Answer: A. O(n log n)​


Explanation: Balanced partitions on average.

74. Selection sort complexity:

A. O(n²)​
B. O(n)​
C. O(log n)​
D. O(n log n)

Answer: A. O(n²)​
Explanation: Always uses nested loops.

75. Binary search requires:

A. Sorted data​
B. Heap​
C. Stack​
D. Graph

Answer: A. Sorted data​


Explanation: Order is mandatory.

76. Output
x = {1,2,3}
print(type(x))

A. set​
B. list​
C. dict​
D. tuple

Answer: A. set​
Explanation: Curly braces with values create set.

77. Output
d = {"a":1,"b":2}
print([Link]("c",0))

A. None​
B. Error​
C. 0​
D. c

Answer: C. 0​
Explanation: Default value is returned if key missing.

78. Output
print(sum([1,2,3]))
A. 5​
B. 6​
C. 7​
D. Error

Answer: B. 6​
Explanation: Sum of all elements.

79. Output
a = (1,2,3)
print([Link](2))

A. 1​
B. 2​
C. 3​
D. Error

Answer: A. 1​
Explanation: Value 2 appears once.

80. Output
print(sorted([3,1,2]))

A. [1,2,3]​
B. [3,2,1]​
C. Error​
D. None

Answer: A. [1,2,3]​
Explanation: sorted() returns new sorted list.
81. Recursion must have:

A. Infinite loop​
B. Base case​
C. While loop​
D. Stack only

Answer: B. Base case​


Explanation: Stops infinite recursive calls.

82. Recursion uses:

A. Heap memory​
B. Call stack​
C. Disk memory​
D. Queue

Answer: B. Call stack​


Explanation: Each call is stored in stack.

83. Factorial recursion complexity:

A. O(n)​
B. O(log n)​
C. O(n²)​
D. O(1)

Answer: A. O(n)​
Explanation: One recursive call per number.

You might also like