AIML Question Bank (1) - Key
AIML Question Bank (1) - Key
Technology
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Abstract Data Types (ADTs) – ADTs and classes – introduction to OOP – classes in Python –
inheritance – namespaces – shallow and deep copying
Introduction to analysis of algorithms – asymptotic notations – recursion – analyzing recursive
algorithms
Univ.
Q.No Question Marks
Ques
1.1&1.2 AbstractData Type and Classes
PART A
7. Define data structure and how the data types are classified at the 2 N/D 23
higher level.
PART B
1. Design and implement a time date ADTthat can be used to 13 A/M 21
represent both a date and time as a single entity.
2. A click counter is a small hand-held device that contains a push 13 A/M 21
button and a count display. To increment the counter, the button
is pushed and the new count shows in the display. Clicker
counters also contain a button that can be pressed to reset the
counter to zero. Design and implement the Counter ADT that
functions as a handheldclicker.
1.3&1.4 introduction to OOP – classes in Python
PART A
1. List out the methodology in OOPS. 2
2. Define abstraction,encapsulation,inheritance,polymorphism,
constructor.
3. Define oop.give real time
4. What is data type in python?
8. Is python Adaptable?
PARTB & C
1. List out all the oop concept and explain them in detail. 2
PART A
1. Define Scope. 6
2. What is namespaces? 7
2. Discuss the shallow and deep copying and differentiate its pros 13
and cons.
3. Difference and explain in detail about the shallow and deep copy 8
of the class with suitable example.
Introduction to analysis of algorithm &Asymptotic notations
PART A
PART B & C
PART A
PART B
Completion Verified by
Signature of Completion Verified
Concerned Dept.
Faculty by Dept.
HOD
HOD
List ADT – array-based implementations – linked list implementations – singly linked lists –
circularly linked lists – doubly linked lists – applications of lists – Stack ADT – Queue ADT –
double ended queues
Univ
Q.No Question Marks
Qus
2.1&2.2 List ADT & array based implementation
PART A
1. Define an array.Give example. 2 AM 23
An array is a data structure that stores a collection of elements, typically of the
same type, arranged in a contiguous block of memory. Each element in the
array is accessed using an index that represents its position within the array.
Example:
python
# Define an array in Python
my_array = [1, 2, 3, 4, 5]
PART B & C
# Example usage:
my_array = [1, 2, 3, 4, 5]
Mount Zion College of Engineering &
Technology
insert_in_middle(my_array, 99)
print(my_array) # Output: [1, 2, 99, 3, 4, 5]
PART A
PART B & C
PART A
1. Develop the program in python to delete a node the minimum value from a 15
singly linked list.
To delete a node with the minimum value from a singly linked list in Python, you
need to traverse the list to find the minimum value and then delete the node
containing that value. Here's a step-by-step explanation and the corresponding
Python code:
Traverse the Linked List:
Start traversing the linked list from the head node.
Keep track of the minimum value found so far and the node that contains it.
Delete the Node:
Once you find the node with the minimum value, remove it from the linked list.
Update the pointers of the previous node to skip the node to be deleted.
Free the memory allocated to the node.
Here's the Python code implementing the above steps:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def delete_min_node(self):
if not self.head:
return "List is empty"
def display(self):
current_node = self.head
while current_node:
print(current_node.data, end=" -> ")
current_node = current_node.next
print("None")
# Example usage:
if __name__ == "__main__":
linked_list = LinkedList()
linked_list.append(10)
linked_list.append(5)
linked_list.append(20)
linked_list.append(3)
linked_list.append(15)
linked_list.delete_min_node()
print("\nLinked List after deleting the node with minimum value:")
linked_list.display()
2. Write an algorithm to merge two sorted linked lists into a single sorted list.
Algorithm: Merge Two Sorted Linked Lists
Input:
Head pointers of two sorted linked lists, let's call them list1_head and
list2_head.
Output:
Head pointer of the merged sorted linked list.
Procedure:
Initialize three pointers: merged_head, current, and tail.
merged_head: Points to the head of the merged list (initially None).
current: Points to the current node being processed in the merged list.
tail: Points to the last node of the merged list (initially None).
Initialize current1 and current2 pointers to the heads of list1_head and
Mount Zion College of Engineering &
Technology
list2_head, respectively.
Iterate while both current1 and current2 are not None: a. Compare the values of
current1 and current2. b. Set the next pointer of current to the node with the
smaller value. c. Move the pointer of the list with the smaller value (current1 or
current2) to its next node. d. Move current to its next node as well.
After the loop, append the remaining nodes of the non-empty list to the merged
list.
Return the merged_head, which points to the head of the merged sorted linked
list.
Here's a Python-like pseudocode implementation of the algorithm:
def merge_sorted_lists(list1_head, list2_head):
# Initialize pointers
merged_head = None
current = None
tail = None
current1 = list1_head
current2 = list2_head
PART A
PART B & C
reference by setting the next pointer of the last node to None. Here's a step-by-
step explanation and the corresponding Python code:
Find the Last Node:
Start from the head node and traverse the circular linked list until you reach the
node that points back to the head node (i.e., the last node).
Break the Circular Reference:
Once you find the last node, set its next pointer to None, effectively breaking
the circular reference.
Here's the Python code implementing the above steps:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def transform_to_chain(self):
if not self.head or not self.head.next:
return "List is already a chain"
current_node = self.head
# Traverse until the last node
while current_node.next != self.head:
current_node = current_node.next
# Break the circular reference
Mount Zion College of Engineering &
Technology
current_node.next = None
def display(self):
if not self.head:
print("List is empty")
return
current_node = self.head
while current_node:
print(current_node.data, end=" -> ")
current_node = current_node.next
if current_node == self.head:
break
print("None")
# Example usage:
if __name__ == "__main__":
circular_list = CircularLinkedList()
circular_list.append(1)
circular_list.append(2)
circular_list.append(3)
circular_list.append(4)
circular_list.append(5)
circular_list.transform_to_chain()
print("\nCircular Linked List transformed into Chain:")
circular_list.display()
PART A
Diagram:
PART B & C
1. Write a function to delete the node n from the given doubly linked list.
P q r n s t z 15
To delete a specific node n from a doubly linked list, you typically need to adjust
the pointers of the adjacent nodes (r and s in this case) to bypass the node n.
Here's a function in Python to achieve this:
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
def delete_node(n):
if n is None or n.next is None:
return
# Example usage:
if __name__ == "__main__":
# Create the doubly linked list: P -> q -> r -> n -> s -> t -> z
P = Node("P")
q = Node("q")
r = Node("r")
n = Node("n")
s = Node("s")
t = Node("t")
z = Node("z")
P.next = q
q.prev = P
q.next = r
r.prev = q
r.next = n
n.prev = r
n.next = s
s.prev = n
s.next = t
t.prev = s
t.next = z
z.prev = t
| | | | |
| | | | |
v v v v v
3. Illustrate Illustrate the algorithms to implement the doubly linked list and 15
perform all the operations on the created list.
. Doubly Linked List Implementation:
Node Definition:
Each node of the doubly linked list contains:
Mount Zion College of Engineering &
Technology
Data: Holds the value of the node.
Pointer to the Previous Node (prev): Points to the previous node in the list.
Pointer to the Next Node (next): Points to the next node in the list.
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
Operations on Doubly Linked List:
Insertion:
Append:
Add a new node at the end of the list.
Traverse the list until the last node, and adjust pointers accordingly.
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
new_node.prev = current
Prepend:
Add a new node at the beginning of the list.
Adjust pointers of the new node and the head node.
def prepend(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
else:
new_node.next = self.head
self.head.prev = new_node
self.head = new_node
Deletion:
Delete:
Remove a node with a given value from the list.
Traverse the list until finding the node with the value to be deleted.
Adjust pointers of the previous and next nodes to bypass the node to be
deleted.
def delete(self, value):
if not self.head:
return
current = self.head
while current:
if current.data == value:
if current == self.head:
self.head = current.next
if self.head:
self.head.prev = None
else:
current.prev.next = current.next
if current.next:
current.next.prev = current.prev
Mount Zion College of Engineering &
Technology
return
current = current.next
Traversal:
Display Forward:
Traverse the list from the head to the last node and print each node's value.
def display_forward(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
Display Backward:
Traverse the list from the last node to the head and print each node's value.
def display_backward(self):
current = self.head
while current and current.next:
current = current.next
while current:
print(current.data, end=" -> ")
current = current.prev
print("None")
Iteration 4: Pop 3, 2 from the stack, and calculate 3 - 2 = 1. Push 1 onto the
stack
Stack: [1, 1]
Iteration 5: Pop 1, 1 from the stack, and calculate 1 - 1 = 0. Push 0 onto the
stack
Stack: [0]
Iteration 6: Pop 2, 0 from the stack, and calculate 2 + 0 = 2. Push 2 onto the
stack
Stack: [2]
Iteration 7: Pop 1, 2 from the stack, and calculate 1 + 2 = 3. Push 3 onto the
stack
Stack: [3]
At the end, the result is the top element of the stack, which is 3.
3. Write the postfix form for the expression -A+B+C+D? 2
Input: -A+B+C+D
Output: AB-C+D+
Step-by-step conversion:
- Initialize an empty stack and an empty list.
- Iterate through each token in the infix expression:
- Token: - (operator)
- Stack is empty, so push '-' onto the stack.
- Token: A (operand)
- Add 'A' to the postfix expression list.
- Token: + (operator)
- Pop '-' from the stack and add it to the postfix expression list.
- Push '+' onto the stack.
- Token: B (operand)
- Add 'B' to the postfix expression list.
- Token: + (operator)
- Pop '+' from the stack and add it to the postfix expression list.
- Push '+' onto the stack.
Mount Zion College of Engineering &
Technology
- Token: C (operand)
- Add 'C' to the postfix expression list.
- Token: + (operator)
- Pop '+' from the stack and add it to the postfix expression list.
- Push '+' onto the stack.
- Token: D (operand)
- Add 'D' to the postfix expression list.
- Pop any remaining operators from the stack and add them to the postfix
expression list.
- Final postfix expression: AB-C+D+
4. What are the postfix and prefix forms of the expression?
PART B & C
1. Explain how polynomial expression can represented using linked list 15
Polynomial expressions can be represented efficiently using linked lists, where
each term of the polynomial is represented by a node in the linked list. Each
node typically contains two fields:
Coefficient: Represents the coefficient of the term.
Exponent: Represents the exponent of the term.
𝑃(𝑥)=3𝑥2+2𝑥−5P(x)=3x2+2x−5
For example, consider the polynomial expression:
class Polynomial:
10. def __init__(self):
11. self.head = None
12.
13. def add_term(self, coefficient, exponent):
14. new_term = Term(coefficient, exponent)
15. if not self.head:
16. self.head = new_term
17. else:
18. current = self.head
19. while current.next:
20. current = current.next
21. current.next = new_term
22.
23. def display(self):
24. current = self.head
Mount Zion College of Engineering &
Technology
25. while current:
26. print(f"{current.coefficient}x^{current.exponent}", end=" + " if
current.next else "")
27. current = current.next
28. print("")
29.
30. # Example usage:
31. if __name__ == "__main__":
32. poly = Polynomial()
33. poly.add_term(3, 2)
34. poly.add_term(2, 1)
35. poly.add_term(-5, 0)
36.
37. print("Polynomial Representation:")
38. poly.display()
class Polynomial:
def __init__(self):
self.head = None
def display(self):
current = self.head
while current:
print(f"{current.coefficient}x^{current.exponent}", end=" + " if
current.next else "")
current = current.next
print("")
while current1:
result_poly.add_term(current1.coefficient, current1.exponent)
current1 = current1.next
while current2:
result_poly.add_term(current2.coefficient, current2.exponent)
current2 = current2.next
return result_poly
# Example usage:
if __name__ == "__main__":
# Create first polynomial: 3x^2 + 2x - 5
poly1 = Polynomial()
poly1.add_term(3, 2)
poly1.add_term(2, 1)
poly1.add_term(-5, 0)
PART A
1. Define stack
A stack is a linear data structure that follows the Last In, First Out (LIFO) 2
principle, meaning that the last element added to the stack is the first one to be
removed. It can be visualized as a collection of elements with two primary
operations: push and pop.
Push: Adds an element to the top of the stack.
Pop: Removes the element from the top of the stack.
Stacks typically support other operations such as peek (viewing the top element
without removing it) and isEmpty (checking if the stack is empty). They are
commonly used in various applications such as expression evaluation, function
call management, undo mechanisms, and more.
2. What are the various operations performed on the stack?
Push: Adds an element to the top of the stack. 2
4. Show the simulation using stack with for the following expression to
convert infix to postfix:p*q+(r-s/t) 2
Tokens: 𝑝p, ∗∗, 𝑞q, ++, ((, 𝑟r, −−, 𝑠s, //, 𝑡t, ))
Postfix expression:
PART B & C
PART A
1. Define queue 2
A queue is a linear data structure that follows the First In, First Out (FIFO)
principle, meaning that the element that is added first to the queue will be the
first one to be removed. It can be visualized as a collection of elements with
two primary operations: enqueue and dequeue.
Enqueue: Adds an element to the rear of the queue.
Mount Zion College of Engineering &
Technology
Dequeue: Removes the element from the front of the queue.
5. What is dequeue? Explain its types and its operations with example. 15
Def:2m operation:10m example:3m
2.10 Double ended queues
PART B & C
dd/mm/ yy
dd/mm/yy dd/mm/yy
Mount Zion College of Engineering &
Technology