Attempt any five questions.
Q.) Attempt any five questions.
Subject: Digital Circuit and Design- Differentiate between a compiler and an interpreter.
Compiler:
- Translates high-level code into machine code in one go.
- Produces an executable file that can be run independently.
- Optimizes code for speed and efficiency.
- Examples: C/C++, Java (with JIT compilation).
Interpreter:
- Executes high-level code line by line.
- Does not produce an executable file.
- Interprets the code each time it is run.
- Examples: Python, JavaScript, Ruby.
- Explain the concept of recursion and provide an example.
- Recursion:
- A method or function that calls itself directly or indirectly.
- Useful for solving problems that have a recursive structure.
- Examples:
- Factorial calculation:
def factorial(n): if n == 0: return 1 return n * factorial(n - 1)
- Tree traversal:
def preorder_traversal(node): if node is not None: print(node.data) preorder_traversal(node.left) preorder_traversal(node.right)
- Factorial calculation:
- Discuss the time complexity of the following code:
def find_min(arr):
min = arr[0]
for i in range(1, len(arr)):
if arr[i] < min:
min = arr[i]
return min
- Time Complexity: O(n)
- The code iterates over the entire array once, resulting in linear time complexity.
- Explain the concept of polymorphism in object-oriented programming.
- Polymorphism:
- The ability for objects of different subclasses to respond to the same method call in different ways.
- Achieved through method overriding in subclasses.
- Examples:
- In a "Shape" class hierarchy, different shapes (e.g., circle, rectangle) can have their own
draw()
method, which provides specific drawing behavior for each shape. - In a "Animal" class hierarchy, different animals (e.g., dog, cat) can have their own
speak()
method, which produces different sounds for each animal.
- In a "Shape" class hierarchy, different shapes (e.g., circle, rectangle) can have their own
- Implement a merge sort algorithm and analyze its time complexity.
Merge Sort Algorithm:
- A divide-and-conquer sorting algorithm that works by recursively dividing the input array into smaller subarrays, sorting those subarrays, and then merging them back together.
Time Complexity:
- Worst-Case: O(n log n)
- Average-Case: O(n log n)
- Best-Case: O(n log n)