3MCA For Coding Question Bank
3MCA For Coding Question Bank
#1. Write a Python program to find the second largest element in an array.
def second_largest(arr):
unique_arr = list(set(arr))
unique_arr.sort(reverse=True)
return unique_arr[1] if len(unique_arr) > 1 else None
def is_empty(self):
return len(self.stack) == 0
def is_empty(self):
return len(self.queue) == 0
def dequeue(self):
if self.is_empty():
return "Queue is empty"
item = self.queue[self.front]
if self.front == self.rear:
self.front = self.rear = -1
else:
self.front = (self.front + 1) % self.capacity
return item
def is_empty(self):
return self.front == -1
class SinglyLinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
current = self.head
while current.next:
current = current.next
current.next = new_node
def display(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
# 11. Write a Python program to implement a simple class for a Bank Account.
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
# 12. Write a Python program to demonstrate inheritance with a base class Animal and derived class Dog.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
# 13. Write a Python program to demonstrate polymorphism with a base class Shape and derived classes.
class Shape:
def area(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
def get_age(self):
return self.__age
class Child(Parent):
def show_message(self):
return "Message from Child"
class ClassB:
def method_b(self):
return "Method B"
@classmethod
def get_company_name(cls):
return cls.company_name
class TreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def inorder_traversal(root):
if root:
inorder_traversal(root.left)
print(root.value, end=" ")
inorder_traversal(root.right)
#20. Write a Python program to check if a binary tree is a binary search tree.
def is_bst(root, min_val=float('-inf'), max_val=float('inf')):
if not root:
return True
if not (min_val < root.value < max_val):
return False
return is_bst(root.left, min_val, root.value) and is_bst(root.right, root.value, max_val)
#21.Write a Python program to implement Depth First Search (DFS) for a graph.
#22.Write a Python program to implement Breadth First Search (BFS) for a graph.
from collections import deque
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
merge_sort(left)
merge_sort(right)
i=j=k=0
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
# 26. Write a Python program to solve the 0/1 Knapsack problem using dynamic programming.
def knapsack(values, weights, capacity):
n = len(values)
dp = [[0 for _ in range(capacity + 1)] for _ in range(n + 1)]
return dp[n][capacity]
#27.Write a Python program to find the longest common subsequence using dynamic programming.
def lcs(x, y):
m, n = len(x), len(y)
dp = [[0] * (n + 1) for _ in range(m + 1)]
return dp[m][n]