0% found this document useful (0 votes)
25 views15 pages

OOP and Algorithms: Python Concepts Explained

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts such as classes, objects, inheritance, polymorphism, encapsulation, and abstraction, along with foundational algorithms including sorting and searching techniques. It includes a series of questions and answers covering these topics, testing knowledge on Python syntax, algorithm efficiency, and OOP principles. The document serves as a study guide for understanding key programming concepts and their applications.

Uploaded by

Black Beast
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)
25 views15 pages

OOP and Algorithms: Python Concepts Explained

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts such as classes, objects, inheritance, polymorphism, encapsulation, and abstraction, along with foundational algorithms including sorting and searching techniques. It includes a series of questions and answers covering these topics, testing knowledge on Python syntax, algorithm efficiency, and OOP principles. The document serves as a study guide for understanding key programming concepts and their applications.

Uploaded by

Black Beast
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

Object-Oriented Programming :

Classes & objects Constructors, destructors Inheritance, polymorphism,


encapsulation, abstraction

Algorithm Foundations :

Basics of sorting: Bubble, Insertion, Selection, Quick Searching algorithms (linear,


binary) and use cases Algorithm efficiency & visualization

Here are the answers for questions 1 to 42, with answer choices and the correct answer
shown after the options:

1. What keyword is used to define a class in Python?

A) object
B) def
C) class
D) function
Answer: C

2. What is the special method to initialize objects in Python?

A) start
B) main
C) def
D) init
Answer: D

3. How do you declare a constructor in Python class?

A) def constructor(self):
B) def init(self):
C) init():
D) def new()
Answer: B

4. Which of these is a valid syntax to create an object of class Car?

A) Car obj = new Car()


B) obj = Car()
C) obj = new Car
D) Car obj()
Answer: B

5. What is the output of type(object()) in Python?


A) <class 'object'>
B) object
C) <type 'object'>
D) None
Answer: A

6. Destructor in Python is defined as:

A) def destroy(self):
B) def del(self):
C) def del(self):
D) del()
Answer: C

7. Which of the following signifies encapsulation?

A) Inheriting features
B) Bundling data/methods together
C) Overriding methods
D) None
Answer: B

8. Which OOP principle is Python’s private member simulated with an underscore?

A) Abstraction
B) Encapsulation
C) Polymorphism
D) Inheritance
Answer: B

9. What is used for inheritance in Python?

A) :
B) extends
C) Parentheses with base class
D) inherits
Answer: C

10. Which kind of inheritance is supported directly by Python?

A) Single
B) Multiple
C) Multilevel
D) All of the above
Answer: D

11. Which of the following features does OOP support?


A) Inheritance
B) Polymorphism
C) Encapsulation
D) Recursion
Answer: A, B, C

12. The special method __str__ is typically used for:

A) Human-readable object string


B) Deleting objects
C) Initialization
D) Displaying docstring
Answer: A

13. Which are examples of polymorphism in Python?

A) Method overriding
B) Overloading operators
C) Using *args
D) Static variables
Answer: A, B

14. Multiple inheritance is:

A) Supported in Python
B) Not supported in Python
C) Simulated by interfaces
D) Achieved using multiple parents
Answer: A, D

15. Select possible method access levels for Python class:

A) Public
B) Private (single underscore)
C) Protected (double underscore)
D) Static
Answer: A, B, C

16. If a class method increases a counter by one each time an object is created,
what is the counter after creating 5 objects from the class?

Answer: 5

17. Number of dunder methods (__init__, __del__, __str__, __repr__) in a simple user-
defined class with all these methods?

Answer: 4
18. How many parent classes does a class with this definition have?

python

class C(A, B):

pass

Answer: 2

19. How many times is the constructor called when you create 3 instances of a
class?

Answer: 3

20. How many underscores are present in the constructor’s special name?

Answer: 4

21. How many built-in types support inheritance in Python: list, dict, int, object,
str?

Answer: 5

22. How many methods will a Python class inherit if written as class MyClass(list):
pass? (Assume list has 50 methods)

Answer: 50

23. Number of new objects created:

python

x = A(); y = A(); z = A()

Answer: 3

24. Number of arguments passed to __init__ for C(1, 2)?

Answer: 3

25. What will be the output of print(type(Algorithm)) if Algorithm is a class?

A) class 'Algorithm'
B) <class 'type'>
C) <class 'object'>
D) class
Answer: B

26. Which special method is used as a constructor in Python?


A) construct
B) new
C) init
D) build
Answer: C

27. Which method is a destructor in Python?

A) del
B) destroy
C) del__
D) finalize()
Answer: A

28. Which of the following correctly creates an instance of a class Foo?

A) [Link]()
B) Foo()
C) new Foo()
D) [Link]()
Answer: B

29. How do you access a member function of an object?

A) [Link]()
B) object:function()
C) [Link]()
D) [Link]()
Answer: C

30. What is “self” in Python class methods?

A) The class
B) The module
C) The instance/object
D) Keyword
Answer: C

31. Which of the following are valid ways to declare a class in Python?

A) class C: pass
B) class C(object): pass
C) class C(); pass
D) class C: def init(self): pass
Answer: A, B, D

32. Which of the following can be members of a class?


A) Attributes
B) Methods
C) Modules
D) Classes
Answer: A, B, D

33. Which are standard “dunder” methods in class usage?

A) str
B) init
C) main
D) del
Answer: A, B, D

34. Which statements about destructors are true?

A) Called when object is garbage collected


B) Explicitly called at any time
C) Usually clean up resources
D) Have only self as argument
Answer: A, C, D

35. How many times does init run if 6 objects of a class are instantiated?

Answer: 6

36. If a class contains 3 data members and 2 methods, how many total attributes
(not counting inherited)?

Answer: 5

37. What is the value of “self.x” after executing class A: def __init__(self): self.x=7
and then making a = A() and accessing a.x?

Answer: 7

38. In single inheritance, how many parent classes does a child have?

Answer: 1

39. If a destructor method prints "Done", how many times will this print if two
objects are deleted?

Answer: 2

40. Number of underscores in del?

Answer: 4

41. Which principle allows a subclass to inherit from multiple classes?


A) Encapsulation
B) Inheritance
C) Polymorphism
D) Abstraction
Answer: B

42. Private members in Python are indicated using:

A) __var
B) var
C) var
D) priv_var
Answer: B

Multiple Choice Single Correct

43. Which of these is NOT a sorting algorithm?


A) Insertion Sort
B) Bubble Sort
C) Binary Sort
D) Quick Sort
Answer: C

44. What is returned if binary search fails to find the target?


A) -1
B) 0
C) None
D) Raises exception
Answer: A

45. What is the best average-case time complexity for any comparison-based
sorting?
A) O(1)
B) O(log n)
C) O(n^2)
D) O(n log n)
Answer: D

46. Which keyword is used in Python to define a class method?


A) classmethod
B) def
C) class
D) staticmethod
Answer: B

47. Which OOP concept refers to a function with the same name but different
implementations in different classes?
A) Encapsulation
B) Polymorphism
C) Inheritance
D) Abstraction
Answer: B

48. In Python, to declare a member protected, prefix with:


A) One underscore
B) Two underscores
C) Double slash
D) No prefix
Answer: A

49. What data structure does a recursive binary search naturally use?
A) Stack
B) Queue
C) Heap
D) Graph
Answer: A

50. In which sorting algorithm is the array divided into two subarrays around a pivot?
A) Merge Sort
B) Quick Sort
C) Bubble Sort
D) Selection Sort
Answer: B

Multiple Choice Multi Correct

51. Which of the following methods are “dunder” (double underscore) methods?
A) init
B) del
C) repr
D) main
Answer: A, B, C

52. Which sorting algorithms use swapping as their key operation?


A) Bubble Sort
B) Quick Sort
C) Merge Sort
D) Selection Sort
Answer: A, B, D

53. Which of the following can be used as a base for inheritance in Python?
A) object
B) int
C) dict
D) float
Answer: A, B, C, D

54. Which mechanisms provide data hiding in Python?


A) name mangling
B) single leading underscore
C) double leading underscore
D) no prefix
Answer: A, B, C

55. Which of these can be used as search algorithms?


A) Linear
B) Binary
C) Interpolation
D) Counting
Answer: A, B, C

56. Which situations can make selection sort more efficient?


A) Tiny arrays
B) Nearly sorted arrays
C) Large arrays
D) All equal elements
Answer: A, D

57. Which are comparative features for analyzing sorting algorithms?


A) Time complexity
B) Stability
C) Space usage
D) Implementation ease
Answer: A, B, C, D

58. For which input data is quicksort’s pivot problem most severe?
A) Already sorted
B) All equal
C) Reverse sorted
D) Random
Answer: A, B, C

59. Which OOP features does Python inherently support?


A) Multiple Inheritance
B) Operator Overloading
C) Method Overriding
D) Static Members
Answer: A, B, C

60. Which following algorithms have best-case time complexity faster than O(n^2)?
A) Bubble Sort
B) Merge Sort
C) Quick Sort
D) Insertion Sort
Answer: B, C

Numerical Answer Type

61. How many recursive calls in binary search to locate an element in a 32-element
list (worst-case)?
Answer: 6

62. How many swaps (minimum) does insertion sort require to sort an already sorted
array of 5 elements?
Answer: 0

63. If __del__ is called for each object destroyed and you delete 4 objects, how many
times is it called?
Answer: 4

64. Number of key steps (not swaps) in bubble sort of 7 elements (number of
passes)?
Answer: 6

65. Minimum comparisons to guarantee finding an item in a 10-element sorted list


(worst-case, binary search)?
Answer: 4

66. In selection sort, how many outer loop iterations to sort 8 elements?
Answer: 7

67. With 3-level inheritance (A->B->C), how many ancestor levels does class C
have?
Answer: 2
68. How many partitions does quicksort do for an 8-element array?
Answer: 7

69. How many objects are constructed if you call for i in range(9): X()?
Answer: 9

70. What is the minimum comparisons for searching in a list of one element?
Answer: 1

Here are questions 76 to 100 (final batch) with options and answers for OOP and
Algorithms:

Multiple Choice Single Correct

76. Which OOP concept relates to providing a simplified interface hiding internal
details?
A) Encapsulation
B) Abstraction
C) Polymorphism
D) Inheritance
Answer: B

77. Which method is called when you print an object directly?


A) str
B) repr
C) init
D) main
Answer: A

78. Which sorting algorithm does NOT necessarily have stable sorting?
A) Bubble Sort
B) Merge Sort
C) Quick Sort
D) Insertion Sort
Answer: C

79. Which algorithm uses divide and conquer approach?


A) Linear Search
B) Bubble Sort
C) Quick Sort
D) Counting Sort
Answer: C
80. Which principle is achieved by function overloading and overriding?
A) Inheritance
B) Encapsulation
C) Polymorphism
D) Abstraction
Answer: C

81. Which access modifier has no direct syntax in Python?


A) Public
B) Protected
C) Private
D) None of the above
Answer: C

82. What is returned by default if a function has no return statement?


A) 0
B) None
C) -1
D) Null
Answer: B

83. Which is the worst-case time complexity for linear search?


A) O(1)
B) O(n)
C) O(log n)
D) O(n^2)
Answer: B

84. Which of these is a visualization technique for sorting algorithms?


A) Bar chart
B) Pie chart
C) Scatter plot
D) Line graph
Answer: A

85. In Python, which method represents the “official” string representation of an


object?
A) str
B) repr
C) main
D) doc
Answer: B
Multiple Choice Multi Correct

86. Which sorting algorithms can be in-place?


A) Bubble Sort
B) Merge Sort
C) Quick Sort
D) Selection Sort
Answer: A, C, D

87. Which are characteristics of a good algorithm?


A) Correctness
B) Finiteness
C) Efficiency
D) Readability
Answer: A, B, C, D

88. Which principles are used to secure class attributes in Python?


A) Encapsulation
B) Name Mangling
C) Polymorphism
D) Private variables
Answer: A, B, D

89. Which are examples of stable sorting algorithms?


A) Insertion Sort
B) Bubble Sort
C) Selection Sort
D) Merge Sort
Answer: A, B, D

90. Which searching algorithms require the data to be sorted?


A) Linear
B) Binary
C) Interpolation
D) Hashing
Answer: B, C

91. Which are valid ways to access a parent class method in Python?
A) super()
B) [Link]()
C) [Link]()
D) [Link]()
Answer: A, B
92. Which algorithm types divide data into parts?
A) Divide and conquer
B) Greedy
C) Brute force
D) Dynamic programming
Answer: A, D

93. Which sorting algorithms have best-case O(n) time?


A) Insertion Sort
B) Bubble Sort
C) Selection Sort
D) Quick Sort
Answer: A, B

94. Which operations can be performed on lists and sets in Python?


A) Add
B) Remove
C) Update
D) Pop
Answer: A, B, D

95. Which Python keywords relate to inheritance?


A) class
B) super
C) def
D) return
Answer: A, B

Numerical Answer Type

96. What is the minimum number of comparisons for linear search in a list of 10?
Answer: 1

97. Number of elements after 3 rounds of bubble sort in a list of 10?


Answer: 10

98. Number of parent classes in class D(A, B, C): pass?


Answer: 3

99. How many swaps are required for selection sort with already sorted 5-element
list?
Answer: 0
100. If a quicksort divides an array of 40 elements repeatedly in halves, how
many partitions until single elements remain?
Answer: 5

You might also like